How to install Go on Centos 7


Install Go on Centos 7

Objective

Go is one of the most famous programming languages in the world. Its wide adoption over the past years makes it an unavoidable language in the development world. To learn more about the full capabilities of the Go language refer to the official documentation.

In this tutorial you will learn how to install Go on a Centos 7 Linux distribution.

 

Requirements

This tutorial assumes that you have a VPS, bare metal server, or as in our case, an OVHcloud Compute Instance running CentOS 7. You should also have basic knowledge of the command line. If you need help setting up a Public Cloud instance with CentOS 7, follow the guide to use an OVHcloud Compute Instance.

 

Instructions

In this tutorial, you will install Go, use it, and learn how to switch between several installed versions.

At the time of writing this tutorial, the latest LTS release of Go was 1.18.x.

 

Installation of Go

To install Go, download and extract the latest package into the /usr/local folder.

Download the latest Go package:

cd /usr/local sudo curl https://dl.google.com/go/go1.18.2.linux-amd64.tar.gz --output go1.18.2.linux-amd64.tar.gz

$ sudo curl https://dl.google.com/go/go1.18.2.linux-amd64.tar.gz --output go1.18.2.linux-amd64.tar.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  135M  100  135M    0     0  27.3M      0  0:00:04  0:00:04 --:--:-- 28.5M

Extract the archive:

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

Add the /user/local/go/bin to yout PATH:

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

Verify the installation:

go version

Output:

$ go version
go version go1.18.2 linux/amd64

Go allows you to manage multiple installed versions. For example, you can also install version 1.17:

go install golang.org/dl/go1.17@latest

Output:

$ go install golang.org/dl/go1.17@latest
go: downloading golang.org/dl v0.0.0-20220510203206-88ea6714b1d9

The go command downloads the binary go1.17 in folder: ~/go/bin.

This binary can be used to install version 1.17:

~/go/bin/go1.17 download

Output:

$ ~/go/bin/go1.17 download
Downloaded   0.0% (    16384 / 134787877 bytes) ...
Downloaded  17.1% ( 23101264 / 134787877 bytes) ...
Downloaded  39.2% ( 52870768 / 134787877 bytes) ...
Downloaded  61.3% ( 82623888 / 134787877 bytes) ...
Downloaded  83.4% (112393392 / 134787877 bytes) ...
Downloaded 100.0% (134787877 / 134787877 bytes)
Unpacking /home/centos/sdk/go1.17/go1.17.linux-amd64.tar.gz ...
Success. You may now run 'go1.17'

The last installation of Go is located in folder: /home/centos/sdk/go1.17.

You can update your path environment variable if you want to use this version:

export PATH=/home/centos/sdk/go1.17/bin:$PATH go version

Output:

$ export PATH=/home/centos/sdk/go1.17/bin:$PATH
$ go version
go version go1.17 linux/amd64

 

Test Go installation

Test your Go installation by writing a Hello World application. Create a helloworld.go file and paste the following code inside:

package main import "fmt" func main() { fmt.Println("👋 Hello World.") }

Save and run it:

go run helloworld.go

Output:

$ go run helloworld.go 
👋 Hello World.

That’s it, you have successfully installed and configured Go on Centos 7.