GoLang: Installing the Go Programming language on Ubuntu 16.04

Update: For the latest version of this article compatible with Ubuntu 22.04 and Go 1.19, see my newer article here.

The Go programming language has gotten considerable momentum, and the fact that it compiles down to machine code has made it popular in containers like Docker where a single executable binary fits the execution model perfectly.

This article will detail installation on Ubuntu 16.04 with the standard 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

$ wget https://storage.googleapis.com/golang/go1.13.6.linux-amd64.tar.gz

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

$ /usr/local/go/bin/go version
go version go1.13.6 linux/amd64

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=$PATH:/usr/local/go/bin

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
$ go version
go version go1.13.6 linux/amd64

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:

$ go build
$ ./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