Install Go (Golang) on CentOS 7 / RHEL 7

Posted on 209 views

Go or GoLang, as it is called, is an open-source programming language designed to help ease the development of simple and scalable applications. It was implemented as a product of Google Engineering in 2009. It was referred to as a statically-typed programming language with the ease of use, versatility, and reliability.

Currently, Golang has become one of the most popular programming languages due to its efficiency, clean design, expressive, and concise design model. Some of the projects that make use of Golang are:

  • snappy – a package manager
  • Juju – a service orchestration tool by canonical
  • Docker – a set of tools for deploying containers
  • Dropbox moved the critical components to Go lang from python

The features of the Go programming language are:

  • It is Open-Source
  • It has a powerful Standard Library and Tool Set
  • Concurrency Support – it offers easy and trackable concurrency options.
  • Garbage Collection
  • Testing Capabilities
  • Cross-Platform Development Opportunities
  • Hiighly scalable

This guide offers a systematic demonstration of how to install Go (Golang) on CentOS 7 / RHEL 7.

Step 1 – Update System

First, ensure that your system is up-to-date and packages are to their latest versions. This is achieved using the command below.

sudo yum update

Install the required packages.

sudo yum install wget

Step 2 – Install Go (Golang) on CentOS 7 / RHEL 7

In this guide, I will demonstrate two ways to install Go (Golang) on CentOS 7 / RHEL 7 i.e

  1. Manual installation
  2. Using Golang installer.

Option 1) – Install Go (Golang) on CentOS 7 / RHEL 7 using the Golang Installer.

This is yet another method to get Golang installed on your CentOS 7 / RHEL 7 system. It involves downloading the official Golang Installers for Linux systems as below.

sudo yum -y install wget
wget https://storage.googleapis.com/golang/getgo/installer_linux

With the installer downloaded, make it executable.

chmod +x ./installer_linux

Now run the installer to install the latest Golang version.

./installer_linux 

The installation will proceed as below:

Welcome to the Go installer!
Downloading Go version go1.19 to /home/thor/.go
This may take a bit of time...
Downloaded!
Setting up GOPATH
GOPATH has been set up!

One more thing! Run `source /home/thor/.bash_profile` to persist the
new environment variables to your current session, or open a
new shell prompt.

You will then be required to source the ~/.bash_profile

source ~/.bash_profile

Finally, verify the installed version of Golang.

$ go version
go version go1.19 linux/amd64

Option 2) Install Go (Golang) on CentOS 7 / RHEL 7 Manually.

This installation method requires one to download the latest available Golang package from the official Golang release page.

Alternatively, you can download the tarball using wget as below.

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

With the tarball downloaded, verify the checksum.

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

Sample Output:

464b6b66591f6cf055bc5df90a9750bf5fbc9d038722bb84a9d56a2bea974be6  go1.19.linux-amd64.tar.gz

Verify that the output matches the one on the download page.

Next, extract the tarball.

sudo tar -C /usr/local -xzf go*.linux-amd64.tar.gz

Then proceed and adjust your PATH variable. This will tell your system where to find and execute the GO executable binaries. This can be done by adding the below line to $HOME/.profile for installation on the current user or /etc/profile for the system-wide installation.

##For Current user
$ vi $HOME/.profile 

##For system-wide
$ sudo vi /etc/profile 

In the file, add the below lines.

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

For the changes made to apply, you may be required to log out and log in back to your system or directly execute them from the profile with the command:

source $HOME/.profile
##OR##
source /etc/profile

That it! You have installed Golang on your CentOS 7 / RHEL 7 system.

Now, verify your installation by checking the installed Golang version.

$ go version
go version go1.19 linux/amd64

Step 3 – Test your Go Installation on CentOS 7 / RHEL 7

Here, we want to test our Go installation by setting up a simple workspace then building a simple Hello World program using Golang.

First, create a workspace directory.

mkdir ~/go

Then create a Go file for the Hello World program.

mkdir -p ~/go/src/hello

Now in the directory, create a hello.go file as below.

vi ~/go/src/hello/hello.go

In the created file, add the below lines.

package main

import "fmt"

func main() 
    fmt.Printf("Hello, World! Golang is Amazing!\n")

Build the Go file.

cd ~/go/src/hello 
go mod init
go build

Now execute your program.

./hello

Sample Output:

rhellab$ ./hello
Hello, World! Golang is Amazing!

From the output above, it is safe to assume that our Golang installation is working perfectly.

Conclusion

That was enough learning! I hope you too managed to install Go (Golang) on CentOS 7 / RHEL 7.

coffee

Gravatar Image
A systems engineer with excellent skills in systems administration, cloud computing, systems deployment, virtualization, containers, and a certified ethical hacker.