Using a Different Version? Choose a different version or system.

Introduction

Go, often referred to as golang is a modern open-source programming language created by Google that allows you to build reliable and efficient applications. Go is a compiled language, which means you need to compile the source code to create an executable file that is used to run the software.

Many popular applications, such as Kubernetes, Docker, Prometheus, and Terraform, are written in Go.

This tutorial explains how to download and install Go on Ubuntu 20.04.

So let's take a look at how it all works?

Prerequisites

  • Ubuntu 20.04 or Ubuntu 20.10
  • 1024MB or above Ram.
  • 20GB Disk Space.
  • 1 vCPU or above CPU.
  • Internet connection to download Go
  • root privileges

Step 1 - Downloading the Go

At the time of writing this article, the latest stable version of Go is version 1.16.x. Before downloading the tarball, visit the official Go downloads page and check if there is a new version available.

Run the following command as a user with sudo privileges to download and extract the Go binary archive in the /usr/local directory:

wget -c https://golang.org/dl/go1.16.3.linux-amd64.tar.gz -O - | sudo tar -xz -C /usr/local

Step 2 - Adjusting the Path Variable

By adding the location of the Go directory to the $PATH environment variable, the system will know where to find the Go executable binaries.

This can be done by appending the following line either to the /etc/profile file (for a system-wide installation) or the $HOME/.profile file (for a current user installation):

##

╭─sinesio@linux ╰─➤ ~/.profile

##

╭─sinesio@linux ╰─➤ export PATH=$PATH:/usr/local/go/bin

Save the file, and load the new PATH environment variable into the current shell session:

##

╭─sinesio@linux ╰─➤ source ~/.profile

Step 3 - Verifying the Go Installation

Verify the installation by printing the Go version:

##

╭─sinesio@linux ╰─➤ go version

go version go1.16.3 linux/amd64

Step 4 - The output should look something like this:

To test the Go installation, we will create a workspace and build a simple program that prints the classic “hello world” message.

By default, the GOPATH variable, which specifies the location of the workspace is set to

##

╭─sinesio@linux ╰─➤ $HOME/go.

To create the workspace directory type:

##

╭─sinesio@linux ╰─➤ mkdir ~/go

Inside the workspace create a new directory src/hello:

##

╭─sinesio@linux ╰─➤ mkdir -p ~/go/src/hello

And in that directory create a file named hello.go:

##

╭─sinesio@linux ╰─➤ cat ~/go/src/hello/hello.go

package main

import "fmt"

func main() { fmt.Printf("Hello, World\n") }

To learn more about Go workspace directory hierarchy, visit the Go Documentation page.

Navigate** to the ~/go/src/hello directory and run go build to build the program:

##

╭─sinesio@linux ╰─➤ cd ~/go/src/hello

##

╭─sinesio@linux ╰─➤ go build

The command above will build an executable file named hello.

Step 5 - You can run the executable by simply executing the command below:

##

╭─sinesio@linux ╰─➤ ./hello

The output should look something like this:

Hello, World

Conclusion

Now that you have downloaded and installed Go on your Ubuntu system, you can start developing your Go projects.

If you hit a problem or have feedback, leave a comment below.

Read more about: Development