Skip to main content

How to Install Go on CentOS

Go, also known as Golang, is a powerful open-source programming language developed by Google. It is designed to be efficient, concise, and highly performant, making it an excellent choice for building scalable and reliable applications. In this tutorial, we will walk you through the step-by-step process of installing Go on CentOS.

Prerequisites

Before you begin, ensure that you have the following prerequisites:

  • A CentOS server with root access or a user with sudo privileges.
  • A stable internet connection.

Step 1: Update System Packages

First, let's update the system packages to their latest versions. Open your terminal and run the following command:

sudo yum update -y

This command will update all the installed packages on your CentOS system.

Step 2: Download and Extract Go

Go has official binary distributions available for various operating systems, including CentOS. To download the latest stable version of Go, navigate to the official Go downloads page in your web browser. You can find it at https://golang.org/dl/.

Look for the version of Go that matches your system architecture. For example, if you're using a 64-bit CentOS, click on the download link for the Linux tar.gz archive.

Once the download is complete, open your terminal and navigate to the directory where the downloaded file is located. Use the cd command to change directories. For example, if you downloaded the file to your Downloads folder, run:

cd ~/Downloads

Next, extract the downloaded archive using the tar command:

sudo tar -C /usr/local -xzf go<version>.linux-amd64.tar.gz

Replace <version> in the command above with the actual version number of the Go package you downloaded. For example, if you downloaded Go version 1.17, the command would be:

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

This command will extract the Go binaries and related files into the /usr/local directory.

Step 3: Set Go Environment Variables

To use Go effectively, you need to set up a few environment variables. These variables include GOROOT, GOPATH, and PATH.

Open your preferred text editor and open the system-wide profile file /etc/profile:

sudo vi /etc/profile

Add the following lines at the end of the file:

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

Save the file and exit the text editor.

Next, apply the changes to the current session by running the following command:

source /etc/profile

This will make the environment variables available for the current user. Alternatively, you can log out and log back in to the system.

Step 4: Verify the Installation

To verify that Go has been installed successfully, open a new terminal session and run the following command:

go version

You should see the installed Go version displayed in the output. For example:

go version go1.17 linux/amd64

Congratulations! You have successfully installed Go on your CentOS system.

Step 5: Test Go Installation

Now, let's write a simple Go program to test the installation. Create a new file named hello.go using your preferred text editor:

vi hello.go

Add the following code to the file:

package main

import "fmt"

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

Save the file and exit the text editor.

To compile and run the Go program, execute the following command:

go run hello.go

You should see the following output:

Hello, Go!

If you see the expected output, then your Go installation is working correctly.

Conclusion

In this tutorial, you learned how to install Go on CentOS step by step. You updated the system packages, downloaded and extracted the Go binaries, set up the necessary environment variables, verified the installation, and tested Go by running a simple program. Now you are ready to start developing applications using the Go programming language.

Remember to regularly update your Go installation to benefit from the latest features and bug fixes. Happy coding with Go!