iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🦘

From Ubuntu 24.04, `apt install golang` is enough for Go environment setup

に公開

About Go Toolchains

Since Go 1.21, Go Toolchains has been introduced, making it unnecessary to manage Go versions manually. Simply put, Go Toolchains is a feature that automatically installs the version specified in a project if it is newer than the version you have locally.

Previously, since the version of Go available via Apt (Ubuntu's package manager) was usually several versions older than the latest, it was common to manually install from the official source (which is the most reliable but a bit tedious), use a PPA, or manage versions using goenv or asdf.

In Ubuntu 24.04, the version of Go available via Apt is 1.22. While this is also older compared to the current latest version 1.23, now that Go Toolchains is available, there's nothing to fear. In other words, just like curl or vim, your Go environment can be set up with just sudo apt install golang.

Verification

We will verify this using a Docker image for Ubuntu. We specify Go 1.23.0 in /app/go.mod. Note that while I mentioned that the environment can be set up with just apt install golang, since CA certificates are required to download the binary from go.dev, we will also install ca-certificates.

/app/go.mod
module playground

go 1.23.0

Ubuntu 22.04 (Before)

$ docker run -it ubuntu:22.04 bash

root@9e59b594fe3d:/# apt update
root@9e59b594fe3d:/# apt install -y golang ca-certificates

root@9e59b594fe3d:/# go version
go version go1.18.1 linux/amd64

root@9e59b594fe3d:/# mkdir /app
root@9e59b594fe3d:/# cd /app
root@9e59b594fe3d:/app# echo "module playground
go 1.23.0" > /app/go.mod

root@9e59b594fe3d:/app# go version
go version go1.18.1 linux/amd64

The old Go version installed via Apt is also used within the /app directory.

Ubuntu 24.04 (After)

$ docker run -it ubuntu:24.04 bash

root@89c21fc27ed0:/# apt update
root@89c21fc27ed0:/# apt install -y golang ca-certificates

root@89c21fc27ed0:/# go version
go version go1.22.2 linux/amd64

root@89c21fc27ed0:/# mkdir /app
root@89c21fc27ed0:/# cd /app
root@89c21fc27ed0:/app# echo "module playground
go 1.23.0" > /app/go.mod

root@89c21fc27ed0:/app# go version
go: downloading go1.23.0 (linux/amd64)
go version go1.23.0 linux/amd64

When you move to the /app directory where go.mod exists and run the go command, you can see that the new version of Go is automatically installed.

(Bonus) Script to use PPA for versions below 24.04

# /etc/os-release contains the Ubuntu version information
source /etc/os-release
# The bc command evaluates strings as mathematical expressions
if [[ $(echo "$VERSION_ID < 24.04" | bc) = 1 ]]; then
  sudo apt-add-repository -y ppa:longsleep/golang-backports
  sudo apt-get update
fi
sudo apt-get install -y golang

Summary

  • Go version management is no longer necessary starting from Go 1.21
  • Ubuntu 24.04 provides Go 1.22 via Apt
  • In short, managing Go with just Apt is now perfectly sufficient!

Discussion