Cyber Month Deal - up to 36% OFF

How to Install Go on Ubuntu 22.04 | Step-by-Step

Published on Feb 20, 2024 Updated on Nov 15, 2024

Go, also called Go language or Golang, has received wider attention among the developer community because of its simplicity and performance compared to other programming languages. This guide will show how to install Go on Ubuntu 22.04 and enable you to use its robust functionalities. Before diving in, let's try to understand the Go language and Golang benefits.

What is Go programming language?

Go programming language, also known as Golang, was developed by Google in 2007, and its first stable release came in 2009. This shows it's a relatively new programming language compared to Java, Python, JavaScript, and other popular languages.

Go has become a widely-used programming language or cloud-native application offering a straightforward yet powerful approach to building efficient, scalable software.

What is Golang used for?

Golang is used for building simple to complex applications and is a powerful yet easy-to-learn programming language. Google mainly developed Go to address their problems when building large-scale applications. They needed a programming language that was very good at concurrent programming. They also needed a language that had clear and simple syntax, which reduced the errors and bugs while writing codes. Go was created to satisfy both of these needs.

Now, let's look at the main Golang benefits and advantages of using Go.

Golang benefits: What is Golang good for?

Golang benefits are its scalability, readability, and cross-compilation capabilities. Go is well-suited for systems programming, networking applications, microservices, command line tools, and other performance-sensitive or concurrent software.

  • Go programming language provides fast compilation times. This makes it ideal for building high-performance applications;
  • Go has Goroutines, which are lightweight and more scalable than traditional threads. They can efficiently handle thousands or even millions of concurrent operations;
  • It has a simple and straightforward syntax that reduces boilerplate code. Therefore, it's easier for developers to read, write, and maintain code;
  • Go has a garbage collector that automatically manages memory allocation and deallocation;
  • Go includes powerful tools like "go fmt" for code formatting and "go test" for testing.

Prerequisites

You need the following two prerequisites for this Go installation tutorial:

  1. An Ubuntu 22.04 installed computer. Check the Ubuntu versions available in it using the lsb_release -a command;
  2. A root user account or a user with sudo privileges.

How to install Go on Ubuntu 22.04: Step-by-step process

Now, let’s go through the necessary steps to install Go on Ubuntu 22.04. We will show two options: using the sudo apt install command or the wget command.

Step 1: Update the packages and repositories

Before you install Go on your Ubuntu 22.04 machine, it’s recommended you update the existing packages and repositories to make sure you have the latest versions of the packages. Therefore, the first thing to do is to open the terminal on your Ubuntu machine and use the sudo apt update command to fetch the most recent updates.

sudo apt update

update existing packages and repositories example

After executing the update command, execute the following command to upgrade your systems’ installed packages.

sudo apt upgrade

upgrade installed packages example

You can also combine both commands and run them simultaneously.

sudo apt update && sudo apt upgrade

Step 2: Install Go on Ubuntu

Go can be installed in a few ways. The most straightforward method is using the sudo apt install command or the package manager to install it. However, this does not guarantee the latest version of the Go will be installed.

Alternatively, you can download remote files using the wget method, a command line utility.

Option 1: Install Go using the Ubuntu package manager

Below is the command to install Go using the Ubuntu package manager.

sudo apt install golang-go

This installation will ask for authorization to continue. Type ‘Y’ and press Enter to continue with the Golang installation.

install go using APT example

Option 2: Install Go using the wget command

Suppose you install the latest Go package using the binary package from the Golang official website. Then, you need to use the wget command to fetch it, specifying the URL of the latest version.

Fetch the binary package

Go to the official Go website and its ‘All releases’ page. Select the correct Linux binary package version and copy the link address.

select linux binary package example

Then, as in the following example, run the wget command. You can specify a custom file name to download the file.

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

download go package example

Extract the package

After the download, you need to extract it using the following command.

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

extract go package example

The command then extracts all the files to the /usr/local/go directory. This will take some time to extract the files.

The go executable files are now located at /usr/local/go/bin/go.

navigate to go directory

Set the PATH Variable

When you work with Go, you must be able to access the Go command from any of your systems’ directories. To enable this functionality, you need to configure the PATH environment variable by adding the path of Go executables in the ~/.profile or ~/.bashrc file. Use the following command to set the path

echo export PATH=$HOME/go/bin:/usr/local/go/bin:$PATH >> ~/.profile

This command will insert the updated PATH variable in the profile file. Alternatively, you can open the file profile file using ‘vi’ or ‘nano’ and directly insert the above line.

Then, save the changes by sourcing the file using the following command.

source ~/.profile

Step 3: Verify the Golang installation

After using any of the above methods to install Go on Ubuntu, you can verify the installation using the following command.

go version

check go version example

In this example, the ‘go1.21.1’ part shows the installed version of Go. The ‘linux/amd64’ indicates the operating system and the architecture for which Go is installed.

Step 4: Test Go installation

To check if Go is installed correctly, you can create a simple Go program and check if the correct output is generated. First, create a folder in your workspace and add the file 'test. go'. Insert the following code in the Go file.

package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}

Then, run the simple go program using the following command.

go run test.go

test go code

If it outputs "Hello, World!" you can confirm your Golang installation was successful.

Also read: How to Install Chrome on Ubuntu 24.04

How to uninstall Go?

If you used the apt-get command to install Go on your Ubuntu 22.04 machine, then use the following command to uninstall it:

sudo apt remove golang-go

Alternatively, if you used the wget command to install it, remove the files extracted in the go repository. Also, remove the declared PATH variables in the profile:

remove go files to uninstall example

Conclusion

In this tutorial, you learned how to install Go on Ubuntu 22.04. It is a reasonably straightforward process; as a best practice, update your packages before the installation. Whether you're developing web applications, cloud services, or system tools, Golang offers a simple and efficient development experience that is valuable for development teams.

Cloud VPS - Cheaper Each Month

Start with $9.99 and pay $0.5 less until your price reaches $6 / month.

Share this article

Related Articles

Published on Jun 7, 2021 Updated on Jun 29, 2022

AlmaLinux Review: a CentOS Clone Supported by CloudLinux

AlmaLinux is an open-source Linux distribution focused on long-term stability, that is a 1:1 binary compatible fork of Red Hat Enterprise Linux (RHEL)

Read More
Published on Sep 14, 2021 Updated on Jun 29, 2022

Debian 11 "bullseye" Review: What‘s New?

Debian 11 “bullseye” was released on 14th of August 2021. This release contains over 11294 new packages out of 59551 packages overall in its repositories.

Read More
Published on May 31, 2022 Updated on May 5, 2023

A Complete Guide to Linux Bash History

Learn how to work with Bash history to become more efficient with any modern *nix operating system.

Read More
We use cookies to ensure seamless user experience for our website. Required cookies - technical, functional and analytical - are set automatically. Please accept the use of targeted cookies to ensure the best marketing experience for your user journey. You may revoke your consent at any time through our Cookie Policy.
build: 06ac5732e.831