How to Install, Update, and Run Go on Linux

Linux TLDR
Last Updated:
Reading time: 5 minutes

Go, often referred to as Golang, is an open-source programming language created by Google. It was designed by Robert Griesemer, Rob Pike, and Ken Thompson and first introduced in 2009.

Go (Golang) is a flexible programming language that can be used in a wide range of applications and software, like:

  • Web Applications
  • Command-Line Tools
  • Network Applications
  • Microservices
  • Data Processing and Pipelines
  • Distributed Systems
  • System Tools and Utilities
  • Blockchain and cryptocurrency applications
  • DevOps tools, and many more.

Isnโ€™t it fascinating? Once you explore the features section, youโ€™ll understand the true power of this tool. Additionally, this article provides practical examples for installing Go (Golang) on your preferred Linux distribution.

Tutorial Details

DescriptionInstallation of Go (Golang) with Practical Examples
Difficulty LevelModerate
Root or Sudo PrivilegesYes
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisitesโ€“
Internet RequiredYes

Key Features

Until now, you know what you can do with Go (Golang), but the whole fame of this application depends on the following features:

  • Simplicity
  • Concurrent and Efficient (utilizes goroutines (lightweight threads) and channels to make programs efficiently utilize modern multicore processors)
  • Fast Compilation
  • Static Typing (variable types are checked at compilation, enhancing type safety and catching errors early in the development process)
  • Garbage Collection (automatically manages memory allocation and deallocation)
  • Built-in Tools
  • Cross-platform
๐Ÿ˜ฎ
Popular open source projects like Docker, Kubernetes, Lime, InfluxDB, and Gogs (Go Git Service) were developed using Go.

Letโ€™s roll to the next section to learn how to install this amazing tool on your preferred Linux distribution.

How to Install Go (Golang) on Linux

Go (Golang) can be easily installed from the distributionโ€™s default package manager, but the default repositories might be outdated. So, it is always recommended to install the most recent version of Go from the source code.

Although, I will show you the installation steps for both ways: package manager and source code, starting with:

Installing Go using Package Managers

Go (Golang) is available in the default repositories (but might be outdated) of most modern Linux distributions and can be easily installed using the distributionโ€™s default package manager.

The following are examples of Go installations on some well-known Linux distributions:

$ sudo apt install golang -y                                                                                #Debian, Ubuntu, or Pop!_OS
$ sudo dnf install golang -y                                                                                #Fedora 
$ sudo yum install golang -y                                                                              #Red Hat, CentOS
$ sudo pacman -Sy go                                                                                        #Arch, Manjaro, or EndeavourOS 
$ sudo zypper install golang -y                                                                          #openSUSE

Once the installation has successfully completed, check the Go version by using the following command:

$ go version

Output:

Checking the Go version installed from the package manager

As you can see from the above picture, the โ€œGo 1.18.1 versionโ€ is installed in the system, but while I am writing this article, the โ€œGo 1.21.0 versionโ€ is out and can be easily installed from source.

Installing Go from Source

To install Go from source on your Linux system, follow these steps:

1. First, visit the official Go website (https://go.dev/dl/), find and download the latest stable version of Go. For example, while I was writing this article, โ€œGo 1.21.0โ€ was the latest version. For that, Iโ€™ll download the following package:

Downloading Go latest package

Ensure the package is suitable for your architecture and operating system. However, if you want to follow this guide, simply execute the following command to download โ€œGo 1.21.0โ€ using the wget command:

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

2. Now, navigate to the directory where you downloaded the Go tarball. If you are already there, proceed to extract it using the below command, directing the files to the โ€œ/usr/local/โ€ path.

๐Ÿ“
The following command will also remove the previous Go installation by deleting the โ€œ/usr/local/goโ€ directory (if it exists).
$ sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xzf go*.linux-amd64.tar.gz

3. To make Go accessible from anywhere, you need to add the following line to your โ€œ/etc/profileโ€ (for system-wide installation) or โ€œ~/.profileโ€ (for user-specific installation).

export PATH=$PATH:/usr/local/go/bin

Then save the file and reload the shell configuration file by restarting the terminal session, or execute the following source command if you added the above line in the user-specific configuration file:

$ source ~/.profile

4. Now, you need to create the workspace directory and set the values for the โ€œGOROOTโ€œ, โ€œGOPATHโ€ and โ€œGOBINโ€ Go environment variables in your user profile.

So, first create the Go workspace by creating a โ€œ~/go_projectsโ€ directory, which is the root of your workspace. Inside the workspace, there will be three more directories, namely:

  1. bin: Contains Go executable binaries.
  2. src: Contains Go source files.
  3. pkg: Contains package objects.

Execute the following command to create the workspace, including the aforementioned directory tree:

$ mkdir -p ~/go_projects/{bin,src,pkg}
$ cd ~/go_projects
$ ls

Output:

Creating the Go workspace directory

5. Then, set values for the โ€œGOROOTโ€œ, โ€œGOPATHโ€ and โ€œGOBINโ€ Go environment variables in your user profile (ref: โ€œ~/.profileโ€œ) pointing towards your workspace directory.

export GOROOT=/usr/local/go
export GOPATH="$HOME/go_projects"
export GOBIN="$GOPATH/bin"

6. Lastly, save the file and reload the changes made in the shell configuration file by restarting the terminal session or executing the following command:

$ source ~/.profile

7. Once you have perfectly completed all the aforementioned steps, execute the following command to view your Go version and environments:

$ go version
$ go env

Output:

Checking the Go version and environment variables

Testing Go (Golang) with a Sample Program

To test if the Go installation is working correctly, letโ€™s create a basic โ€œHello, World!โ€ Go program. So, follow the following steps one-by-one:

๐Ÿ“
Follow the below steps only if you have installed Go from source.

1. First, create a hello project directory under the โ€œ~/go_project/src/โ€ path.

$ mkdir -p ~/go_projects/src/hello

2. Then, using your preferred text editor, create the โ€œhello.goโ€ file:

$ vim ~/go_projects/src/hello/hello.go

3. Now copy and paste the following content into the aforementioned file (ref: โ€œhello.goโ€œ):

package main

import "fmt"

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

4. Now, compile the above program using โ€œgo installโ€ and run it:

$ go install ~/go_projects/src/hello/hello.go
$ $GOBIN/hello

Output:

Compiling and running the Go (GoLang) program

Congratulations!!! You have successfully installed and run your first Go program on Linux. Note that Go also provides you with some useful built-in tools that you can use, like:

  • go build: Compile Go programs.
  • go test: Run tests for your Go code.
  • go fmt: Format your Go code.
  • go get: Fetch and install Go packages from the internet.
  • go doc: Access documentation for Go packages.

And if you want to run Go binary executables like other Linux commands, then add โ€œ$GOBINโ€ to your $PATH environment variable.

But when you do so, ensure the program name does not conflict with the Linux built-in commands, like โ€œlsโ€œ, as Linux already uses the ls command to list files and directories.

Updating Go (Golang) Installed from Source

If you have installed Go using the systemโ€™s default package manager, you can simply update your system. However, if you have installed Go from source, then you only have to follow the 1st and 2nd steps from the Installing Go from Source section.

Whereas, the first step involved visiting the Go website (https://go.dev/dl/) and downloading the latest Go version suitable for your architecture and operating system.

Then, execute the following command to remove the existing Go installation by deleting the โ€œ/usr/local/goโ€ directory (if it exists) and extracting the latest downloaded tarball, directing the files to the โ€œ/usr/local/โ€ path.

$ sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xzf go<VERSION>.linux-<ARCH>.tar.gz

How to Remove Go (Golang) from Linux

To remove Go from your Linux system, you can either use the distributionโ€™s default package manager (if Go was installed using the package manager) or manually remove the Go installation directory (if Go was installed from source).

Removing Go installed using the distribution default package manager:

$ sudo apt remove golang -y                                                                               #Debian, Ubuntu, or Pop!_OS
$ sudo dnf remove golang -y                                                                               #Fedora 
$ sudo yum remove golang -y                                                                              #Red Hat, CentOS
$ sudo pacman -Rs go                                                                                           #Arch, Manjaro, or EndeavourOS 
$ sudo zypper remove golang -y                                                                         #openSUSE

Removing Go installed from source:

$ sudo rm -rf /usr/local/go && rm -rf ~/go_projects

Then remove the following environment variables from the shell configuration file where you added them during installation:

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

Final Word

I hope you find this article useful, and if you find something missing or have a suggestion to add something to this article, then do let us know via the comment section.

Till then, peace!

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.