Sign In
Sign In

How to Install and Configure Go on Ubuntu: A Step-by-step Guide

How to Install and Configure Go on Ubuntu: A Step-by-step Guide
Anees Asghar
Technical writer
Go
18.10.2024
Reading time: 6 min

Go, also known as Golang, is a programming language created by Google. It simplifies coding and provides great performance, which makes it popular among developers. Its user-friendly syntax and built-in concurrency support allow developers to efficiently create scalable applications. Moreover, Go is a cross-platform programming language and is available for all major operating systems, including Linux, Windows, etc.

In this article, we’ll discuss what Golang is and how to install and use it on Ubuntu.

What is Golang?

Golang is a simple yet powerful programming language that enables developers to build basic to advanced applications. Go was developed to manage large-scale projects, with a focus on concurrency and clean code. Its scalability and cross-platform compatibility have made it a popular choice among developers.

Go is widely used in systems programming, networking, and microservices. Key benefits of Golang include fast compilation, simple syntax, automatic memory management, and tools like go fmt and go test.

Prerequisites for Installing Go on Ubuntu

To install the Go language on Ubuntu, the below-listed requirements must be fulfilled:

  • Ubuntu must be pre-installed.
  • A user with root or sudo privileges.

If a user fulfills the prerequisites, he can proceed with the installation of Go on Ubuntu.

Method 1: Installing Golang on Ubuntu via the Default Package Manager

Apt is the default package manager of Ubuntu. Installing Golang through the default package is the most convenient way. However, it’s important to note that the version installed via Ubuntu's default package manager may not always be the latest.

Let’s go through the below-listed steps to install Go on Ubuntu through apt:

Updating and Upgrading System Repositories

Before installing Golang on Ubuntu, it is recommended to update and upgrade the system repositories to ensure we have the latest package information:

sudo apt update && sudo apt upgrade -y

Image1

Installing Go Using apt

Now run the command below to install Go on Ubuntu using the default package manager:

sudo apt install golang-go -y

Image3

The installation process for Golang on Ubuntu may take a while to complete. 

Verify Go Language Installation

Finally, confirm the Golang installation on Ubuntu by checking its version:

go version

The output indicates that the go version go1.22.2 has been installed successfully:

Image2

Uninstall Go from Ubuntu

If Golang is no longer needed, uninstall it from Ubuntu by executing the following command:

sudo apt remove golang-go -y

Image5

It's recommended to remove unnecessary dependent packages, as they can take up extra space. To do this, simply execute the below-given command:

sudo apt autoremove

Image4

Method 2: Installing Golang Using wget

To install the Go programming language on Ubuntu from the official source, users can use the wget command. This command downloads the latest Golang binary package from the official website. 

Let’s go through the step-by-step process below to download and install Golang on Ubuntu using the wget command.

Download and Install Go Language

First, open a browser and navigate to the Go language’s official All Releases page. Scroll down to select an appropriate binary package under the stable version. Copy the link address of the selected binary package:

Image7

Now specify the copied link address with the wget command, as shown below:

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

Here, the -O option is used to specify an output file name. For instance, the above command downloads the Golang binary package and saves it as golang.tar.gz:

Image6

After this, use the tar command to extract the downloaded package:

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

Image9

Set Up the Go Environment

Configuring the PATH variable for Golang allows access to Go commands and tools from any directory on the system. For this purpose, the Go binary paths need to be added to the PATH environment variable:

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

Image8

Next, execute the following command to apply the changes made to the profile file:

source ~/.profile

Image12

Verify Go Installation

Once the Go environment is configured, execute the command below to check if Go has been successfully installed:

go version

Image10

Configure the $GOPATH and $GOROOT (Optional)

In Golang, the default workspace directory is ~/go, and the environment variable $GOPATH refers to this location. However, users can customize this workspace by setting the $GOPATH environment variable in their ~/.profile, ~/.bashrc, or ~/.zshrc files:

export GOPATH=$HOME/golangExamples

This command will set the workspace directory to ~/golangExamples. Moreover, users can also set $GOROOT to override the default Go installation path. However, this is usually not necessary:

export GOROOT=/usr/local/go

Finally, execute the command below to apply the changes:

source ~/.profile

Users can customize the $GOPATH and $GOROOT environment variables, however, it is not recommended to change them unless you have a specific reason.

Remove Go From Ubuntu

If Golang is downloaded using the wget command and now needs to be removed, simply run the command below:

sudo rm -rf /usr/local/go

This command will permanently remove the Go language from Ubuntu. 

How to Use Go Programming Language on Ubuntu?

Once the Go programming language is installed on Ubuntu, we can use it to fulfill our programming needs, such as creating applications, and web services, handling concurrent tasks, etc. 

You can use any text editor, like nano, to create a new file and save it with a .go extension:

nano exampleCode.go

After creating a go file, paste the following code into it to print a "Welcome to hostman.com" message on the terminal:

package main

import "fmt"

func main() {
    fmt.Println("Welcome to hostman.com")
}

Save the code, close the nano editor, and then run the command below in the terminal to execute the Go program:

go run exampleCode.go

The output shows that the "Welcome to hostman.com" message is successfully displayed in the Ubuntu terminal, which confirms that the Golang program has been executed correctly:

Image11

That’s all about installing, configuring, and using the Go programming language on Ubuntu.

Conclusion

Golang is a cross-platform programming language best known for its easy-to-use syntax, concurrency support, and scalability. Key benefits of Golang include fast compilation, simple syntax, and built-in tools like go fmt and go test, etc. These features make Golang a perfect choice for building efficient applications. In this article, we explored two methods to install Golang on Ubuntu. The first method uses the default package manager (apt). The second method involves downloading via wget. Choose any of the discussed methods to install the Go language and start using it to develop scalable applications on Ubuntu.

Go
18.10.2024
Reading time: 6 min

Do you have questions,
comments, or concerns?

Our professionals are available to assist you at any moment,
whether you need help or are just unsure of where to start
Email us