How to install Golang via Linux terminal in 5 easy steps

Are you looking to get started with Golang? Need help installing Golang on Linux or WSL (Windows Subsystem for Linux)?

Here the 4 east steps to get Golang up and running your machine.

Step 1: Download Linux tar.gz package from Golang website

The first step is to download the latest, and stable binary release from the Golang website.

Download here: https://golang.org/dl/

Step 2: Unzip the tar.gz package and move it to the /usr/local directory

The next step is to unzip the tar.gz package and move the files into the machine local directory.

You can execute these 2 commands with 1 command in the terminal


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

Let’s break down the command above.


-C /usr/local

This part of the command I’m telling the tar command to change the directory on where to dump the extracted output. In this case, I want the extracted files to go the /usr/local directory, where most local binaries live.

The next part of the command is:


-xzf go1.15.6.linux-amd64.tar.gz

-xzf is just saying to extract all the compress files from the tar.gz package.

After those arguments you see go1.15.6.linux-amd64.tar.gz, this should be pointing to where the package was downloaded.

I use Windows, so my downloaded packages go to my Download directory.

For me, my command looks like this:


tar -C /usr/local -xzf /mnt/c/Users/ruben/Downloads/go1.15.6.linux-amd64.tar.gz

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

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

Open up your .bashrc or .bash_profile file.


vim ~/.bash_profile

Then add the following to a new line:


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

Step 4: Add the $GOPATH to your environment variables

In the same file that you added your $PATH environment variable, add a new variable called $GOPATH and point to where your Golang apps are going to live.


export GOPATH="/mnt/c/Users/${user}/directory/to/your/golang/workspace"

It’s common practice to have all your Golang apps in 1 directory. To learn more about Golang workspaces go here: https://golang.org/doc/gopath_code.html#Workspaces

Step 5: Test Go command

Now it’s time to test if your Go command works but first reload your .bash_profile with the source command.


source ~/.bash_profile

Now run go version:


go version

#Output
go version go1.15.6 linux/amd64

I like to tweet about Golang and post helpful code snippets. Follow me there if you would like some too!