Log In

Installing Go on CentOS

Installing Go on CentOS
Reading time: 6 min
Hostman Team
Technical writer

CentOS is an open-source operating system based on the source code of the Red Hat Enterprise Linux project. At its core, CentOS is based on the Linux kernel.

Novice users may need clarification about the CentOS versioning since Red Hat produces two distributions of this operating system, CentOS Linux and CentOS Stream.

So, we will first look at their differences and then describe how to install Go on CentOS. 

CentOS distributions

CentOS is originally based on the commercial Red Hat Enterprise Linux (RHEL) distribution but differs from it in that it is distributed as free software and has no official support.

For this reason, it is sometimes assumed that RHEL is used as a corporate product in large companies, while small development teams prefer CentOS.

During the entire existence of CentOS, nine versions have been released, the last of which represented a separate branch: CentOS Stream.

The thing is, when the company released version 8 of CentOS, most developers were still using version 7. Around the same time, Red Hat began the process of merging with IBM and decided to stop supporting version 8 in 2021. However, because of the widespread use of version 7, Red Hat decided to continue releasing critical updates until 2024.

It was version 8 that was followed by CentOS Stream in 2019, which is released on a rolling-release model. In other words, updates for this distribution are released regularly and continuously.

The latest version of CentOS is still numbered 9 (that is, the next version after 8), but instead of "Linux", "Stream" is specified. At the same time, the Stream branch is also in version 8 of CentOS.

In this article, we will look at installing the Golang programming language on the latest versions of CentOS which are 8 and 9, but you can also use this guide to install Go on CentOS 7.

1. Uninstall old versions of Golang

Check for older versions

To avoid possible problems during installation, you must ensure that Golang is not present on your system. There are several ways to do this.

You can try to request the Go version through a special command:

go version

If Go is indeed already installed, the console will display a message with the Golang version.

Another option, which can also help with uninstallation, is to find out the location of the directory with Go that you want to uninstall later:

which go

If Golang already exists on the system, the path to the directory will appear in the console.

Uninstall Go

If Golang is indeed already installed, then it needs to be removed:

rm -rf /usr/local/go

If the which go command showed another directory, specify it in the command above. However, it is usually /usr/local/go.

The rm command deletes directories and files, while the -rf flag indicates the recursive-forced type of deletion.

  • r stands for recursive, which means it deletes the specified folder, all its subfolders, subfolders of subfolders, etc.
  • f stands for force, meaning no external conditions or variables can prevent the deletion.

Clear environment variables

To remove Go from the environment variables, you need to locate one of the following files:

  • /etc/profile
  • ~/.profile

In the file, remove the lines that point to the Go directory. Usually, the lines are these: 

export PATH=$PATH:/usr/local/go/bin
export GOROOT=$HOME/go1.X
export PATH=$PATH:$GOROOT/bin

If any of the above lines are present in the file, delete them.

Now you can proceed to downloading Golang.

2. Download Golang

Go to the official Golang website. To download the compiler, you can use wget or curl. We will use the first option, directly pointing the link to the archive we need:

wget https://dl.google.com/go/go1.21.3.linux-amd64.tar.gz

Note that the link specifies the exact version of Go. You can find the number of the latest version for the Linux kernel-based operating systems on the Downloads page.

The wget command launches a Linux utility that downloads files from the Internet via a command line terminal. It works with all major protocols: HTTP, HTTPS, and FTP.

After executing this command, the archive will be downloaded. In our case, it is go1.21.3.linux-amd64.tar.gz; you may have a different version. 

After downloading the archive, check its checksum and make sure that the hash displayed in the command line matches the hash specified on the download page:

sha256sum go1.21.3.linux-amd64.tar.gz

Note that the .tar.gz extension indicates the compressed archive file format, which is a combination of two other archive formats:

  • TAR. Used for storing multiple files in a single archive
  • GZIP. Used to compress the data in an archive to reduce its size

After you download the archive, you will need to extract it. 

3. Extract the archive

For extraction, use the tar utility:

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

When extracting, specify the default directory /usr/local. This is where the Golang compiler will be placed. The extract command should be run as root or a sudo user.

Now, we need to tell the system where the compiler is located. To do this, we will set the environment variables.

4. Set environment variables

In order for the command line terminal to know where to look for Go executables when we compile a program, we need to specify the address of the Golang directory in the environment variables.

To do this, open the /etc/profile file and add the following lines to it:

export PATH=$PATH:/usr/local/go/bin
export GOROOT=/usr/local/go
export GOPATH=$HOME/Documents/go

We have set 3 environment variables:

  • PATH. Helps the command line find the compiler binaries
  • GOROOT. Points to the directory where Go is installed
  • GOPATH. Contains the address of the working directory

For the changes to apply, you can either restart the command line terminal or enter the updated data manually with a special command:

source /etc/profile

5. Check the installation

Now, let's check that Golang was installed correctly and can be accessed from the console terminal. To do this, use the command:

go version

The terminal should display the message with Golang version number.

That's it. We have installed Go on CentOS.

6. Compiling a simple program

We will try running a simple application to make sure that everything works as it should.

Let's create a new file in the home directory:

nano main.go

Then fill it with the following contents:

package main
import "fmt"
func main() {
     fmt.Println("Hello, World!") // CONCLUSION: Hello, World!

Now let's run our program using the run command:

go run main.go

To create a full-fledged executable file, we need another command:

go build

If you do not specify a file name as an argument, the command will compile main.go.

However, you can explicitly specify the name of the file to be built into the final "build":

go build filename.go

Without explicitly specifying a filename in the build command, an error may occur due to the way Go's module system works:

go: go.mod file not found in current directory or any parent directory; see 'go help modules'

There are two ways to solve this error. One is to manually initialize go.mod file in the working directory of the project, specifying the name of the source file:

go mod init filename.go

Another is to change the value of the GO111MODULE environment variable to auto:

go env -w GO111MODULE=auto

Or to off:

go env -w GO111MODULE=off

The build itself adds the system-dependent auxiliary code needed to run the application on any computer with the same system architecture to the executable.


Since CentOS is based on the Linux kernel, installing Golang is the same process for all recent versions of the operating system: 7, 8, 9 (Stream).

You can learn more about the language syntax in the official Golang documentation.