Installing and Configuring Go (Golang) on Ubuntu 22.04
This guide covers how to install, configure, and optimize Go for both development and production environments on Ubuntu 22.04 LTS.
Table of Contents
- Prerequisites
- Installing Go
- Setting Up Go Environment
- Configuring for Development
- Configuring for Production
- Verifying Installation
- Troubleshooting
Prerequisites
Before starting, ensure your system is up-to-date:
sudo apt update
sudo apt upgrade -yInstalling Go
Method 1: Installing from Ubuntu Repository
The simplest way to install Go is using the Ubuntu package manager:
sudo apt install golang-goThis method is straightforward but may not provide the latest version.
Method 2: Installing from Official Go Website (Recommended)
- Download the latest Go binary from the official website:
wget https://go.dev/dl/go1.21.0.linux-amd64.tar.gz- Extract the archive to the
/usr/localdirectory:
sudo tar -C /usr/local -xzf go1.21.0.linux-amd64.tar.gz- Clean up the downloaded file:
rm go1.21.0.linux-amd64.tar.gzSetting Up Go Environment
- Create a directory for your Go workspace:
mkdir -p $HOME/go/{bin,src,pkg}- Configure Go environment variables by adding the following to your
~/.profileor~/.bashrcfile:
echo 'export GOPATH=$HOME/go' >> ~/.profile
echo 'export GOROOT=/usr/local/go' >> ~/.profile
echo 'export PATH=$PATH:$GOROOT/bin:$GOPATH/bin' >> ~/.profile- Apply the changes:
source ~/.profileConfiguring for Development
Install Development Tools
- Install essential development tools:
sudo apt install -y build-essential git curl- Set up Go modules (for Go 1.16+):
go env -w GO111MODULE=on- Install useful Go development tools:
go install golang.org/x/tools/gopls@latest
go install github.com/ramya-rao-a/go-outline@latest
go install github.com/go-delve/delve/cmd/dlv@latest
go install honnef.co/go/tools/cmd/staticcheck@latestConfigure IDE/Editor
For VSCode:
- Install the Go extension from the marketplace
- Run the command
Go: Install/Update Toolsto install all necessary tools
Configuring for Production
Security Considerations
- Create a dedicated user for running Go applications:
sudo adduser --system --group golang- Set up proper permissions for your Go application directories:
sudo mkdir -p /var/www/go-apps
sudo chown golang:golang /var/www/go-appsSetting Up Systemd Service
Create a systemd service file for your Go application:
sudo nano /etc/systemd/system/goapp.serviceAdd the following content:
[Unit]
Description=Go Application
After=network.target
[Service]
Type=simple
User=golang
WorkingDirectory=/var/www/go-apps/myapp
ExecStart=/var/www/go-apps/myapp/myapp
Restart=on-failure
RestartSec=5
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=goapp
[Install]
WantedBy=multi-user.targetEnable and start the service:
sudo systemctl enable goapp.service
sudo systemctl start goapp.serviceSetting Up Nginx as a Reverse Proxy
- Install Nginx:
sudo apt install -y nginx- Create a configuration file:
sudo nano /etc/nginx/sites-available/goapp- Add the following configuration:
server {
listen 80;
server_name your-domain.com;
location / {
proxy_pass http://localhost:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}- Enable the site and restart Nginx:
sudo ln -s /etc/nginx/sites-available/goapp /etc/nginx/sites-enabled/
sudo systemctl restart nginxVerifying Installation
Verify that Go is correctly installed:
go versionVerify that your Go environment is set up correctly:
go envTest with a simple "Hello, World!" program:
mkdir -p $HOME/go/src/hello
echo 'package main
import "fmt"
func main() {
fmt.Println("Hello, Go!")
}' > $HOME/go/src/hello/hello.go
cd $HOME/go/src/hello
go run hello.goTroubleshooting
Common Issues
"go: command not found":
- Check if Go is installed:
ls -la /usr/local/go - Ensure PATH is set correctly:
echo $PATH
- Check if Go is installed:
Permission denied errors:
- Check file permissions:
ls -la $GOPATH - Fix with:
chmod -R 755 $GOPATH
- Check file permissions:
Module-related errors:
- Ensure GO111MODULE is set correctly:
go env GO111MODULE - Try clearing the module cache:
go clean -modcache
- Ensure GO111MODULE is set correctly:
Missing dependencies:
- Run:
go mod tidy
- Run:
Updating Go
To update Go to a newer version:
- Download the new version
- Remove the old version:
sudo rm -rf /usr/local/go - Extract the new version:
sudo tar -C /usr/local -xzf go1.xx.x.linux-amd64.tar.gz - Verify the update:
go version
This guide covers the basics of setting up Go on Ubuntu 22.04 for both development and production use. For more advanced configurations, consult the official Go documentation.