How to Install Go on Linux

In this post, we will see how to install Golang in Linux (Ubuntu). Golang is a multipurpose programming language, that can be used to create system software and much more. It comes with memory safety, garbage collection, and a very accessible concurrent programming system. Installing Go in Linux is not hard. We will install it in Ubuntu using the command line.

Update Ubuntu System

It’s recommended to keep the Ubuntu system updated. We will upgrade our Ubuntu server with the latest changes using the below commands.

# sudo apt update
# sudo apt upgrade
  • The “apt update” command finds all the upgradable packages and downloads them
  • The “apt upgrade” command applies the latest changes
Sudo Apt Update Upgrade
Updating Ubuntu Server

Install Go for Linux

To install Golang, we first need to download it. Then extract the tarball in the local directory. Next, we add the PATH for the Go binary.

1. Download the Go binary for Linux

The Go Linux binary can be downloaded by going to the site and then clicking on Download Go.

Go Download Linux Amd64
Go download for Linux amd64

Here we will download the tarball from the link using wget command.

The command to download the installer file using wget is:

# wget
Wget Linux Go Binary Downloaded
Wget Linux Go binary is downloaded

Now we can check the downloaded file as shown below, using the ls command.

The ls command lists out all files and directories in the current directory.

Go Installer Tarball Show
Show Go Installer tarball in the directory

We can see that go tarball has been downloaded successfully. The file will contain .tar.gz extension which means it is a gzipped tar file.

2. Extract the Golang binaries tarball

Now, we need to extract the tarball using the tar command to a directory of our choice.

We will use /usr/local/ for our convenience.

So, let’s extract the tarball using the command shown below.

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

Above you can see there are some flags used in conjunction with the command. Let’s see what those mean.

  • The flag -C means move to the directory given. Here we use /usr/local.
  • The flags -xzf are three separate flags in unison, -x for extract, -z for gzip, -f file.

The file has been extracted to the directory /usr/local.

To check if extraction is successful, simply go to the directory using the cd command.

cd /usr/local/

Then ls in that directory and as we can see it has been extracted without any hassle.

Check Tarball Extraction Result
Check Tarball Extraction Result

3. Set the PATH for Go

Now let’s see what our current PATH variable is.

To check that simply type echo $PATH in the command line.

echo $PATH
Gopath Is Not Set
Go path Is Not Set

We can see that Go PATH is not set. So, we need to set that.

To set that we will open the .profile file in our home directory. Then we append the PATH modification to that file and save it.

So, let’s do that.

First, run the command to open the file for editing.

We will use nano an integrated text editor to edit the file.

sudo nano $HOME/.profile

Run the above command to edit the file inside the terminal window.

Edit Profile Script For Current User
Edit Profile Script For Current User

The file will open inside the terminal window.

Now append the text as follows in that file.

export PATH=$PATH:/usr/local/go/bin
Append Gopath In Profile Script
Append Go PATH in .profile script

Save the profile file and then run source .profile to apply the changes in the system.

source .profile

Now, we can run go commands from the terminal.

But before that let’s see if we have saved the file correctly.

We simply cat the file content.

cat $HOME/.profile
Changed Script Successfully
Changed script successfully

The PATH modification line is added to the file.

Now, we can test if we can run Go commands from the terminal.

4. Check installed Go version

Run the command go version to check the version installed. We tried installing go version 1.13.5.

go version
Check Go Version
Check Go Version

It works! Congratulations! You have successfully installed Go in your Linux.