How to Install The Go Programming Language in Ubuntu 20.04

Tutorial Jan 6, 2021

Go is a modern programming language designed and developed at Google. The syntax used for writing Go programs is very much similar to C. And like C, Go is also compiled and statically typed. Some of the noticeable modern applications made with Go are Docker, Hugo, Kubernetes, OpenShift, Caddy and many more.

This is a tutorial on how to download and install the latest stable version of Go on an Ubuntu 20.04 machine. At the time of writing this, the stable version of Go is 1.15.6.

Note: It is recommended that you install Go with a non-root user with sudo privileges. To learn how to create a sudo user in Ubuntu 20.04, click here.

INSTALLATION

The first step is to download the latest stable version of Go from the official downloads page. We will be using wget to do this,

wget https://golang.org/dl/go1.15.6.linux-amd64.tar.gz

Next, we extract the tarball we just downloaded, move the extracted go directory to /usr/local/ and recursively change /usr/local/go’s owner and group to root.

tar xvf go1.15.6.linux-amd64.tar.gz
sudo mv go /usr/local
sudo chown -R root:root /usr/local/go

UPDATE ~/.profile

Now we need to set the $GOPATH variable. Then add both $GOPATH/bin and /usr/local/go/bin to the $PATH variable.

Open up ~/.profile in an editor like nano or vim.

vim ~/.profile

Add these lines to the end of the file,

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

Next, load the updated ~/.profile with the source command

source ~/.profile

Note: A log-off and log back in will be required to make the above action persist onto newly opened terminal windows.

This concludes the installation of Go. We can run the following command to confirm that the Go command is working.

go version
go version go1.15.6 linux/amd64

HELLO WORLD

To make sure that Go is working as expected and to complete this guide, let's go ahead and create the famous "hello world" application.

The $GOPATH ($HOME/go) environment variable we set a while back is our Go workspace directory. Our source files go inside $GOPATH/src, where each project will be in their own directory. So, let's go ahead and create the directory needed for our "hello world" project.

mkdir -p $GOPATH/src/hello

Use nano, vim or any other text editor to create the hello.go source file,

vim $GOPATH/src/hello/hello.go

Type in or copy-paste the following into the file and save it,

package main

import "fmt"

func main() {
    fmt.Println("Hello, world!")
}

Next, we compile and install our "hello world" project with Go's install command.

go install hello

If the above command finishes without any errors, we can run our program with the following command,

hello
Hello, world!

That is all! We have a working installation of Go.

Tags

Lime

Simple, like quantum physics.

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.