Ubuntu

How to Install and use Go on Ubuntu 22.04

Golang, commonly referred to as GO, is an open-source, popular programming language used by professionals to develop different applications. It’s easy to build and simple to manage, thus making it an ideal language to create efficient software. The code syntax of GO is quite similar to C, but the language offers more features, including garbage collection, memory safety, structural typing and more.

This article will guide you on installing Go on the Linux distribution Ubuntu 22.04.

How to install Go on Ubuntu 22.04

There are several methods to install Go on Ubuntu 22.04 and the details of each method are given below:

Method 1: Using Apt command

The apt package management system in Ubuntu 22.04 includes the repository of Go, thus allowing the user to install the latest version of software directly from the apt command. The following installation command will be used to install Go on Ubuntu:

$ sudo apt install golang-go

To check the Go version, use the following command:

$ go version

Removing Go installed from Ubuntu 22.04
To remove Go installed from the apt repository, use the following command.

$ sudo apt remove --autoremove go

Method 2: Install Go from binary file

You can use this method to install the latest version of Go on Ubuntu 22.04. For that purpose, you will need to follow the steps given below:

Step 1: First, download the latest version of the Go binary file using the following command:

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

You can confirm the latest version by visiting the Go download page.

The above command will install the Go compressed file in the Downloads directory of Ubuntu 22.04.

Step 2: Now, use the following command to extract the file in “/usr/local” location:

$ sudo tar -xf go1.18.1.linux-amd64.tar.gz -C /usr/local

Step 3: Next, use your favorite editor to open the Go profile via the following command.

$ sudo nano /etc/profile

Replace the above highlighted text with the text provided below:

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

Save the file and execute the following command to reload the new path environment:

$ source.profile

To check the go version installed from this method, use the following command.

$ go version

Removing Go installed from binary file
To remove Go from Ubuntu 22.04, execute the following command.

$ sudo rm -rf /usr/local/go

Method 3: Install Go using Snap

You can also install Go using the Snap Store on Ubuntu 22.04Snap Store is a package management system that includes the latest version of the software. To install Go from Snap Store, follow the below steps.

Step 1: Open the Ubuntu terminal and use the following installation command to install Go from the Snap Store.

$ sudo snap install go --classic

Removing Go through Snap
To remove Go from the Snap Store, you can use the following command.

$ sudo snap remove go

How to use Go in Ubuntu
Once the Go installation is completed, it’s time to execute a simple code written in the Go language to test whether the environment is working fine on Ubuntu or not.

Create a directory with the name of your choice using the following command:

$ mkdir Welcome-Users

Now go to the directory and run the example module using the following commands:

$ cd Welcome-Users
$ go mod init example/Welcome-Users

Create a file to store your code using the following command:

$ touch Welcome-Users.go

Edit the file “Welcome-Users.go” using the following command:

$ sudo nano Welcome-Users.go

Then paste the following code in the file:

package main
import "fmt"
func main() {
    fmt.Printf("Welcome Linux Hint Users\n")
}

Save the above file, and then use the following command to run the code:

$ go run Welcome-Users.go

That’s it, now you can execute other codes using the same method on Ubuntu 22.04. For further guidance, you can read the Go Documentation.

Conclusion

Go language is the right opportunity for young developers and professionals to enjoy a good coding experience. With Go, you will be able to create multiple web applications using a secure and robust coding environment. There are several methods to install Go on Ubuntu 22.04 and it’s entirely up to your which method to pick because all will install Go within a couple of minutes.

About the author

Awais Khan

I'm an Engineer and an academic researcher by profession. My interest for Raspberry Pi, embedded systems and blogging has brought me here to share my knowledge with others.