<a id="install-golang"></a>

# How to set up a development environment for Go on Ubuntu

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

## 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 [Pre-compiled Go binaries]() for instructions on how to install Go without a package manager.

### Debian package

1. Update the list of available packages:
   `dev@ubuntu:~$ ``sudo apt update`
2. Install the latest version of Go:
   `dev@ubuntu:~$ ``sudo apt install golang-go`
3. Confirm successful installation with:
   `dev@ubuntu:~$ ``go version`

#### NOTE
To install a specific version of Go use:

`dev@ubuntu:~$ ``sudo apt install golang-<version>`

### Snap package

1. Install the latest Go snap:
   `dev@ubuntu:~$ ``sudo snap install go --classic`
2. Confirm successful installation:
   `dev@ubuntu:~$ ``go version`

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

`dev@ubuntu:~$ ``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:
  `dev@ubuntu:~$ ``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:
  `dev@ubuntu:~$ ``source $HOME/.profile`

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

<a id="precompiled-go-binaries"></a>

### Pre-compiled Go binaries

Pre-compiled Go binaries are available in a compressed format on the [release page](https://go.dev/dl/) of the official Go website.

1. Fetch a specific Go version with `wget`:
   `dev@ubuntu:~$ ``wget https://go.dev/dl/go<version>.linux-amd64.tar.gz `
2. Extract the files into an appropriate directory:
   `dev@ubuntu:~$ ``sudo tar -C /usr/local -xzf go<version>.linux-amd64.tar.gz`
3. Add the `go` binary to your `$PATH` environment variable:
   `dev@ubuntu:~$ ``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:
   `dev@ubuntu:~$ ``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:
   `dev@ubuntu:~$ ``/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:

- [Visual Studio Code](https://code.visualstudio.com/): a free editor with a dedicated [Go extension](https://marketplace.visualstudio.com/items?itemName=golang.Go) maintained by the Go Team
- [GoLand](https://www.jetbrains.com/go/): a paid editor developed specifically for the Go programming language

Both can be installed as snaps:

`dev@ubuntu:~$ ``sudo snap install code --classic``dev@ubuntu:~$ ``sudo snap install goland --classic`

### Delve debugger and official Go language server

IDEs used for Go development commonly rely on [Delve](https://github.com/go-delve/delve) for debugging.
Delve can also be installed as a standalone program and run [on the command line](https://github.com/go-delve/delve/blob/master/Documentation/cli/getting_started.md).

To install Delve, run:

`dev@ubuntu:~$ ``sudo apt install delve`

An overview of basic Delve use is included in our [Develop with Go on Ubuntu](https://documentation.ubuntu.com/ubuntu-for-developers/tutorials/go-use.md#use-go) tutorial.

A Go language server, [`gopls`](https://pkg.go.dev/golang.org/x/tools/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:

`dev@ubuntu:~$ ``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:

`dev@ubuntu:~$ ``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.

## What next

See the tutorial introducing the use of Go and related tooling: [Develop with Go on Ubuntu](https://documentation.ubuntu.com/ubuntu-for-developers/tutorials/go-use.md#use-go).
