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
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 https://go.dev/ and then clicking on Download Go.
Here we will download the tarball from the link using wget command.
The command to download the installer file using wget is:
# wget https://dl.google.com/go/go1.13.5.linux-amd64.tar.gz
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.
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
-Cmeans move to the directory given. Here we use /usr/local.
- The flags
-xzfare 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
ls in that directory and as we can see it has been extracted without any hassle.
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.
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.
The file will open inside the terminal window.
Now append the text as follows in that file.
Save the profile file and then run source .profile to apply the changes in the system.
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.
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.
It works! Congratulations! You have successfully installed Go in your Linux.