UGC bg

How to install Go on Ubuntu 22.04 ?


Install and configure Go on Ubuntu 22.04

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 Ubuntu 22.04 Linux distribution.

 

Requirements

This tutorial assume that you have an Ubuntu 22.04, running in an OVHcloud Compute Instance for example, and some basic knowledges of how to operate it. If you don’t have a running Ubuntu 22.04, 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 on Ubuntu, the easiest way is to use apt-get command:

sudo apt-get update && sudo apt-get -y install golang-go

Output should be like this:

$ sudo apt-get update && sudo apt-get -y install golang-go
Hit:1 http://nova.clouds.archive.ubuntu.com/ubuntu jammy InRelease
Hit:2 http://nova.clouds.archive.ubuntu.com/ubuntu jammy-updates InRelease
Hit:3 http://nova.clouds.archive.ubuntu.com/ubuntu jammy-backports InRelease
Hit:4 http://security.ubuntu.com/ubuntu jammy-security InRelease
Ign:5 https://pkg.jenkins.io/debian-stable binary/ InRelease
Hit:6 https://pkg.jenkins.io/debian-stable binary/ Release
Reading package lists... Done
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  bzip2 cpp cpp-11 g++ g++-11 gcc gcc-11 gcc-11-base golang-1.18-go golang-1.18-src golang-src libasan6 libatomic1 libc-dev-bin libc-devtools libc6-dev libcc1-0
  libcrypt-dev libdpkg-perl libfile-fcntllock-perl libgcc-11-dev libgd3 libgomp1 libisl23 libitm1 liblsan0 libmpc3 libnsl-dev libquadmath0 libstdc++-11-dev libtirpc-dev
  libtsan0 libubsan1 linux-libc-dev manpages-dev pkg-config rpcsvc-proto
Suggested packages:
  bzip2-doc cpp-doc gcc-11-locales g++-multilib g++-11-multilib gcc-11-doc gcc-multilib autoconf automake libtool flex bison gdb gcc-doc gcc-11-multilib bzr | brz
  mercurial subversion glibc-doc debian-keyring bzr libgd-tools libstdc++-11-doc dpkg-dev
The following NEW packages will be installed:
  bzip2 cpp cpp-11 g++ g++-11 gcc gcc-11 gcc-11-base golang-1.18-go golang-1.18-src golang-go golang-src libasan6 libatomic1 libc-dev-bin libc-devtools libc6-dev libcc1-0
  libcrypt-dev libdpkg-perl libfile-fcntllock-perl libgcc-11-dev libgd3 libgomp1 libisl23 libitm1 liblsan0 libmpc3 libnsl-dev libquadmath0 libstdc++-11-dev libtirpc-dev
  libtsan0 libubsan1 linux-libc-dev manpages-dev pkg-config rpcsvc-proto
0 upgraded, 38 newly installed, 0 to remove and 14 not upgraded.
Need to get 143 MB of archives.
After this operation, 634 MB of additional disk space will be used.
...
Scanning processes...                                                                                                                                                       
Scanning candidates...                                                                                                                                                      
Scanning linux images...                                                                                                                                                    

Restarting services...
Service restarts being deferred:
 systemctl restart systemd-logind.service

No containers need to be restarted.

No user sessions are running outdated binaries.

No VM guests are running outdated hypervisor (qemu) binaries on this host.

Next, you can test your fresh installation:

go version

Output should be like this:

$ go version
go version go1.18.1 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  37.7% ( 50822784 / 134787877 bytes) ...
Downloaded  82.8% (111606960 / 134787877 bytes) ...
Downloaded 100.0% (134787877 / 134787877 bytes)
Unpacking /home/ubuntu/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/ubuntu/sdk/go1.17.

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

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

Output should be like this:


$ export PATH=/home/ubuntu/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 Ubuntu 22.04.

 

Go further

Check the offers of public cloud instance on OVHcloud.