How to install Golang on Linux
Golang or Go for short is a fantastic modern statically typed and compiled programming language. It’s a free and open-source language developed by Google with the aim of developed a fast and easy to learn language. Needless to say, it has accomplished its goal.
Go was developed by very smart engineers at Google such as Ken Thompson (the one and only) with the features of low-level languages but removes the traps associated with languages such as C. In Go, you don’t have to worry about memory management with its garbage collection or memory safety, concurrency and many more.
For this article though, we will not focus on how great the Go language is but more how we can get the Go compiler running on our Linux machines.
This article has been tested on the the following Linux distributions: Debian 8, 9, 10, and 11, Ubuntu 16, 18, 20, 22. This article is also guaranteed to work on any distributions based on Debian or Ubuntu flavors.
Without further ado, let’s get started.
Update System
As usual, the first step before installing any software packages on Linux is to ensure the system iss up to date. We can do this with two simple commands as shown:
sudo apt-get update && sudo apt-get upgrade
Install Golang in Linux
The next step is to download and install the Go compiler on our system. We can do that by heading to the link in the resource below and download the Go compiler.
http://go.dev
Right click the Download button and copy the download link to the Go archive.
Next, open your terminal and run the command below:
wget https://go.dev/dl/go1.18.3.linux-amd64.tar.gz
The command above will download the archive into your current directory.
Once the download is complete, run the command below to extract the archive. We can do this by running the tar command followed by the directory we wish to store the Go compiler. In our example, we will store the binary in /opt
Run the command as:
tar -C /opt -xvf go1.18.3.linux-amd64.tar.gz
Feel free to replace the name of the archive with your target downloaded version
The command above will extract the Go archive to the /opt
directory.
We can verify by listing the files as shown:
ls -la /opt/go
The command above should return all the files and directories as shown:
root@68ad9b9bc81e:/opt/go# ls -la
total 244
drwxr-xr-x 10 root root 4096 Jun 1 16:52 .
drwxr-xr-x 1 root root 4096 Jun 27 17:41 ..
-rw-r--r-- 1 root root 56057 Jun 1 16:44 AUTHORS
-rw-r--r-- 1 root root 1339 Jun 1 16:44 CONTRIBUTING.md
-rw-r--r-- 1 root root 111408 Jun 1 16:44 CONTRIBUTORS
-rw-r--r-- 1 root root 1479 Jun 1 16:44 LICENSE
-rw-r--r-- 1 root root 1303 Jun 1 16:44 PATENTS
-rw-r--r-- 1 root root 1475 Jun 1 16:44 README.md
-rw-r--r-- 1 root root 397 Jun 1 16:44 SECURITY.md
-rw-r--r-- 1 root root 8 Jun 1 16:44 VERSION
drwxr-xr-x 2 root root 4096 Jun 1 16:47 api
drwxr-xr-x 2 root root 4096 Jun 1 16:52 bin
-rw-r--r-- 1 root root 52 Jun 1 16:44 codereview.cfg
drwxr-xr-x 2 root root 4096 Jun 1 16:47 doc
drwxr-xr-x 3 root root 4096 Jun 1 16:48 lib
drwxr-xr-x 12 root root 4096 Jun 1 16:48 misc
drwxr-xr-x 6 root root 4096 Jun 1 16:52 pkg
drwxr-xr-x 48 root root 4096 Jun 1 16:49 src
drwxr-xr-x 27 root root 12288 Jun 1 16:49 test
The next step is to set the PATH to the Go binary in the system’s environment variable.
Add Go to Path
To add the Go binary to the path, we can edit our shell configuration file. The commands below shows how to edit the profile file for either bash or zsh.
sudo nano ~/.bashrc
sudo nao ~/.zshrc
Next, add the following line at the end of your target file.
export PATH=$PATH:/opt/go/bin
The command above will add the /opt/go/bin
directory to the system’s path. This allows you to run go from any directory on your system.
Save and close the file and run the command below to apply the changes:
source ~/.bashrc
source ~/.zshrc
Verify Go is Installed
Once completed, you can verify go is installed successfully by running the command:
go version
This should return the currently installed version of the Go compiler. An example output is as shown:
go version go1.18.3 linux/amd64
And with that, you have successfully installed Golang on your Linux machine.
Conclusion
Using this guide, you discovered quick and easy steps to setup and configure the Go compiler on your Linux machine.
Explore our other Go tutorials to learn more.
Thanks for reading and I’ll see you in the next one.