How to Install Go (Golang) on Ubuntu Linux Step-by-Step

In this guide, we are going to cover how you can install Golang Go on Ubuntu Linux step-by-step. For demonstration purposes, we will use Ubuntu 22.04 LTS as our Linux environment.

Go is also referred to as Golang, it is a free and open-source programming language designed at Google by Robert Griesemer, Rob Pike, and Ken Thompson. It is an apple to many a developer’s eye due to its simplicity, efficiency, and concurrency. Its concurrent nature implies means its ability to run multiple tasks at the same time.

It is mostly used for backend purposes such as server-side programming. It features majorly in the game and cloud-native development,  development of command-line tools, data science, and many more use cases.

There are three main ways of installing Go in Linux:

  1. Installing from the Official Binary package
  2. Installing using APT package manager ( Debian & Ubuntu distros)
  3. Installing from Snap packages

Let us go through each of these installation methods.

Install Go from the Official Binary Package

This is the most preferred installation method as it provides the latest version of Golang Go and cuts across all Linux distributions. To achieve this, follow the steps outlined.

Step 1:  Download the Go

The first step in installing Go is to update the system. So log in to your server and update the local package index as shown.

$ sudo apt update

Step 2:  Download the Go Binary Package

The next step is to download the installation file which is a tarball file. To download it, head over to the official Go download page and download the 64-bit tarball installation file ( amd64.tar.gz )

On the command line, you can download the latest tarball file using the wget command. At the time of writing this guide, the latest version of Go is Go 1.20.1. This is likely to change by the time you are reading this guide, so ensure to replace the version number accordingly.

$ wget https://go.dev/dl/go1.20.1.linux-amd64.tar.gz

Download-go-wget-command-linux

Step 3:  Unzip the tarball file & move it to /usr/local directory

Once the download is complete, unzip the tarball to the /usr/local/ directory which is the primary directory for executable files.

$ sudo tar -C /usr/local -xzf go1.20.1.linux-amd64.tar.gz

The -C option uncompresses the contents of the tarball file to the /usr/local directory inside the go folder. To confirm this, list the contents of the /usr/local/  directory using the ls command.

$ ls /usr/local/go

list-user-local-go-ubuntu-linux

Step 4: Add the Golang binary to the $PATH environment variable

The next step is to add Go to the $PATH environment variable.

Open up your .bashrc or .bash_profile file.

$ nano ~/.bash_profile

Paste the following line.

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

Save the changes and exit the file.

Add-Go-Command-Path-Linux

Next, reload the  .bashrc or .bash_profile file using the source command as shown.

$ source ~/.bash_profile

Now Go is successfully installed.

Step 5: Verify Go Version

To check if Go is installed and its version, run the command:

$ go version

The following output shows that go has successfully been installed.

Go-Command-Version-Check-Linux

Install Go from the APT Package Manager

If you are running Debian / Ubuntu or any of their derivatives and don’t mind not installing the latest version of Go, then using the APT package manager will do just fine.

To get started, update the package lists as shown.

$ sudo apt update

If you are curious enough, you can search for the availability of the golang-go package from Debian / Ubuntu repositories as shown.

$ apt search golang-go

Apt-Search-Golang-go-Ubuntu

To install Golang Go using apt command, run

$ sudo apt install golang-go

Install-golang-go-apt-command-ubuntu-linux

Once the installation is complete, verify that Go is installed as shown.

$ go version

From the output, you can see that Go version 1.18.1 has been installed. Notably, this is not the latest version at the time of writing this guide.

Go-Version-Chech-Post-Installation-Apt

Install Go from Snap Package

Installing Go from snap is as easy as they come. First, you need to ensure that snap is already enabled on your system. Next, install Go from snap as follows.

$ sudo snap install go --classic

Once the installation is complete, verify that Go has successfully been installed as shown

$ /snap/bin/go version

Install-go-with-snap-ubuntu-linux

Testing Go Installation

In this section, we will create a simple Go program and test it to see if our installation works.

We will create a separate directory for our project as follows.

$ mkdir -p ~/go_projects/test

Next, we will navigate into the directory.

$ cd ~/go_projects/test

We will create a simple program called greetings.go that prints out a simple message on the terminal.

$ nano greetings.go

Copy and paste the following lines of code into the file.

package main
import "fmt"
func main() {
    fmt.Printf("Congratulations! Go has successfully been installed on your system\n")
}

Save the changes and exit. Then run the program as follows

$ go run greetings.go

You will get the following output. This confirms that the installation of Golang Go is working as expected.

Run-Go-Program-Ubuntu-Linux

Conclusion

You are now all set! Go has successfully been installed. In this guide, we have demonstrated how to install Go (Golang ) programming language on Linux.

Leave a Comment

Your email address will not be published. Required fields are marked *