How to install Go on Ubuntu 22.04


Install and configure Go on Ubuntu 22.04

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

 

Requirements

This tutorial assumes that you have a VPS, bare metal server, or as in our case, an OVHcloud Compute Instance running Ubuntu 22.04. You should also have basic knowledge of the command line. If you need help setting up a Public Cloud instance with Ubuntu 22.04 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 on Ubuntu, use the apt-get command:

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

Output:

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

Verify installation:

go version

Output:

$ go version
go version go1.18.1 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  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'

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/ubuntu/sdk/go1.17/bin:$PATH go version

Output:


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