GoLang: Installing the Go Programming language on Ubuntu 20.04

See the newer version of this article, “Installing the Go Programming language on Ubuntu 22.04“.

The Go programming language consistently ranks as one of the most popular languages in developer surveys.  In fact, Kubernetes as well as most of the CNF projects are written in Go.  And it compiles down to machine code, which has made it popular in containers like Docker where a single executable binary fits the execution model perfectly.

This article will detail the installation on Ubuntu 20.04 with a simple hello world validation.

Installation

First, download the latest 64 bit linux package from the download page.  Unzip to the standard location, and run a quick check of the version.

cd /tmp

# find latest version using curl or by manually looking at download page
go_latest=$(curl -s https://go.googlesource.com/go | grep -Po "<li class=\"RefList-item\"><a href=\"/go/\+/refs/tags/go\d\.\d*\">go\K(\d\.\d*)</a></li>" | cut -d'<' -f1)
echo "Latest go release: $go_latest"

# download archive
wget https://storage.googleapis.com/golang/go${go_latest}.linux-amd64.tar.gz

# remove any older versions, extract new version
sudo rm -rf /usr/local/go
sudo tar -C /usr/local -xzf go${go_latest}.linux-amd64.tar.gz

# check version
/usr/local/go/bin/go version

Environment

Before going further, make sure to set the GOPATH and PATH variables to your environment.

On Ubuntu, append the following to ~/.profile:

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

Then create the subdirectories and source the profile so that your current environment is updated.  Now you should be able to run go without needing to specify the path.

source ~/.profile

echo $GOPATH
mkdir -p $GOPATH

Hello World

Let’s start by going into the $GOPATH, and creating a source file.

mkdir -p $GOPATH/src/hello; cd $_

Create a file named $GOPATH/src/hello/hello.go

package main

import "fmt"

func main() {
    fmt.Printf("hello, world\n")
}

And then compile and run:

# compile
go build hello.go

# run executable
$ ./hello
hello, world

 

REFERENCES

https://golang.org/dl/

https://golang.org/doc/install

https://www.digitalocean.com/community/tutorials/how-to-install-go-1-6-on-ubuntu-16-04

https://tecadmin.net/install-go-on-ubuntu/#

NOTES

For statically linked

export CGO_ENABLED=0

On Windows

installer creates GOHOME and appends PATH

GOROOT=c:\go

PATH=$PATH;c:\go\bin

User should set new system var GOPATH=%userprofile%\go