How to install Go on Linux using official binary releases

Installing Go on Linux

In this tutorial we walk through how to install the Go programming language on Linux. We are using the binary distribution you can download from Golang.org. Additionally we talk about why we prefer this method over other methods for installing Go.

GoLang Language - gophers and people on computers

Why install go on Linux via this method?

Linux distributions have their own package management systems. For example apt on Debian variants and yum on Fedora variants. Package managers are powerful tools enabling you to easily install, track and upgrade software you run on your system.

However for faster moving software and tools the packages typically lag behind what is current. For example the current version of Go is 1.13 as of this writing. If I install Go via apt on Ubuntu 18.04 I will get version 1.10. I can use the –dry-run parameter with apt-get install to see what apt will install.

apt-get --dry-run install golang

If I look through the output I see that 1.10 will be installed.

Inst golang-1.10 (1.10.4-2ubuntu1~18.04.1 Ubuntu:18.04/bionic-updates [all])

1.10 is too old for my needs. When I need Go on a system I prefer to install official binaries released by Golang.org. The result is that I can get whatever version I want. Additionally I am using the builds that are supported and tested by the GoLang community.

Alternative methods to install Go on Linux?

Installing the Golang.org binary release is not the only way to get the latest Go. For example there are non-official distributions and packages built by users in the community. You can install a later version Go on Ubuntu via these methods:

When considering these option remember the Go project does not support or test these other distributions. Additionally you are trusting the entities that maintain these builds. If the Go binaries were to get hacked, I am more confident the Golang community will catch it sooner and notify me sooner because so many people are using their distribution. I know I can always get the latest version without hoping a package maintainer is going to keep their builds up to date. Hence I choose to use official Go binaries and do not trust the others options for production use. I think its great that they package up Go in easy to consume packages. But it does not meet my requirements.

The Go Github repo has an Ubuntu focused wiki page with more details about these options. Check it out if you are interested in investigating them further.

Install the official Go binary release

Step 1 Download

The first thing we need to do is download the latest Go binary release from the Go download site.

I will download version 1.13.5 for Linux. In this case I am getting the X86 64bit version. I use the following wget command to fetch the archive file.

wget https://dl.google.com/go/go1.13.5.linux-amd64.tar.gz

Step 2 Extract

Next I need to explode the archive file. I will do this using a tar command which decompresses the file and untars it at the same time.

 tar -xvzf go1.13.5.linux-amd64.tar.gz 

The flags to the tar command in this case mean the following:

  • x – extract. The command to extract files.
  • v – verbose. This gives us detailed about about what the command is doing.
  • z – decompress using gzip. This tells the tar command to decompress the file using gzip.
  • f – file. This tells tar that we are giving it a file path parameters. Versus for example using stdin.

When you run this command you get a bunch of output displaying the files being extracted from the archive. Now I should have a directory called go.

ubuntu@packer-test:~$ ls -l
total 117264
drwxr-xr-x 10 ubuntu ubuntu      4096 Dec  4 22:53 go
-rw-rw-r--  1 ubuntu ubuntu 120074076 Dec  5 01:25 go1.13.5.linux-amd64.tar.gz

Step 3 move the go binaries

Next we will move this go directory into /usr/local. We are doing this so the Go binaries are located in a system wide location. We could also move them into our home directory if our intention to is to only make it available for one user. Lets assume we want to put it in a central location. We must use sudo to move the go directory.

sudo mv go /usr/local

Step 4 Update PATH environment Variable

At this point the installation is done. The last remaining step is to update your PATH environment variable to include the go/bin directory. You can do this on the fly by setting the PATH environment variable from the command like. Do it like so:

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

However this change will only be good for your current session and will not persist. You should update your .bashrc file to include the following line:

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

If you are thinking that line looks like the command we just ran you are correct! We are just leveraging the .bashrc to run the command to set the variable for us. Because the .bashrc file is loaded every time we start a new shell. You can immediately load your new .bashrc file like so:

source ~/.bashrc

Step 5 Validate

Now lets confirm we can run go. Do this by running go help.

ubuntu@packer-test:~$ go help
Go is a tool for managing Go source code.

Usage:

        go <command> [arguments]

The commands are:

        bug         start a bug report
        build       compile packages and dependencies
        clean       remove object files and cached files
        doc         show documentation for package or symbol
        env         print Go environment information
        fix         update packages to use new APIs
        fmt         gofmt (reformat) package sources
        generate    generate Go files by processing source
        get         download and install packages and dependencies
        install     compile and install packages and dependencies
        list        list packages or modules
        mod         module maintenance
        run         compile and run Go program
        test        test packages
        tool        run specified go tool
        version     print Go version
        vet         report likely mistakes in packag

Conclusion

We suggest you consider using official Go binaries when installing Go on your system. By using official binary distributions you ensure you can always install the latest version. Typically newer than what is available via the operating system’s package manager. Additionally you are not relying on untrusted parties maintaining a PPA or similar archive. If you are interested in installing and managing multiple versions of the Go binary release for development purposes, check out GVM. GVM is the Go Version Manager.