how to install go on ubuntu 22.04

How to Install Go on Ubuntu 22.04

Golang, also known as Go, is a modern programming language developed by Google in 2007 to improve its programming productivity.

Go is open-source, and it is getting more and more popular and used in many applications such as Docker, Kubernetes, and static generators like Hugo.

This language is well-designed to be used by professionals for application development. Go is easy to create and maintain, making it an ideal programming language for building efficient software. Go is reliable, easy to build, and has efficient software that scales fast. In this tutorial, we will show you how to install Go on Ubuntu 22.04.

Prerequisites

  • Ubuntu 22.04
  • SSH access with root privileges or a regular system user with sudo privileges

Step 1. Log in to the server

First, log in to your Ubuntu 22.04 server through SSH as the root user:

ssh root@IP_Address -p Port_number

You will need to replace “IP_Address” and “Port_number” with your server’s IP address and SSH port number, respectively. Additionally, replace “root” with the username of the system user with sudo privileges.

You can check whether you have the proper Ubuntu 22.04 version installed on your server with the following command:

# lsb_release -a

You should get this output:

No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04 LTS
Release: 22.04
Codename: jammy

Before starting, you have to make sure that all Ubuntu OS packages installed on the server are up to date. You can do this by running the following commands:

# apt update -y

Step 2. Install Go

There are several methods of installing Go on Ubuntu 22.04. The first option is to install Go from the default Ubuntu 22.04 repository. The second option is to download the Go binary provided by their developer. And the third option is to install it from the source.

To install it from the default Ubuntu repository, we can simply execute this command to install Go.

# apt install golang-go

At the time of this writing, you will get Go version 1.18.1 after installing it from the Ubuntu repository; we can check it by running the command below.

# go version

The second method is to install Go by using the binary package. If you want the more recent version of Go and install it the easier way, we can download the binary distribution from the Go website. You can check and get the download link from their website at https://go.dev/dl/; let’s download Go now.

# wget https://go.dev/dl/go1.21.0.linux-amd64.tar.gz -O go.tar.gz

Once downloaded, we need to extract the file.

# tar -C /usr/local -xzvf go.tar.gz

The command above will extract the files and you should see the directory /usr/local/go/. So, at this moment, you can access the go executable at /usr/local/go/bin/go.

Next, we need to tell our system where to find the Go binary file. To do this, we need to set the PATH environment variable. We need to add a path to the ~/.profile or ~/.bashrc file for the current user.

# nano ~/.profile

Then, append these lines to the file:

export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH

Don’t forget to save the file and then load the new PATH variables into the current shell by running the following command.

# source ~/.profile

At this point, you can simply type ‘go’ every time you need to call /usr/local/bin/go. For example, let’s check the Go version:

# go version

The version of Go installed on your Ubuntu system will be displayed on the terminal. It will show you an output like this:

go version go1.21.0 linux/amd64

Step 3. Use Go

Once Go is downloaded and configured, it is time to test it. In this case, we will run a simple code written in the Go language to check if the environment is working on our Ubuntu 22.04 system. Let’s create a file.

# nano sample.go

Then, insert the following into the file.

package main
import "fmt"
func main() {
fmt.Printf("Hello World\n")
}

Save the file, then exit. Next, execute this command to run the script.

# go run sample.go

The command will return an output like this:

That’s it. You have successfully installed Go on your Ubuntu 22.04 system.

Of course, you don’t need to install Go on Ubuntu 22.04 VPS if you are using one of our Ubuntu VPS plans, which you can ask Linux experts to install Go on Ubuntu 22.04 system for you. Our administrators are available 24/7 and will start working on your request immediately; always trust our epic support.

PS. If you liked this article on how to install Go on Ubuntu 22.04, share it with your friends on social media or leave a comment in the comment section.

One thought on “How to Install Go on Ubuntu 22.04

Leave a Reply

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