Home DevOps How to Install Go on Ubuntu 20.04

How to Install Go on Ubuntu 20.04

by admin

Go is a modern programming language developed by Google and has been used to write many well known applications such as Docker, Terraform and Kubernetes. This short article covers how to install Go on Ubuntu so that you can get up and running with Go!

Download the Go Installation Files

First of all we need to download the install package from the Go website’s downloads page.

install-go-ubuntu

As I am looking to install Go on Linux, I want to download the go1.17.5.linux-amd64.tar.gz file. To do so I want to right click the hyperlink, and copy it. I will use wget to download the file into my Ubuntu systems /tmp folder:

$ cd /tmp
$ wget https://go.dev/dl/go1.17.5.linux-amd64.tar.gz

The file should then download:

Resolving go.dev (go.dev)... 2001:4860:4802:32::15, 2001:4860:4802:38::15, 2001:4860:4802:34::15, ...
Connecting to go.dev (go.dev)|2001:4860:4802:32::15|:443... connected.
Saving to: ‘go1.17.5.linux-amd64.tar.gz’

go1.17.5.linux-amd64.tar.g 100%[========================================>] 128.56M   137MB/s    in 0.9s

2021-12-23 12:56:05 (137 MB/s) - ‘go1.17.5.linux-amd64.tar.gz’ saved [134808667/134808667]

Install Go

Now that we have the file we can ‘install’ it by unzipping it to our /usr/local folder:

$ tar -C /usr/local -xzf go1.17.5.linux-amd64.tar.gz

Check that has worked ok by listing the contents of the newly created Go directory:

$ ls /usr/local/go
AUTHORS          CONTRIBUTORS  PATENTS    SECURITY.md  api  codereview.cfg  lib   pkg  test
CONTRIBUTING.md  LICENSE       README.md  VERSION      bin  doc             misc  src

Great! All good so far.

Adding Go to $Path

Next we need to add the Go binary to our path so that we can run Go easily. To do so, as your non-root user, add the following two lines to your .bashrc file using your favourite text editor:

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

Once you have updated your .bashrc, reload your shell:

$ exec $SHELL

With that done it should be possible to run the Go command. Test it by running the go version command:

$ go version
go version go1.17.5 linux/amd64

Looks good!

Testing the Go Install on Ubuntu

Finally, we can test Go is working correctly by creating and running a simple hello world program. Open your favourite text editor again, and create a file with the following contents:

package main
import "fmt"
func main() {
    fmt.Println("Hello, World! I've Just Installed Go On Ubuntu!")
}

Save the file as hello.go. Once done, run the program:

$ go run hello.go
Hello, World! Just testing my Upgraded Go!

And we’re done! In this article you have learned how to download and install Go on Ubuntu. We showed how to add Go to $PATH so that it can be called easily, and finally tested the Go installation with a simple hello-world program.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More