How to Install Go on Ubuntu 20.04

Install Go on Ubuntu 20.04 and start developing reliable applications with ease. Being clean and efficient, Go is an open-source utility meant to enhance the productivity of programmers who like to code in Windows, Mac, or Linux environments.

With Go, you’re looking at a cross-platform compatible, compiled programming language. Introduced back in 2012, Go or popularly known as golang, offer programmers a great way to develop concise and promising applications. 

The complied nature of this language is why users first need to compile the source code before creating an executable file and eventually running it. Sounds confusing? Don’t worry, I’ve got you covered.

The following tutorial will help you learn the best way to install Go on Ubuntu 20.04.

How to Install Go on Ubuntu 20.04

You can install Go on your Ubuntu system in three different ways. While employing the apt repository is considered the easiest route, some users opt to invoke the wget command and download the Go utilities from the source.

Using apt Repository to Install Go on Ubuntu 20.04

One of the easiest approaches to installing Go on Ubuntu 20.04 is by employing the system app repository. However, the method is less recommended because the official apt repository comprises an older variant of the Go package.

Updating the apt Repository

Launch the Terminal by hitting the Ctrl+Alt+T key combination

Run the following command:

$ sudo apt update
updating apt repository

Installing Golang

Run the following command:

$ sudo apt install golang
installing go lang and install go on ubuntu 20.04

Input Y and hit the Enter key to confirm the installation process. After that, wait for the system to get all the required Go dependencies ready.

Finally, verify the installation of Go by executing the command as:

$ go version

Installing Go by Downloading Source Code

If you want to operate modern-day applications most efficiently, it is crucial to have the latest version of the Go language. The best way to get information about the same is by visiting the official Go download page and looking for the most updated variant available. 

Once you’re done, you can install Go on Ubuntu 20.04 by following the steps mentioned below.

Downloading the Go Binary Archive

Start by downloading the latest version of the Go binary archive using the wget command. In other words, launch the Terminal and run the command in the following format:

$ wget [Link]
downloading go binary

A compressed tar file corresponding to the Go binary will download your system on successful execution.

Getting the Binary Archive Extracted

Are you done downloading the Go binary archive? Well then, it is time to get it extracted. Use the -C flag alongside the file name and the desired pathname to get the job done. It is important; you extract the downloaded tar file in the usr/local directory

Run the command in the following format:

$ [file] -C /usr/local

Adjusting the Path Variable for Go

The next step is adding the Go directory path to the environment variable. Doing this is crucial as it will assist the system in searching the Go executable binaries. You can choose between /etc/profile or $Home/profile for the path. While the former is for system-wide installation, the latter is user-specific.

Operate any source code editor to open /etc/profile

$ sudo nano /etc/profile

Make sure the end of the file has the following path:

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

Save the file by pressing the Ctrl+O key combination.

Press Ctrl+X to exit.

Activating the Path Environment

Finally, you’re ready to activate the PATH environment variable. Doing this is pretty simple; just run the following command:

$ source /etc/profile

Verifying Go Installation

As you already know, using the Go version command will help you verify the installation of Go on Ubuntu 20.04

Input:

$ go version

Installing Go using Snap

Having talked about two methods of installing Go on your Ubuntu 20.04 system, let’s look at another easy and efficient way of getting the Go language ready in just a few minutes.

Run the following command:

$ sudo snap install --classic-- channel=[desired version number]/stable go
install go on ubuntu 20.04

Using Go in Ubuntu 20.04

using Go on ubuntu

Now that we’re done uncovering the methods of installing the Go language on Ubuntu, it is time to look at the operational overview.

Creating a Test Program with Go

For testing the Go language, you’ll have to create a new workspace and start with a test program. To create a new directory, we’ll invoke the mkdir command.

$ mkdir testgoprogram

After that, you’ll need to create a few files inside the testgoprogram directory. Let’s name it, howareyoudoing.go. Make sure you use a source code editor for this in the following manner:

$ sudo nano testgoprogram/horwareyoudoing.go

Paste the following codes:

package main
import "fmt"
func main()  {
        fmt.Printf("Desired Message")
}

As soon as you exit the previous file, go ahead creating a new one. Name it go.mod and again build it inside the testgoprogram directory. 

$ sudo nano testgoprogram/go.mod

Paste the following code:

module example.com/mod

Save the file and exit.

The Final Test

Navigate to the testgoprogram directory and build the Go program using the cd command.

$ cd testgoprogram
$ go build

Finally, execute the program by invoking the following code:

$ ./mod

Can you see the value you entered in the Desired Message section? If yes, this means Go is successfully running on your Ubuntu 20.04. 

Uninstalling Go from Ubuntu 20.04

In case you don’t want to use the Go program anymore, follow the steps below and remove the associated files.

Use the rm command together with the -rf flag and remove the file from the destination where the archive is extracted 

$ sudo rm -rf /usr/local/go
removing go from ubuntu

At last, get rid of the Go directory $PATH environment variable by running the following code:

$ sudo nano /etc/profile
# remove the source code line from $PATH
$ source /etc/profile

Voilà, this is how you install Go on Ubuntu 20.04. In this article, I’ve walked you through three different methods of getting the Go language ready in a matter of minutes. Also, reading all the way through, you’ll learn the easiest way to remove the files associated with Go from your system.

If this guide helped you, please share it.

Leave a Reply
Related Posts