How to Install Go on CentOS 7

How to Install Go on CentOS 7

In this tutorial, we will show you how to install and compile Go, as well a run a basic Go program on a CentOS 7 VPS.

Install Go on CentOS 7

Go, also known as Golang, is an open-source programming language developed by Google. Go provides an easy way to build simple, reliable, and efficient software. Go is a system programming language designed for speed and simplicity. You can compile a single go program and an executable that will run on Windows, macOS, and Linux, without making any changes to your code. This cross-platform compatibility makes Go an attractive option for developers that only want to code once.

Let’s get started with the installation.

Prerequisites

  • A CentOS 7 VPS with root access enabled, or a user with sudo privileges.

Step 1: Log in and Update Packages

First, we’re going to need to log into our server using SSH. You can do that by entering this command:

ssh root@IP_Address -p Port_Number

Remember to replace “root” with your username if you are not using the root user. Change “IP_Address” and “Port_Number” according to your server’s IP address and SSH port number.

Once you are logged in, you should update all of your packages to their latest available versions.

yum update -y

Once all the packages are up-to-date, restart your server to apply the configuration changes.

Step 2: Download and Install Go

First, you will need to download the latest version of the Go tarball from the Go official website. At the time of writing this article, the latest stable version of Go is version 1.12.7.

To download the Go tarball, run the following command:

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

Once the tarball is downloaded, verify the tarball checksum with the following command:

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

You should see an output that looks similar to the one below:

66d83bfb5a9ede000e33c6579a91a29e6b101829ad41fffb5c5bb6c900e109d9

Compare the hash value from the above output to the checksum value on the Go download page. If they match that means the integrity of the file validated and you can proceed with the installation.

Next, extract the downloaded file to the /usr/local directory with the following command:

tar -C /usr/local -xvzf go1.12.7.linux-amd64.tar.gz

Step 3: Edit the Path Variable for Go

Next, we will need to set up the Go path environment variable to execute Go like any other command, no matter where you are in the filesystem.

You can set the environment variable globally by creating a file called go.sh in the /etc/profile.d directory.

nano /etc/profile.d/go.sh

Add the following line:

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

Save and close the file when you have finished.

If you want to set the Go path environment variable for a specific user, then you will need to define Go environment variables in your user’s .bash_profile file.

nano ~/.bash_profile

Add the following lines:

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

Save and close the file. Then, run the source command to reload the updated profiles:

source /etc/profile.d/go.sh
source ~/.bash_profile

Next, verify the Go installation with the following command:

go version

You should see the following output:

go version go1.12.7 linux/amd64

Step 4: Create your First Go Project

Here we will create a sample program in the Go language. First, create a new directory for the Go workspace with the following command:

mkdir $HOME/project

Next, create a new src/test directory inside $HOME/project with the following command:

mkdir -p $HOME/project/src/test

Next, create a simple program (test.go) with the following command:

nano $HOME/project/src/test/test.go

Add the following content:

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

GET YOUR VPS
package main

import "fmt"

func main() {
    fmt.Printf("This is my first Go Program\n")
}

Save and close the file. Then, compile the program with the following command:

cd $HOME/project/src/test/
go build

The above command will generate an executable named test. You can now run the program with the following command:

./test

The output should be similar to the one below:

This is my first Go Program

That’s it! Now you can use Go to code your programs for any platform.

In this tutorial, we learned how to install Go on a CentOS 7 VPS. We also learned how to set up the Go environment variable and create a Go program.


Installing Go on CentOS 7

Of course, you don’t need to do any of this if you use one of our managed Linux VPS Hosting services, in which case you can simply ask our expert Linux admins to install Go on CentOS 7 for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post on how to install Go on CentOS 7, please share it with your friends on the social networks using the buttons below, or simply leave a reply down in the comments section. Thank you.

Leave a Comment