Go, which is also known as ‘golang’, is the most popular programming language. The Go first version of 1.0 was released in November 2012, and Google developed it. It allows you to create more pretty and reliable applications. Go is a compiled programming language. First, you need to build a source code to create an executable file that will help to run the software. Most popular platforms such as Docker, Kubernetes, Terraform, and Prometheus are written in Go programming. It is a cross-platform and open-source programming language installed on multiple operating systems such as Windows, macOS, and Linux.
We will install the GO programming language on the Linux distribution Ubuntu 20.04 in this article.
Install Go on Ubuntu 20.04
By following different installation methods, Go can install on Ubuntu 20.04 system. Here, we will discuss three different ways for Go language installation:
- Install Go using Ubuntu apt repository
- Install Go by downloading the source code
- Install Go using snap
Method 1: Install Go using Ubuntu apt repository
Sometimes, the Ubuntu official apt repository contains the older version of Golang packages. While writing this article, the repository of Ubuntu 20.04 contains the Golang 1.13.8 version, an older Go language version. Therefore, it is not recommended to install Go from the Ubuntu apt repository. However, users can easily install Golang packages using the apt package manager. So, by pressing ‘Ctrl+Alt+t’, access the terminal window. Update packages list and type the below-mentioned command to install Go on Ubuntu 20.04 system:
Confirm the installation of Go packages by pressing ‘y’ and hit the ‘Enter key. After confirmation, all required Go dependencies will be installed on your system.
Verify the installation to display the installed Go version on the terminal by running the following command:
As you can see, the default Go version 1.13.8 has been installed on this system.
Method 2: Install Go by downloading source code
Most software applications require the latest version of the Go programming language. In this case, you require installing the latest Go version on your Ubuntu system. While writing this article, the Go 1.17.1 was the latest stable version available for installation. Therefore, before downloading the binary archive, check the latest version on the official Go downloads page. Implement the following steps to install Go using the source code method:
Step 1: Download the Go binary archive
Find and download the latest stable version of Go on Ubuntu 20.04 system by running the following wget command:
After running the above command, a compressed tar file will download on your system.
Step 2: Extract the binary archive
Once the binary archive of Go is downloaded, extract the tar in the /usr/local directory by executing the following command:
Step 3: Adjust path variable for GO
Now, we will add the Go directory path to the environment variable so that the system can easily understand where to search the Go executable binaries. The path of the Go directory either you can add in the ‘/etc/profile file for a system-wide installation that we will follow here or the $Home/.profile file that is specifically defined for current user installation. Using a source code editor, open the file ‘/etc/profile’ as follows:
Now, add the following path at the end of the file.
To save changes, press ‘Ctrl+O’ and then exit from this file by pressing ‘Ctrl+X’.
Activate the PATH of the environment variable by executing the below-mentioned command:
Finally, check the installed version of Go language with the help of the following terminal command:
The installed latest version should display on the terminal window:
Method 3: Install Go using Snap
You can also install Go on Ubuntu 20.04 system by using the snap application. For this purpose, type the below-mentioned commando install Go using snap:
For example, we want to install Go version 1.17. Therefore, the above command will be changed in the following form:
Create test program using Go
To test the Go language installation on Ubuntu 20.04 system, we will build a new workspace and create a new test program using the Go programming language.
First, create a new directory for the test program by running the below-mentioned command:
Create a new file ‘helloworld.go’ inside the ‘goprogram’ directory by using any source code editor as follows:
Paste or type the following source code lines in this file:
import "fmt"
func main() {
fmt.Printf("Hello, Welcome to the Linuxhint with Go installation\n")
}
Save the above changes and close the file. Now, create a new file ‘go.mod’ file inside the ‘goprogram’ directory for building and running the Go program as follows:
Paste the following line in this file.
Save the above changes and navigate into the ‘goprogram’ directory. Here, build the Go program by using the following command:
$ go build
Now, execute the program with the use of the following program:
The following output prints on the terminal window showing that the Go has been successfully installed and running on your system.
Remove and uninstall Go from Ubuntu 20.04
If you do not want to use Go on your system further then, remove the file where the binary archive is extracted as follows:
Now, remove the Go directory $PATH environment variable as follows:
$ source /etc/profile
Conclusion
You learned in this tutorial how to install the Go or golang on Ubuntu 20.04 system by using different installation methods. We have also discussed how you can build and execute the GO program on your system after installation. Contact us via comments in case of any error.