How to Install Go on Ubuntu 20.04

Posted on by Margaret Fitzgerald | Updated:
Reading Time: 3 minutes

What is Go?

Go is a statically typed and compiled programming language developed by Google and is often referred to as Golang due to its domain name. It was created by Rob Pike, Ken Thompson, and Robert Griesemer in 2007 to remove obstacles impacting compilation speeds, ease of programming, and safety. In today’s article, we will discuss the installation requirements of Go and how to install it on Ubuntu 20.04.

Install Go on Ubuntu 20.04

Requirements

  • Server: 4 GB RAM and 2 kernels.
  • Operating System: Ubuntu 20.04.
  • Root User: We execute all commands as the root user. If you run commands as a regular user, do not forget to implement the sudo command.

Installation Methods

There are two ways to install Go: using the Ubuntu repository or the official Golang suite.

Ubuntu Repository Installation

If having a specific version or environment variables is not crucial, the fastest and easiest option is to install from the official repository. Before installing Go, it is best to update the system first by using apt update && apt upgrade -y.

root@host:~#  apt update && apt upgrade -y
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following package was automatically installed and is no longer required:
  gradle-6.8.3
Use 'apt autoremove' to remove it.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
The following package was automatically installed and is no longer required:
  gradle-6.8.3
Use 'apt autoremove' to remove it.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
root@host:~#

Once the system is updated, you can install Go by using apt install golang -y.

root@host:~# apt install golang -y
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Setting up golang-1.13-src (1.13.8-1ubuntu1) ...
Setting up golang-1.13-race-detector-runtime (0.0+svn332029-0ubuntu2) ...
Setting up libstdc++-9-dev:amd64 (9.3.0-17ubuntu1~20.04) ...
Setting up g++-9 (9.3.0-17ubuntu1~20.04) ...
Setting up golang-1.13-go (1.13.8-1ubuntu1) ...
Setting up g++ (4:9.3.0-1ubuntu2) ...
update-alternatives: using /usr/bin/g++ to provide /usr/bin/c++ (c++) in auto mode
Setting up golang-src (2:1.13~1ubuntu2) ...
Setting up golang-race-detector-runtime (2:1.13~1ubuntu2) ...
Setting up golang-go (2:1.13~1ubuntu2) ...
Setting up golang-1.13-doc (1.13.8-1ubuntu1) ...
Setting up golang-1.13 (1.13.8-1ubuntu1) ...
Setting up golang-doc (2:1.13~1ubuntu2) ...
Setting up golang (2:1.13~1ubuntu2) ...
Processing triggers for man-db (2.9.1-1) ...
root@host:~#

Always verify if the installation was successful.

root@host:~# go version
go version go1.13.8 linux/amd64
root@host:~#

Since the current version of Go is go1.16.3, you can see the official repository did not install the latest version.

Official Golang Suite Installation

Now, we will review how to install Go from the official site. You can choose to install either the latest or a specific version of Go. 

Download Go by selecting your preferred operating system. As we just stated, the latest stable version is go1.16.3, but you could also download go1.15.11 since it is still stable.

Update the packages if you have not already done so, and then download the archive.

root@host:~# wget  https://golang.org/dl/go1.16.3.linux-amd64.tar.gz
--2021-04-19 21:17:26--  https://golang.org/dl/go1.16.3.linux-amd64.tar.gz
Resolving golang.org (golang.org)... 172.217.18.113, 2a00:1450:4001:82b::2011
Connecting to golang.org (golang.org)|172.217.18.113|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://dl.google.com/go/go1.16.3.linux-amd64.tar.gz [following]
--2021-04-19 21:17:26--  https://dl.google.com/go/go1.16.3.linux-amd64.tar.gz
Resolving dl.google.com (dl.google.com)... 142.250.185.238, 2a00:1450:4001:813::200e
Connecting to dl.google.com (dl.google.com)|142.250.185.238|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 129021323 (123M) [application/octet-stream]
Saving to: 'go1.16.3.linux-amd64.tar.gz'

go1.16.3.linux-amd64 100%[===================>] 123,04M  10,7MB/s    in 11s     

2021-04-19 21:17:38 (10,8 MB/s) - 'go1.16.3.linux-amd64.tar.gz' saved [129021323/129021323]

root@host:~#

Make sure to verify that everything has downloaded correctly by using the sha256sum command. We can verify if the returned key value in the code matches the SHA256 Checksum value on the Golang download page.

root@host:~# sha256sum go1.16.3.linux-amd64.tar.gz 
951a3c7c6ce4e56ad883f97d9db74d3d6d80d5fec77455c6ada6c1f7ac4776d2  go1.16.3.linux-amd64.tar.gz
root@host:~#

Now, extract the file.

root@host:~# tar xvf go1.16.3.linux-amd64.tar.gz
root@host:~#

Next, set up the paths in your profile.

root@host:~# nano ~/.profile
root@host:~#

Add the following values and then save.

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

Apply the changes to the profile.

root@host:~# source ~/.profile
root@host:~#

Verify that Go is installed and working properly.

root@host:~# go version
go version go1.16.3 linux/amd64
root@host:~#

Using Go

Let's check everything on a real mini-project. All created packages must be in the folder designated in the GOPATH environment variable, which is what we will do now.

Create a folder using the command below.

root@host:~# mkdir -p ~/work/go/
root@host:~#

Now, navigate to the folder you created.

root@host:~# cd ~/work/go/
root@host:~#

Let's create our app file using the nano editor.

root@host:~/work/go# nano helloworld.go

Here is an explanation of what each part of the code below accomplishes:

  • package main: Declare a package (package is a way to group functions consisting of all files in one directory).
  • import "fmt": Import the fmt package, which contains functions for formatting text.
  • func main (): Run the main function.
  • fmt.Printf ("Hello, Liquid Web!)\n"): Print a message.
package main

import "fmt"

func main() {
    fmt.Printf("Hello, Liquid Web!)\n")
}

Build the program using the build command.

root @ host: ~ / work / go # go build helloworld.go
root @ host: ~ / work / go #

Run the app with the following command.

root @ host: ~ / work / go # ./helloworld
Hello, Liquid Web!)
root @ host: ~ / work / go #

Conclusion

In this article, we covered how to install and test the Go programming language on Ubuntu 20.04. Go removes the obstacles impacting compilation speeds, ease of programming, and safety.

Discover the wide range of hosting solutions and add-ons that Liquid Web offers to meet your needs.

About the Author: Margaret Fitzgerald

Margaret Fitzgerald previously wrote for Liquid Web.

Have Some Questions?

Our Sales and Support teams are available 24 hours by phone or e-mail to assist.

1.800.580.4985
1.517.322.0434

Latest Articles

How to Use Material-UI in React

Read Article

Cloning an Existing Virtual Machine with VMware

Read Article

Five Steps to Create a Robots.txt File for Your Website

Read Article

Premium Business Email Pricing FAQ

Read Article

Microsoft Exchange Server Security Update

Read Article