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

Ubuntu Weekly Newsletter 866

Welcome to the Ubuntu Weekly Newsletter, Issue 866 for the week of November 10 –…

13 hours ago

Ubuntu vs Debian: Linux Distributions Compared Deep Dive

Debian and Ubuntu are two popular Linux distributions. In this deep dive we will guide…

22 hours ago

How to Install Google Cloud BigQuery Python client library on Linux

In this article, we will see how to Install Google Cloud BigQuery Python client library…

3 days ago

Wallpaper Contest for Xfce 4.20 open for voting

Nov 15,2024 Wallpaper Contest for Xfce 4.20 open for voting The submission phase for the…

3 days ago

Canonical announces the first MicroCloud LTS release

MicroCloud 2.1.0 LTS is now available, expanding the number of Canonical infrastructure solutions with a…

4 days ago

Join Canonical in Paris at Dell Technologies Forum

Canonical is thrilled to be joining forces with Dell Technologies at the upcoming Dell Technologies…

5 days ago