Go, also called Go language or Golang, has received wider attention among the developer community because of its simplicity and performance compared to other programming languages. This guide will show how to install Go on Ubuntu 22.04 and enable you to use its robust functionalities. Before diving in, let's try to understand the Go language and Golang benefits.
What is Go programming language?
Go programming language, also known as Golang, was developed by Google in 2007, and its first stable release came in 2009. This shows it's a relatively new programming language compared to Java, Python, JavaScript, and other popular languages.
Go has become a widely-used programming language or cloud-native application offering a straightforward yet powerful approach to building efficient, scalable software.
What is Golang used for?
Golang is used for building simple to complex applications and is a powerful yet easy-to-learn programming language. Google mainly developed Go to address their problems when building large-scale applications. They needed a programming language that was very good at concurrent programming. They also needed a language that had clear and simple syntax, which reduced the errors and bugs while writing codes. Go was created to satisfy both of these needs.
Now, let's look at the main Golang benefits and advantages of using Go.
Golang benefits: What is Golang good for?
Golang benefits are its scalability, readability, and cross-compilation capabilities. Go is well-suited for systems programming, networking applications, microservices, command line tools, and other performance-sensitive or concurrent software.
- Go programming language provides fast compilation times. This makes it ideal for building high-performance applications;
- Go has Goroutines, which are lightweight and more scalable than traditional threads. They can efficiently handle thousands or even millions of concurrent operations;
- It has a simple and straightforward syntax that reduces boilerplate code. Therefore, it's easier for developers to read, write, and maintain code;
- Go has a garbage collector that automatically manages memory allocation and deallocation;
- Go includes powerful tools like "go fmt" for code formatting and "go test" for testing.
Prerequisites
You need the following two prerequisites for this Go installation tutorial:
- An Ubuntu 22.04 installed computer. Check the Ubuntu versions available in it using the
lsb_release -a
command; - A root user account or a user with sudo privileges.
How to install Go on Ubuntu 22.04: Step-by-step process
Now, let’s go through the necessary steps to install Go on Ubuntu 22.04. We will show two options: using the sudo apt install
command or the wget
command.
Step 1: Update the packages and repositories
Before you install Go on your Ubuntu 22.04 machine, it’s recommended you update the existing packages and repositories to make sure you have the latest versions of the packages. Therefore, the first thing to do is to open the terminal on your Ubuntu machine and use the sudo apt update
command to fetch the most recent updates.
sudo apt update
After executing the update command, execute the following command to upgrade your systems’ installed packages.
sudo apt upgrade
You can also combine both commands and run them simultaneously.
sudo apt update && sudo apt upgrade
Step 2: Install Go on Ubuntu
Go can be installed in a few ways. The most straightforward method is using the sudo apt install command or the package manager to install it. However, this does not guarantee the latest version of the Go will be installed.
Alternatively, you can download remote files using the wget method, a command line utility.
Option 1: Install Go using the Ubuntu package manager
Below is the command to install Go using the Ubuntu package manager.
sudo apt install golang-go
This installation will ask for authorization to continue. Type ‘Y’ and press Enter to continue with the Golang installation.
Option 2: Install Go using the wget command
Suppose you install the latest Go package using the binary package from the Golang official website. Then, you need to use the wget
command to fetch it, specifying the URL of the latest version.
Fetch the binary package
Go to the official Go website and its ‘All releases’ page. Select the correct Linux binary package version and copy the link address.
Then, as in the following example, run the wget command. You can specify a custom file name to download the file.
wget https://go.dev/dl/go1.21.4.linux-amd64.tar.gz -O go.tar.gz
Extract the package
After the download, you need to extract it using the following command.
sudo tar -xzvf go.tar.gz -C /usr/local
The command then extracts all the files to the /usr/local/go directory. This will take some time to extract the files.
The go executable files are now located at /usr/local/go/bin/go.
Set the PATH Variable
When you work with Go, you must be able to access the Go command from any of your systems’ directories. To enable this functionality, you need to configure the PATH environment variable by adding the path of Go executables in the ~/.profile or ~/.bashrc file. Use the following command to set the path
echo export PATH=$HOME/go/bin:/usr/local/go/bin:$PATH >> ~/.profile
This command will insert the updated PATH variable in the profile file. Alternatively, you can open the file profile file using ‘vi’ or ‘nano’ and directly insert the above line.
Then, save the changes by sourcing the file using the following command.
source ~/.profile
Step 3: Verify the Golang installation
After using any of the above methods to install Go on Ubuntu, you can verify the installation using the following command.
go version
In this example, the ‘go1.21.1’ part shows the installed version of Go. The ‘linux/amd64’ indicates the operating system and the architecture for which Go is installed.
Step 4: Test Go installation
To check if Go is installed correctly, you can create a simple Go program and check if the correct output is generated. First, create a folder in your workspace and add the file 'test. go'. Insert the following code in the Go file.
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
Then, run the simple go program using the following command.
go run test.go
If it outputs "Hello, World!" you can confirm your Golang installation was successful.
Also read: How to Install Chrome on Ubuntu 24.04
How to uninstall Go?
If you used the apt-get
command to install Go on your Ubuntu 22.04 machine, then use the following command to uninstall it:
sudo apt remove golang-go
Alternatively, if you used the wget
command to install it, remove the files extracted in the go repository. Also, remove the declared PATH variables in the profile:
Conclusion
In this tutorial, you learned how to install Go on Ubuntu 22.04. It is a reasonably straightforward process; as a best practice, update your packages before the installation. Whether you're developing web applications, cloud services, or system tools, Golang offers a simple and efficient development experience that is valuable for development teams.