How to Install Golang on Ubuntu 20.04 | 20.04

Golang, also known as Go, is a modern, open-source programming language developed by Google. It was designed by Robert Griesemer, Rob Pike, and Ken Thompson with the primary goal of providing a simple, efficient, and easy-to-use language for software development. Go was first released in 2009 and has gained widespread popularity among developers for its speed, simplicity, and powerful features.

Key Features and Benefits of Golang

  • Concurrency: Golang offers built-in support for concurrent programming with goroutines, lightweight threads managed by the Go runtime. This enables efficient and straightforward concurrent processing, making it well-suited for applications that require parallelisms, such as web servers and networking tools.
  • Static Typing: Go is a statically typed language that helps catch type-related errors during compilation. This feature enhances code safety and allows for better performance during runtime.
  • Garbage Collection: Go includes an efficient garbage collector, which automatically handles memory management and reclaims unused memory. This feature reduces the risk of memory leaks and simplifies the development process.
  • Cross-Platform: Go supports multiple platforms, including Windows, macOS, Linux, and others. This enables developers to write code that can be easily compiled and executed across different operating systems without any modifications.
  • Standard Library: Go has a rich standard library that provides a wide range of packages and functions for common programming tasks, such as handling files, working with regular expressions, and networking. This allows developers to build powerful applications without relying on third-party libraries.

Golang vs. Other Languages

  • Simplicity: Go is designed to be simple and easy to learn, with a clean syntax that allows developers to understand and write code quickly. This stands in contrast to languages like C++ and Java, which can be more complex and harder to master.
  • Performance: Go offers excellent performance, focusing on efficient compilation and execution. Go’s performance is comparable to languages like C and C++ but with the added benefit of garbage collection and improved memory management.
  • Scalability: Go’s built-in support for concurrency makes it an excellent choice for building scalable applications, as it can handle many concurrent connections or tasks. This is an advantage over languages like Python, which has a Global Interpreter Lock (GIL) that can limit concurrency.

In the upcoming sections, this guide will demonstrate how to install Golang on Ubuntu 22.04 Jammy Jellyfish or Ubuntu 20.04 Focal Fossa using the Golang backports PPA for the latest stable versions.

Section 1: Install Golang with 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: Select the Installation Method

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

Section 2: Installing Go (Golang) from Source

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.

Example command:

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.

Section 3: Creating a Sample Golang Program

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\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:


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:


You should see the following output:

Hello, World from

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

Section 4: Managing and Switching Between Golang Versions

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 output:

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.

Additional Resources and Links

To further enhance your understanding of Golang and Ubuntu Linux, we recommend the following official resources:

  • Golang Official Website: The official website for Golang provides comprehensive information on the language, its features, and best practices for developing robust applications.
  • Golang Documentation: Visit the official Golang documentation to learn more about the language, including tutorials, reference materials, and language specifications.

Share to...