How to Install Golang on Ubuntu 22.04 or 20.04

In this guide, we will demonstrate how to install Golang on Ubuntu 22.04 or 20.04. Golang, also known as Go, is an open-source programming language designed for efficiency, readability, and high performance. Developed by Google, it’s particularly favored for its simplicity and scalability in software development.

Key Features of Golang:

  • Concurrent Programming: Go’s built-in support for concurrency allows it to efficiently utilize multi-core processors, making it ideal for cloud computing and server-side applications.
  • Strongly Typed: It ensures type safety, helping to prevent errors that occur due to mismatched types.
  • Garbage Collection: This feature automatically frees memory, helping to manage resources more efficiently and reduce memory leaks.
  • Fast Compilation: Go compiles to machine code, which means it runs swiftly and efficiently.
  • Extensive Standard Library: Offers a wide range of inbuilt packages for various functionalities, enhancing development speed and efficiency.
  • Cross-Platform Development: You can develop applications for various platforms including Windows, Linux, and macOS.

Understanding these features sets the stage for integrating Golang into your development environment. Installing it on Ubuntu is a straightforward process that involves downloading the package, setting environment variables, and verifying the installation. Let’s dive into the step-by-step installation process.

Install Golang on Ubuntu 22.04 or 20.04 via APT

In this section, we will guide you through the process of installing Golang on your Ubuntu system using the APT package manager. There are two methods to achieve this: installing Golang using Ubuntu’s default repository or using the Golang-Backports PPA for a more up-to-date version.

Step 1: Update Ubuntu System

To ensure a smooth installation, it’s essential to have an up-to-date system. Run the following command to update your Ubuntu system:

sudo apt update && sudo apt upgrade

Step 2: Install Golang on Ubuntu via APT Command

There are two methods available for installing Golang on your Ubuntu system. Choose the one that best suits your needs.

Method 1: Install Golang with Ubuntu’s Repository

This method is the simplest way to install Golang using the APT repository provided by Ubuntu. To install Golang, execute the following command:

sudo apt install golang

After installation, you can verify it by running:

go version

Note: The Golang version in Ubuntu’s repository may not always be the latest. If you require a more recent version, consider using Method 2, described below.

Method 2: Install Golang with Golang-Backports PPA

For users who prefer a more recent and regularly updated version of Golang, the Golang-Backports PPA is an excellent choice. Follow these steps to install Golang using the Golang-Backports PPA:

Import the PPA by running the following command:

sudo add-apt-repository ppa:longsleep/golang-backports -y

Update the APT package list to include the newly added PPA:

sudo apt update

Install Golang using the PPA. This command works even if you have previously installed an older version:

sudo apt install golang

Finally, check the installed Golang version to ensure it meets your requirements:

go version

Note: The Golang-Backports PPA includes versions 1.8, 1.9, 1.10, 1.11, 1.12, 1.13, 1.14, 1.15, 1.16, 1.17, 1.18, 1.19, and 1.20. If you need a specific older version, you can install it by specifying the version number. For example, to install Golang v1.18, run:

sudo apt install golang-1.18

Install Go (Golang) from Source on Ubuntu 22.04 or 20.04

If you prefer installing a specific or more recent version of Go, the manual method is an excellent choice. In this section, we’ll walk you through the process of downloading and installing Go from source.

Step 1: Download the Go source file

First, download the latest version of Go from the official website using the wget command. Replace x.x.x with the desired version number.

https://go.dev/dl/go1.x.x.darwin-amd64.tar.gz

Step 2: Extract the Go source file

After downloading Go, extract the file to the /usr/local directory using the tar command.

sudo tar -xzf go1.*.*.linux-amd64.tar.gz -C /usr/local/

Step 3: Update the PATH variable

Update the $PATH variable in the /etc/profile file using the echo and tee commands:

echo 'export PATH=$PATH:/usr/local/go/bin' | sudo tee -a /etc/profile

This command appends the required line (export PATH=$PATH:/usr/local/go/bin) to the /etc/profile file without the need to open a text editor. The -a flag ensures that the content is appended to the file rather than overwriting it.

Step 4: Activate the updated PATH variable

Activate the updated PATH environment variable by running the source command:

source /etc/profile

Step 5: Verify the installation

Finally, confirm that the installation was successful by checking the Go version:

go version

This command will display the currently installed Go version, indicating a successful installation.

Creating a Sample Golang Program on Ubuntu 22.04 or 20.04

In this section, you’ll learn how to create a simple Golang program that prints “Hello, World” to demonstrate that your Go installation is functioning correctly.

Step 1: Create a new directory for the program

First, create a new directory to store your sample Go program:

mkdir go-hello

Step 2: Create a Go source file

Now, create a new Go source file within the go-hello directory:

nano go-hello/hello.go

Step 3: Write the “Hello, World” program

Add the following code to the hello.go file to create a “Hello, World” program:

package main

import "fmt"

func main() {
     fmt.Printf("Hello, World from LinuxCapable.com\n") 
}

Save the file by pressing (CTRL+O) and then exit the editor by pressing (CTRL+X).

Step 4: Create a go.mod file

Create a go.mod file in the go-hello directory to manage the Go project’s dependencies:

sudo nano go-hello/go.mod

Add the following content to the go.mod file:

module example.com/mod

Save the file (CTRL+O) and exit the editor (CTRL+X).

Step 5: Build and execute the program

Navigate to the go-hello directory and build the program:

cd go-hello && go build

Lastly, execute the “Hello, World” program by running the following command:

./mod

You should see the following output:

Hello, World from LinuxCapable.com

This confirms that your Go installation is working as expected and you’ve successfully created and executed a simple Go program.

Managing and Switching Between Golang Versions on Ubuntu 22.04 or 20.04

In this section, we will guide you through the process of managing and switching between multiple Golang versions installed on your system using the update-alternatives command. This is particularly useful when you need to work with different Golang versions for various projects.

Step 1: Add Golang Versions to the Alternatives System

To manage multiple Golang versions, you must first add each installed version to the alternatives system. Suppose you have Golang 1.18, 1.19, and 1.20 installed. You can add them using the following example commands:

sudo update-alternatives --install /usr/bin/go go /usr/lib/go-1.18/bin/go 118
sudo update-alternatives --install /usr/bin/go go /usr/lib/go-1.19/bin/go 119
sudo update-alternatives --install /usr/bin/go go /usr/lib/go-1.20/bin/go 120

In these commands, /usr/bin/go is the generic symlink, go is the name of the alternative, /usr/lib/go-1.18/bin/go, /usr/lib/go-1.19/bin/go, and /usr/lib/go-1.20/bin/go are the paths to the respective Golang binaries, and 118, 119, and 120 are the priority numbers.

Step 2: List Available Golang Alternatives

Once you’ve added the Golang versions to the alternatives system, you can list the available alternatives by running the following command:

sudo update-alternatives --list go

This command displays the available Golang versions and their respective paths.

Step 3: Switch Between Golang Versions

To switch between Golang versions, use the –config option followed by the name of the alternative, like this:

sudo update-alternatives --config go

This command prompts you to select the desired version from the available alternatives. Enter the corresponding number and press Enter to change the active Golang version.

example switching golang alternative versions in terminal on ubuntu linux

Step 4: Verify the Active Golang Version

After switching the version, it’s essential to verify the change by running the following command:

go version

This command displays the currently active Golang version, ensuring you use the desired version for your projects.

Closing Thoughts on Installing Golang on Ubuntu Linux

In summary, installing the Golang programming language on Ubuntu Linux is a seamless process that empowers developers to harness the power and simplicity of this modern language. We’ve discussed various aspects, including updating the system, installing Golang via the APT repository and the Golang backports PPA, and managing multiple Golang versions using the update-alternatives command. By following these steps, users can effortlessly set up Golang on their Ubuntu Linux systems and begin developing high-performance applications.

Your Mastodon Instance
Share to...