How to install Go on Ubuntu 2

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

How to install Go on Ubuntu 3

$ sudo apt install golang

How to install Go on Ubuntu 4

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

How to install Go on Ubuntu 5

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.

How to install Go on Ubuntu 6

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/

How to install Go on Ubuntu 7

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

How to install Go on Ubuntu 8

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

How to install Go on Ubuntu 9

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

$ go version

How to install Go on Ubuntu 10

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

How to install Go on Ubuntu 11

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

How to install Go on Ubuntu 12

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)
}

How to install Go on Ubuntu 13

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

How to install Go on Ubuntu 14

Paste the following line in this file.

module example.com/mod

How to install Go on Ubuntu 15

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

$ cd goprogram
$ go build

How to install Go on Ubuntu 16

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

$ ./mod

How to install Go on Ubuntu 17

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.

Leave a Comment

Your email address will not be published. Required fields are marked *