UGC bg

How to install Go on Centos 7 ?


Install Go on Centos 7

Objective

Go is one of the most famous language in the world. Its wide adoption in the past years makes it an unavoidable language in the development as Ops world. To have more information and the capabilities of the Go language see the official documentation.

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

 

Requirements

This tutorial assume that you have an Centos 7, running in an OVHcloud Compute Instance for example, and some basic knowledges of how to operate it. If you don’t have a running Centos 7, follow the guide to use an OVHcloud Compute Instance.

 

Instructions

In this tutorial, you will, first, install Go, then you will use it and to finish you learn how to switch between different installed versions.

At the time of writing this tutorial, the last LTS release of Go is 1.18.x.

 

Installation of Go

To install Go, you will download and extract the latest package in 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 ti yout PATH:

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

Verify that everything is OK:

go version

Output should be like:

$ go version
go version go1.18.2 linux/amd64

Go allow you to manage multiple installed versions. For example, to install the version 1.17:

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

Output should be like this:

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

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

Next, you can use this binary to install the version 1.17:

~/go/bin/go1.17 download

Output should be like this:

$ ~/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'

Your fresh installation of Go is in the 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 should be like this:

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

 

Test Go installation

To test your Go installation, you can write an Hello World application. Create a helloworld.go file and past the following code:

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

Save and run it:

go run helloworld.go

Output should be like this:

$ go run helloworld.go 
👋 Hello World.

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

 

Go further

Check the offers of public cloud instance on OVHcloud.