How to set up a development environment for Go on Ubuntu

Go is a popular language for back-end web development, microservices and CLI tools. This how-to guide outlines how to install a Go distribution and set up a development environment on Ubuntu.

Installing and setting up Go

Go includes the go command, a compiler and other tools. You can install Go as a Debian package or as a snap. Snap provides a larger number of Go versions but — unlike a Debian package — these cannot be installed in a Docker container.

Note

See Precompiled Go binaries for instructions on how to install Go without a package manager.

Debian package

  1. Update the list of available packages:

    sudo apt update
    
  2. Install the latest version of Go:

    sudo apt install golang-go
    
  3. Confirm successful installation with:

    go version
    

Note

To install a specific version of Go use:

sudo apt install golang-<version>

Snap package

  1. Install the latest Go snap:

    sudo snap install go --classic
    
  2. Confirm successful installation:

    go version
    

Note

To install a specific version of Go, specify the channel; for example:

sudo snap install go --channel=1.22/stable --classic

Adding the Go binary to $PATH

When not using a package manager to install Go, you need to add the Go binary to your $PATH environment variable.

  • To temporarily add go to your $PATH, run:

    export PATH=$PATH:/path/to/your/go/bin
    
  • To persist the change and make go available in new terminal sessions and across reboots, append the above line to $HOME/.profile (or /etc/profile) and source the file:

    source $HOME/.profile
    

Note

The files to modify and commands to use when modifying the $PATH may vary depending on your shell environment.

Precompiled Go binaries

Precompiled Go binaries are available in a compressed format on the release page of the official Go website.

  1. Fetch a specific Go version with wget:

    wget https://go.dev/dl/go<version>.linux-amd64.tar.gz 
    
  2. Extract the files into an appropriate directory:

    sudo tar -C /usr/local -xzf go<version>.linux-amd64.tar.gz
    
  3. Add the go binary to your $PATH environment variable:

    export PATH=$PATH:/usr/local/go/bin
    

Downloading and using multiple Go versions

Sometimes it may be necessary to run multiple Go versions on the same machine.

  1. To install Go versions 1.21 and 1.23, run:

    sudo apt install golang-1.21 golang-1.23
    

    Each go binary is installed in /usr/lib/go-<version>/bin/

  2. Test the go binaries by calling them with their full paths:

    /usr/lib/go-1.21/bin/go version
    /usr/lib/go-1.23/bin/go version
    

Editing and debugging

While it is possible to write and edit Go code using any plain-text editor, various Integrated Development Environments (IDEs) offer features to simplify the development process.

Advanced text editors can also be extended using Language Server Protocol plugins to enhance the user experience.

Integrated Development Environments and editors

Some of the most common IDEs used for Go are:

Both can be installed as snaps:

sudo snap install code --classic
sudo snap install goland --classic

Delve debugger and official Go language server

IDEs used for Go development commonly rely on Delve for debugging. Delve can also be installed as a standalone program and run on the command line.

To install Delve, run:

sudo apt install delve

An overview of basic Delve use is included in our Develop with Go on Ubuntu tutorial.

A Go language server, gopls, is actively maintained, which has helped ensure that Go is widely supported across many editors, including Emacs, (Neo)Vim, and others.

To install gopls from the Ubuntu archive:

sudo apt install gopls

Note

If you have Go installed and go in your $PATH, then the latest version of many Go tools like gopls and Delve can be installed with:

go install url/of/tool/<name-of-tool>@latest

This is useful when you need a version of a tool that is not yet available through the package manager.

Cross-compilation

Go has excellent cross-platform build capabilities. To build a program called hello.go, containing valid Go code, run:

go build hello.go

This builds a binary that can be run on your Ubuntu system.Different target systems can be set for the compiler.

To set the environment for a Windows AMD64 build, run:

export GOOS=windows GOARCH=amd64

This causes go build to create a hello.exe binary that runs on Windows.

To test the (Linux) binary, execute it by running:

./hello

Note

For the basics of writing and testing a ‘Hello, world~’ program in Go, see our Develop with Go on Ubuntu tutorial.

Building for multiple targets

For a full list of targets, run:

go tool dist list

For this example, filter the output to Windows and Linux on amd:

go tool dist list | grep 'amd' | grep -E 'windows|linux'

Create a Makefile that automatically sets the build environment and creates executable binaries for both Windows and Linux platforms:

EXE=heygo
WINDOWS=$(EXE)_win_amd64.exe
LINUX=$(EXE)_linux_amd64

.PHONY: all clean

all: windows linux

windows: $(WINDOWS)
linux: $(LINUX)

$(WINDOWS):
  env GOOS=windows GOARCH=amd64 go build -v -o $(WINDOWS) -ldflags="-s -w" ./heygo.go

$(LINUX):
  env GOOS=linux GOARCH=amd64 go build -v -o $(LINUX) -ldflags="-s -w" ./heygo.go

clean:
  rm -f $(WINDOWS) $(LINUX)

Generate the builds and test the Linux build:

make all
./hello_linux_amd64

What next

See the tutorial introducing the use of Go and related tooling: Develop with Go on Ubuntu.