Install Go (Golang) on Ubuntu 22.04/20.04/18.04

Posted on 251 views

In this guide, I’ll walk you through different ways you can install Go on Ubuntu 22.04/20.04/18.04 server. Go is an open source programming language with a novel type system to enable flexible and modular program construction, and make programmers more productive.

Go uses concurrency mechanisms which makes it easy to write programs that get the most out of multicore and networked machines. Go has been known to be efficient, clean, expressive and concise.

It is a fast, statically typed, compiled language that feels like a dynamically typed, interpreted language.

Linux Mint: How to Install Go (Golang) on Linux Mint

For RHEL 8 / CentOS 8: How to Install Go on RHEL 8

Fedora Linux: Install Go on Fedora

Method 1: Install Go (Golang) on Ubuntu from PPA Repository

Install software-properties-common package:

sudo apt update
sudo apt install software-properties-common 

For Ubuntu server, add Golang backports PPA repository and install Go on Ubuntu22.04/20.04/18.04:

sudo add-apt-repository ppa:longsleep/golang-backports 
sudo apt update
sudo apt install golang-go

Proceed with the installation:

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  golang-1.17-go golang-1.17-src golang-src
Suggested packages:
  bzr | brz mercurial subversion
The following NEW packages will be installed:
  golang-1.17-go golang-1.17-src golang-go golang-src
0 upgraded, 4 newly installed, 0 to remove and 58 not upgraded.
Need to get 71.8 MB of archives.
After this operation, 423 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

Set $GOPATH

mkdir -p ~/go/bin,pkg,src
echo 'export GOPATH="$HOME/go"' >> ~/.bashrc
echo 'export PATH="$PATH:$GOPATH//://bin:/bin"' >> ~/.bashrc
source ~/.bashrc

Method 2: Install Go using Golang installer

The second method is the installation of Go from provided Golang installer.

First install wget

sudo apt install wget

Download installer with wget to your local system.

wget https://storage.googleapis.com/golang/getgo/installer_linux

After downloading the file, make it executable:

chmod +x ./installer_linux

And lastly run the installer from your current terminal shell.

$ ./installer_linux 
Welcome to the Go installer!
Downloading Go version go1.18.1 to /home/jmutai/.go
This may take a bit of time…
Downloaded!
Setting up GOPATH
GOPATH has been set up!

One more thing! Run source /home/jmutai/.bash_profile to persist the new environment variables to your current session, or open a new shell prompt.

Source the ~/.bash_profile to start using Go environment variables in your current session.

$ source ~/.bash_profile
$ go version
go version go1.18.1 linux/amd64

Method 3: Manually Install Go onUbuntu22.04/20.04/18.04

The second method involves downloading Go archive and installing it to the system. You may need to check Go releases page for the latest version.

Ensure you have wget installed

sudo apt install wget

Then download Go

VER=1.18.1
wget https://dl.google.com/go/go$VER.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go$VER.linux-amd64.tar.gz
rm go$VER.linux-amd64.tar.gz

Set Go PATH Variable

Run the commands below to set your set $GOPATH

mkdir -p ~/go/bin,pkg,src
echo 'export GOPATH="$HOME/go"' >> ~/.bashrc
echo 'export PATH="$PATH:$GOPATH//://bin:/bin"' >> ~/.bashrc

Testing Go installation on Ubuntu22.04/20.04/18.04

Create source directory:

mkdir -p ~/go/src/test
vim ~/go/src/test/test.go

Add print Hello Gophers message

package main
import "fmt"
func main() 
    fmt.Printf("Hello, Gophers\n")

Build test.go file

cd ~/go/src/test
go build

Run binary to test

$ ./test
Hello, Gophers

Thanks for using our guide to install latest Go on Ubuntu22.04/20.04/18.04

Gravatar Image
A systems engineer with excellent skills in systems administration, cloud computing, systems deployment, virtualization, containers, and a certified ethical hacker.