Installing Go (Golang) on Ubuntu Linux such as Ubuntu 24.04, 22.04, or 20.04 is a straightforward process. We can use the default system repository to download the open-source Go programming language using the commands given in this tutorial, for easily building simple, reliable, and efficient software.
Go language which was designed at Google combines the speed of compiled languages with the ease of use characteristic of dynamic languages, making it an attractive choice for a wide range of applications, from web servers to data analysis. Ubuntu Linux is becoming popular among the developers community due to its open-source nature and robust community support. Hence, why not use it for developing apps in Go as well…
There are three easy ways to install Golang– one is using the default system repository and the others are using a PPA repo and its Tarball file. Let’s see how to use them.
#1st method using system default repository:
Use Apt to Update Ubuntu Linux
As we are about to use the system repositories for installing Go Lang, it is recommended first to run the system update command. It will rebuild the package index cache for APT and also install the latest updates, if available.
sudo apt update
Install Go Lang using the APT repo
Yes, although GO Lang is available through the default repositories of Ubuntu, however, the version through it will not be the latest one. Therefore, if you are looking for the latest Go version then go for 2nd or 3rd method to install it instead of this one.
sudo apt install golang
#2nd Method using PPA repository:
Add Go PPA repository
To quickly get the not only latest Go programming version on Ubuntu but also the future update just by using the default Ubuntu package manager i.e APT, we can add a PPA repository, here is the command to follow:
sudo add-apt-repository ppa:longsleep/golang-backports
Installing the latest Go on Ubuntu 24.04, 22.04, or 20.04
Once you have added the repository, we can install the latest available version of Go Lang on our Ubuntu system whether it is Ubuntu 24.04, 22.04, or 20.04… The installation command will be the same as we used earlier in the first method.
sudo apt install golang
#3rd Method by manually downloading the Go Tarball file
Download the Go Tar file
Those who don’t want to add any third-party PPA repository on their existing Ubuntu systems can manually download the archive file of the latest Go Lang version from the official website. Visit the link and click on the file available for Linux distros.
Extract the Go Archive
Extract the downloaded archive to “/usr/local”, the given command will automatically create a “Go” directory with all the extracted files.
First switch to a location on the terminal where you have saved the downloaded file and after that run the given command.
sudo tar -C /usr/local -xzf filename.tar.gz
In our case the downloaded file name was “go1.22.0.linux-amd64.tar.gz“, hence the command will be:
sudo tar -C /usr/local -xzf go1.22.0.linux-amd64.tar.gz
Make sure you have used the right file name to successfully execute the command.
Configure the PATHS
We have just copied the Go files to our desired location but the system doesn’t know where the Go executable is if we run a command to use it. Therefore either we need to manually switch to the “/usr/local/go/bin” directory or add it to our system path so that it can recognize it and allow us to run and use Go from anywhere in the terminal.
So, let’s see how to add it to our system’s path:
echo 'export PATH=$PATH:/usr/local/go/bin' | sudo tee -a ~/.bashrc
Reload Shell session:
source ~/.bashrc
Check Go Version
To check the installed GoLang version on Ubuntu or any other OS, execute the given command syntax:
go version
Write a Go program to test it
To get familiar with the programming language, let’s create a simple hello program to learn how Golang works.
nano hello.go
Add the following lines
package main
import "fmt"
func main () {
fmt.Printf( "hello world\n" )
}
Save the file using Ctrl+X, Y, and hit the Enter key.
Explanation: package main
– Tells the Go compiler that the file should compile as an executable program. Whereas import
tell the system to import “fmt
” from the Go library for printf
function.
Compile the program:
go hello.go
After compilation, an executable file will appear in the same directory. We can execute that to see the output:
./hello
To practice further, you can use the Golang online editor or learn more from the documentation.