custom background image

Comment installer Go sur Ubuntu 22.04 ?


Installer et configurer Go sur Ubuntu 22.04

Objectif

Go est l'un des langages les plus utilisés dans le monde. Sa large adoption au cours des dernières années en fait un langage incontournable dans l'univers du développement et des opérations web. Pour en savoir plus sur les capacités du langage Go, consultez la documentation officielle.

Dans ce tutoriel, nous allons apprendre à installer Go sur le système de distribution Linux Ubuntu 22.04.

 

Exigences

Pour suivre ce tutoriel, vous devez disposer de la version 22.04 d'Ubuntu et l'exécuter sur une Instance Compute OVHcloud, par exemple. Quelques connaissances de base sur son fonctionnement sont également nécessaires. Si vous ne disposez pas d'Ubuntu 22.04, suivez ce guide pour utiliser une Instance Compute OVHcloud.

 

Instructions

Dans ce tutoriel, vous allez commencer par installer Go. Ensuite, vous l'exécuterez puis vous finirez par apprendre à basculer entre les différentes versions installées.

Au moment de la rédaction de ce tutoriel, la dernière version LTS de Go est la version 1.18.x.

 

Installation de Go

Pour installer Go sur Ubuntu, le moyen le plus simple est d'utiliser la commande apt-get :

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

Le résultat s'affiche comme ceci :

$ 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.

Ensuite, vous pouvez tester votre nouvelle installation :

go version

Le résultat s'affiche comme ceci :

$ go version
go version go1.18.1 linux/amd64

Go vous permet de gérer plusieurs versions installées. Par exemple, si vous souhaitez installer la version 1.17 :

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

Le résultat s'affiche comme ceci :

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

La commande go télécharge le binaire go1.17 dans le dossier ~/go/bin.

Vous pouvez ensuite utiliser ce binaire pour installer la version 1.17 :

~/go/bin/go1.17 download

Le résultat s'affiche comme ceci :


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

Votre nouvelle installation de Go se trouve dans le dossier /home/ubuntu/sdk/go1.17.

Vous pouvez mettre à jour votre variable d'environnement path si vous souhaitez utiliser cette version :

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

Le résultat s'affiche comme ceci :


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

 

Tester l'installation de Go

Pour tester votre installation de Go, vous pouvez écrire une application Hello World. Créez un fichier helloworld.go et copiez le code suivant :

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

Sauvegardez et exécutez :

go run helloworld.go

Le résultat s'affiche comme ceci :


$ go run helloworld.go 
👋 Hello World.

Félicitations, vous venez d'installer et de configurer Go sur Ubuntu 22.04 !

 

Aller plus loin

Consultez les offres d'instance Public Cloud chez OVHcloud.