Categories: Ubuntu

How to install Go on Ubuntu

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
Sponsored
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:

  1. Install Go using Ubuntu apt repository
  2. Install Go by downloading the source code
  3. 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:

$ sudo apt update

$ sudo apt install golang

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:

$ go version

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:

$ wget https://golang.org/dl/go1.16.5.linux-amd64.tar.gz

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:

$ tar -xzf go1.16.5.linux-amd64.tar.gz -C /usr/local/

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:

$ sudo nano /etc/profile

Now, add the following path at the end of the file.

export PATH=$PATH:/usr/local/go/bin

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:

$ source /etc/profile

Finally, check the installed version of Go language with the help of the following terminal command:

$ go version

Sponsored

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:

$ sudo snap install –classic –channel=version-no/stable go

For example, we want to install Go version 1.17. Therefore, the above command will be changed in the following form:

$ sudo snap install –classic –channel=1.17/stable go

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:

$ mkdir goprogram

Create a new file ‘helloworld.go’ inside the ‘goprogram’ directory by using any source code editor as follows:

$ sudo nano goprogram/helloworld.go

Paste or type the following source code lines in this file:

package main
import “fmt”
func main() {
  fmt.Printf(“Hello, Welcome to the Linuxhint with Go installationn)
}

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:

$ sudo nano ooprogram/go.mod

Paste the following line in this file.

module example.com/mod

Save the above changes and navigate into the ‘goprogram’ directory. Here, build the Go program by using the following command:

$ cd goprogram
$ go build

Now, execute the program with the use of the following program:

$ ./mod

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:

$ sudo rm -rf /usr/local/go

Now, remove the Go directory $PATH environment variable as follows:

$ sudo nano /etc/profile        # remove the source code line from $PATH
$ 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.

Ubuntu Server Admin

Recent Posts

Building RAG with enterprise open source AI infrastructure

One of the most critical gaps in traditional Large Language Models (LLMs) is that they…

3 hours ago

Life at Canonical: Victoria Antipova’s perspective as a new joiner in Product Marketing

Canonical is continuously hiring new talent. Being a remote- first company, Canonical’s new joiners receive…

1 day ago

What is patching automation?

What is patching automation? With increasing numbers of vulnerabilities, there is a growing risk of…

2 days ago

A beginner’s tutorial for your first Machine Learning project using Charmed Kubeflow

Wouldn’t it be wonderful to wake up one day with a desire to explore AI…

3 days ago

Ubuntu brings comprehensive support to Azure Cobalt 100 VMs

Ubuntu and Ubuntu Pro supports Microsoft’s Azure Cobalt 100 Virtual Machines (VMs), powered by their…

3 days ago

Ubuntu Weekly Newsletter Issue 870

Welcome to the Ubuntu Weekly Newsletter, Issue 870 for the week of December 8 –…

4 days ago