Skip to content

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

Before starting, ensure your system is up-to-date:

bash
sudo apt update
sudo apt upgrade -y

Installing Go

Method 1: Installing from Ubuntu Repository

The simplest way to install Go is using the Ubuntu package manager:

bash
sudo apt install golang-go

This method is straightforward but may not provide the latest version.

  1. Download the latest Go binary from the official website:
bash
wget https://go.dev/dl/go1.21.0.linux-amd64.tar.gz
  1. Extract the archive to the /usr/local directory:
bash
sudo tar -C /usr/local -xzf go1.21.0.linux-amd64.tar.gz
  1. Clean up the downloaded file:
bash
rm go1.21.0.linux-amd64.tar.gz

Setting Up Go Environment

  1. Create a directory for your Go workspace:
bash
mkdir -p $HOME/go/{bin,src,pkg}
  1. Configure Go environment variables by adding the following to your ~/.profile or ~/.bashrc file:
bash
echo 'export GOPATH=$HOME/go' >> ~/.profile
echo 'export GOROOT=/usr/local/go' >> ~/.profile
echo 'export PATH=$PATH:$GOROOT/bin:$GOPATH/bin' >> ~/.profile
  1. Apply the changes:
bash
source ~/.profile

Configuring for Development

Install Development Tools

  1. Install essential development tools:
bash
sudo apt install -y build-essential git curl
  1. Set up Go modules (for Go 1.16+):
bash
go env -w GO111MODULE=on
  1. Install useful Go development tools:
bash
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@latest

Configure IDE/Editor

For VSCode:

  1. Install the Go extension from the marketplace
  2. Run the command Go: Install/Update Tools to install all necessary tools

Configuring for Production

Security Considerations

  1. Create a dedicated user for running Go applications:
bash
sudo adduser --system --group golang
  1. Set up proper permissions for your Go application directories:
bash
sudo mkdir -p /var/www/go-apps
sudo chown golang:golang /var/www/go-apps

Setting Up Systemd Service

Create a systemd service file for your Go application:

bash
sudo nano /etc/systemd/system/goapp.service

Add 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.target

Enable and start the service:

bash
sudo systemctl enable goapp.service
sudo systemctl start goapp.service

Setting Up Nginx as a Reverse Proxy

  1. Install Nginx:
bash
sudo apt install -y nginx
  1. Create a configuration file:
bash
sudo nano /etc/nginx/sites-available/goapp
  1. 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;
    }
}
  1. Enable the site and restart Nginx:
bash
sudo ln -s /etc/nginx/sites-available/goapp /etc/nginx/sites-enabled/
sudo systemctl restart nginx

Verifying Installation

Verify that Go is correctly installed:

bash
go version

Verify that your Go environment is set up correctly:

bash
go env

Test with a simple "Hello, World!" program:

bash
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.go

Troubleshooting

Common Issues

  1. "go: command not found":

    • Check if Go is installed: ls -la /usr/local/go
    • Ensure PATH is set correctly: echo $PATH
  2. Permission denied errors:

    • Check file permissions: ls -la $GOPATH
    • Fix with: chmod -R 755 $GOPATH
  3. Module-related errors:

    • Ensure GO111MODULE is set correctly: go env GO111MODULE
    • Try clearing the module cache: go clean -modcache
  4. Missing dependencies:

    • Run: go mod tidy

Updating Go

To update Go to a newer version:

  1. Download the new version
  2. Remove the old version: sudo rm -rf /usr/local/go
  3. Extract the new version: sudo tar -C /usr/local -xzf go1.xx.x.linux-amd64.tar.gz
  4. 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.