How to Install Go (Golang) Compiler on Ubuntu 20.04

how to install go (golang) compiler on ubuntu 20.04

Go, also called Golang, is an open-source programming language developed by Google. It is cross-platform and can be installed on Linux, Windows, and macOS. It is written in C programming language C and was developed to create dependable and efficient software. It is used by many organizations such as Kubernetes, Docker, MongoDB, Soundcloud, Netflix, Uber, Prometheus, Terraform, etc.

install go (golang) compiler on ubuntu 20.04

In this tutorial, we will show you how to install Go on a Ubuntu 20 VPS. Also, we will show you how to set up the Go environment variable and create a Go program.

Prerequisites

Step 1: Log in to the Server & Update the Server OS Packages

First, log in to your Ubuntu 20.04 server via SSH as the root user:

ssh root@IP_Address -p Port_number

You must replace ‘IP_Address’ and ‘Port_number’ with your server’s respective IP address and SSH port number. Also, you should replace ‘root’ with the username of the admin account if needed.

Before we begin the installation, we should ensure that all the Ubuntu OS packages installed on the server are updated. You can do this by running the following commands:

apt-get update -y
apt-get upgrade -y

Step 2: Download and Install Go

After this system update, you will need to download the latest version of the Go tarball from the Go official website.

At the time of writing this article, the latest stable version of Go is version 1.16.7. But before downloading, visit the official Go downloads page and check if there is a new version available.

To download the Go tarball, run the following command:

wget https://golang.org/dl/go1.16.7.linux-amd64.tar.gz

Once the tarball is downloaded, verify the tarball checksum with the following command:

sha256sum go1.16.7.linux-amd64.tar.gz

You should see an output that looks similar to the one below:

7fe7a73f55ba3e2285da36f8b085e5c0159e9564ef5f63ee0ed6b818ade8ef04 go1.16.7.linux-amd64.tar.gz

Compare the hash value from the above output to the checksum value on the Go download page. If they match, that means the file’s integrity is validated, and you can proceed with the installation.

Next, extract the downloaded file to the recommended /usr/local directory with the following command:

tar -C /usr/local -xvzf go1.16.7.linux-amd64.tar.gz

That should extract Go and saved it in the /usr/local directory.

Step 3: Path Variable for Go

Next, we will need to add the path of the Go directory to the $PATH environment variable to execute Go like any other command, no matter where you are in the filesystem.

You can set the environment variable globally by creating a file called go.sh in the /etc/profile.d directory.

nano /etc/profile.d/go.sh

Add the following line:

export PATH=$PATH:/usr/local/go/bin

Save and close the file when you have finished.

If you want to set the Go path environment variable for a specific user, you will need to define Go environment variables in your user’s .bash_profile file.

nano ~/.bash_profile

Add the following lines:

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

Save and close the file. Then, run the source command to reload the updated profiles:

source /etc/profile.d/go.sh
source ~/.bash_profile

Next, you can use the following command to check the Go version:

go version

You should see the following output:

go version go1.16.7 linux/amd64

Step 4: Create your First Go Project

Now to check that everything is OK, we will create a sample program in the Go language. First, create a new directory for the Go workspace with the following command:

mkdir $HOME/myproject

Next, create a new src/test directory inside $HOME/project with the following command:

mkdir -p $HOME/myproject/src/hello

Next, create a simple program (hello.go) with the following command:

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

GET YOUR VPS
nano $HOME/myproject/src/hello/hello.go

Add the following content:

package main
import "fmt"
func main() {
    fmt.Printf("This is my first Go Program\n")
}

Save and close the file. Then, compile the program with the following command:

cd $HOME/myproject/src/hello/
go mod init
go build

The above command will generate an executable named hello. You can now run the program with the following command:

hello

The output should be similar to the one below:

This is my first Go Program

That’s it! Now you can use Go to code your programs for any platform.


Of course, you don’t need to do any of this if you use one of our managed Linux VPS Hosting services, in which case you can ask our expert Linux admins to install Go on Ubuntu 20 for you. They are available 24/7 and will take care of your request immediately.

installation of go (golang) compiler on ubuntu 20.04

PS. If you liked this post on how to install Go on Ubuntu 20, please share it with your friends on the social networks using the buttons below, or leave a reply in the comments section. Thank you.

Leave a Comment