Table of Contents
In this article, I will take you through the steps to install latest version of GO on Ubuntu 20.04 LTS (Focal Fossa). Go is a free and open source compiled programming language. It is known as one of the fastest growing programming language due to its most popular concurrency features. It was developed by Google in 2009 and since then it has grown stronger with a vibrant community of developers supporting it from around the world. It is also known as a multipurpose programming language specifically designed to build fast, scalable applications.
Go programs can be run through go utility which can be easily installed on a Linux, Windows or Mac systems. go command has number of subcommands. The simplest one is run. Here we will see the steps to install latest version of GO on Ubuntu 20.04 LTS (Focal Fossa).
How to Install latest version of GO on Ubuntu 20.04 LTS (Focal Fossa)
Also Read: An Introduction to GO Programming Language for Beginners
Step 1: Prerequisites
a) You should have a running Ubuntu 20.04 LTS
Server.
b) You should have sudo
or root
access to run privileged commands.
c) You should have apt
or apt-get
utility available in your System.
d) You should have a file transfer utility like wget
or curl
installed in your System.
Step 2: Update Your Server
In the first step, you need to sync the installed packages with the latest available versions from the default Ubuntu repo by using sudo apt update
command as shown below.
cyberithub@ubuntu:~$ sudo apt update
Hit:1 https://download.docker.com/linux/ubuntu focal InRelease
Hit:2 https://dl.google.com/linux/chrome/deb stable InRelease
Hit:3 http://in.archive.ubuntu.com/ubuntu focal InRelease
Get:4 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Get:5 http://in.archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Get:6 http://in.archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB]
Get:7 http://security.ubuntu.com/ubuntu focal-security/main amd64 DEP-11 Metadata [40.8 kB]
Get:8 http://security.ubuntu.com/ubuntu focal-security/universe amd64 DEP-11 Metadata [77.4 kB]
Get:9 http://in.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [2,072 kB]
Get:10 http://security.ubuntu.com/ubuntu focal-security/multiverse amd64 DEP-11 Metadata [2,464 B]
Get:11 http://in.archive.ubuntu.com/ubuntu focal-updates/main amd64 DEP-11 Metadata [277 kB]
Get:12 http://in.archive.ubuntu.com/ubuntu focal-updates/universe amd64 DEP-11 Metadata [392 kB]
Get:13 http://in.archive.ubuntu.com/ubuntu focal-updates/multiverse amd64 DEP-11 Metadata [944 B]
Get:14 http://in.archive.ubuntu.com/ubuntu focal-backports/main amd64 DEP-11 Metadata [7,968 B]
Get:15 http://in.archive.ubuntu.com/ubuntu focal-backports/universe amd64 DEP-11 Metadata [30.6 kB]
Step 3: Download GO
You can go to official website and download the latest GO tarball using any of the file transfer utility like wget
or curl
. Here we are using wget
utility to download the latest package as shown below.
cyberithub@ubuntu:~$ wget https://go.dev/dl/go1.19.2.linux-amd64.tar.gz --2022-10-12 22:56:27-- https://go.dev/dl/go1.19.2.linux-amd64.tar.gz Resolving go.dev (go.dev)... 216.239.36.21, 216.239.38.21, 216.239.32.21, ... Connecting to go.dev (go.dev)|216.239.36.21|:443... connected. HTTP request sent, awaiting response... 302 Found Location: https://dl.google.com/go/go1.19.2.linux-amd64.tar.gz [following] --2022-10-12 22:56:27-- https://dl.google.com/go/go1.19.2.linux-amd64.tar.gz Resolving dl.google.com (dl.google.com)... 142.250.76.46, 2404:6800:4007:808::200e Connecting to dl.google.com (dl.google.com)|142.250.76.46|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 148883574 (142M) [application/x-gzip] Saving to: ‘go1.19.2.linux-amd64.tar.gz’ go1.19.2.linux-amd64.tar.gz 100%[============================================================================>] 141.99M 7.89MB/s in 20s 2022-10-12 22:56:48 (6.93 MB/s) - ‘go1.19.2.linux-amd64.tar.gz’ saved [148883574/148883574]
Step 4: Verify the File
Once the file is downloaded, you can verify the integrity of the file by checking its sha256 checksum using sha256sum go1.19.2.linux-amd64.tar.gz
command as shown below.
cyberithub@ubuntu:~$ sha256sum go1.19.2.linux-amd64.tar.gz
5e8c5a74fe6470dd7e055a461acda8bb4050ead8c2df70f227e3ff7d8eb7eeb6 go1.19.2.linux-amd64.tar.gz
Step 5: Extract Tarball
Next step is to extract the tarball under /usr/local
directory using sudo tar -C /usr/local -xzf go1.19.2.linux-amd64.tar.gz
command as shown below.
cyberithub@ubuntu:~$ sudo tar -C /usr/local -xzf go1.19.2.linux-amd64.tar.gz
Step 6: Set GO PATH
You need to set the binary path under global PATH
environment variable using below export
command.
cyberithub@ubuntu:~$ export PATH=$PATH:/usr/local/go/bin cyberithub@ubuntu:~$ echo $PATH /home/cyberithub/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/go/bin
However to set the GO path permanently, you need to add the entry in ~/.profile
file by editing it using sudo nano ~/.profile
command and then adding the value at the end as shown below.
cyberithub@ubuntu:~$ sudo nano ~/.profile ............................... export PATH=$PATH:/usr/local/go/bin
To reflect the changes, you can either restart your system or you can just run source ~/.profile
command as shown below.
cyberithub@ubuntu:~$ source ~/.profile
Step 7: Check Version
After successful installation, you can test it by using go version
command as shown below.
cyberithub@ubuntu:~$ go version go version go1.19.2 linux/amd64
Step 8: Write Your First Program
Now that go is successfully installed in your System. It is time to write our first program. Let's create a directory called example
.
cyberithub@ubuntu:~$ mkdir example
Switch to that directory using cd example
command.
cyberithub@ubuntu:~$ cd example
To enable dependency tracking for your code, you need to create a go.mod
file by running go mod init example
command as shown below.
cyberithub@ubuntu:~/example$ go mod init example go: creating new go.mod: module example
Then create a file called example.go
using nano
editor with below contents in it. This is a simple program to display Hello, World !
on the output.
cyberithub@ubuntu:~/example$ nano example.go package main import "fmt" func main() { fmt.Println("Hello, World!") }
Press Ctrl+X
to save and exit the program. Then run your program by using go run .
command as shown below. You will see Hello, World!
on the output as shown below.
cyberithub@ubuntu:~/example$ go run . Hello, World!