GoLang: Installing the Go Programming language on Ubuntu

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

This article will detail installation on Ubuntu 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

# grab latest version from download page
latest_version=$(curl -s https://go.dev/dl/ | grep -P 'download downloadBox' | grep linux-amd64 | grep -Po '/dl/go\K(.*)(?=\.linux-amd64.tar.gz)')
echo "latest golang version: $latest_version"

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

# remove any older version, then extract latest
sudo rm -fr /usr/local/go
sudo tar -C /usr/local -xzf go${latest_version}.linux-amd64.tar.gz

# validate
/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=$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 is set to: $GOPATH"
mkdir -p $GOPATH

# validate
go version

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:

# build binary
go build hello.go

# run binary
./hello

 

REFERENCES

Golang install

Golang download

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