Install the latest Golang on Linux

Install the latest Golang on Linux
People using non-rolling release distributions like Fedora, Ubuntu, Pop OS, or Linux Mint may notice that the Golang package from the official repository is older than the latest version. This article explains how to install the latest Golang on your Linux system using the manual installation method. You can read more about why the manual installation is preferred for compilers and other command-line tools here.

Step 01:
Download the latest (go$go_version.linux-amd64.tar.gz) binary archive file package from the official website. This article assumes you have an x86_64 system. If you are running an ARM computer, download the go$go_version.linux-arm64.tar.gz or go$go_version.linux-armv6l.tar.gz package and change the file name in Step 03 accordingly. Use the uname -a command to check your system architecture.

Step 02:
Open the terminal and change the directory to /usr/local folder.
cd /usr/local

Step 03:
Extract the downloaded go$go_version.linux-amd64.tar.gz archive file into that folder.
sudo tar -xvzf ~/Downloads/go$go_version.linux-amd64.tar.gz

Step 04:
Enter the following command to open the environment variables file.
sudo nano /etc/environment

Step 05:
In the opened file, append the following directory to the existing PATH variable separated by a colon.

Add the following environment variable at the end of the file.

The /etc/environment file in my system before making these changes:

The /etc/environment file in my system after making these changes:
Save the changes and close nano (Ctrl + O, Ctrl + X).

If the /etc/environment is empty or if you like to learn more about setting environment variables, please refer the How to Set Environment Variables in Linux? article.

Step 06:
Run the following two commands to set the currently installed Go lang to be the default choice in your system.
sudo update-alternatives --install "/usr/bin/go" "go" "/usr/local/go/bin/go" 0
sudo update-alternatives --set go /usr/local/go/bin/go

Step 07:
Though Go binaries are installed in /usr/local/go folder, it is recommeded to have a separate directory to install additional Go packages you may use as dependencies in your Go program. Create a new directory ~/.go to install additional Go packages without admin privilege.
mkdir ~/.go

Step 08:
Open the ~/.bash_profile file to add the folder created in the last step as a valid Go path.
nano ~/.bash_profile

Step 09:
Add the following two lines at the end of the ~/.bash_profile file.
export GOPATH=~/.go
export PATH=$PATH:$GOPATH/bin

Step 10:
Restart the system to apply these changes. You can also try the following commands to load these changes into your current terminal without restarting the system but you may have to do this every time you open a new terminal until you restart the machine.
source /etc/environment
source ~/.bash_profile

Step 11:
Enter the following command to check the version of Go.
go version
If you get the installed Go version as the output, you have successfully installed the latest Go in your system.

No comments

Post a Comment



© all rights reserved