Golang: How to install Go on Ubuntu Server

Installing Go on Ubuntu

Recently I wanted to try a project on my Ubuntu server, but it was built with Golang language, so I had to install Go on my server to get started. In this post, we will first download the latest release, install Go, and finally verify if it was installed successfully.

Note: The Go was installed on Ubuntu server 22.04, but it should work on other Ubuntu releases too.

Step 1 - Downloading Go

First, we need to copy a download link for the Linux release from the offical Go download page. The featured downloaded version should be suitable for most cases as this will be the most recent and stable release.

Official Go Download Page

Right-click and copy the Linux download link. The copied URL will be in the format of a .tar.gz file.

Sometimes, the Go project will require a specific version of Go to be installed. In that case, scroll down to the "Archieved versions" and download the necessary Go release.

Official Go Download Page - Archieved versions

Next, we open the terminal and download the file by entering the wget command and pasting the copied download link.

wget [download_link]

As of April 2024, the latest stable release is 22.2 and the file would be go1.22.2.linux-amd64.tar.gz, so we would use:

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

Step 2 - Removing any old version and installing the downloaded version

Now that we have downloaded the file, we run the following command that does two things. It first deletes any previous version of Go from the system and then installs the downloaded version.

rm -rf /usr/local/go && tar -C /usr/local -xzf [downloaded_file]

With version 22.2, the file is go1.22.2.linux-amd64.tar.gz, so we use:

rm -rf /usr/local/go && tar -C /usr/local -xzf go1.22.2.linux-amd64.tar.gz

The Go should now be installed in /usr/local/go directory on your Linux system. We can confirm that it was installed by listing files and directories in that location:

ls /usr/local/go

Step 3 - Adding go bin directory to PATH and verifying installation

Finally, we need to add the location of Go binaries to the system's PATH environment variable. This allows us to run Go commands from any location in the system.

We do this by appending /usr/local/go/bin to the end of the PATH environment variable:

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

To ensure that Go was successfully installed, we use the following command:

go version

Fixing Command 'go' not found error

Even though I installed Go successfully, the next time I logged into the terminal, I received the "Command 'go' not found" error. This usually indicates that the directory where Go is installed is not persistent in the PATH environment variable.

We can fix that by adding the command we ran in the previous step into one of the shell configuration files:

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

In my case, I added it at the end of the .bashrc file with nano text editor.

Adding Go path to .bashrc file with nano text editor

Close/reopen a terminal and go command should now work.

Summary

In this post, we installed the Golang language in the Ubuntu server. The installation itself is not that difficult, it involves downloading, extracting the necessary files, and setting the PATH variable. After that, we are set to use Go for our programming projects on Ubuntu.

Tags:

Write a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.