update_go owl mascot

update_go

Install or update Go to the latest stable release, verified and rolled back safely.

update_go.sh is a hardened Bash script that installs or updates Go to the latest stable Linux release. By default it installs to /usr/local/go. It checks the download before using it, swaps the new tree in by rename, and restores the old tree if the swap fails.

What it does

  1. Fetches release metadata from https://go.dev/dl/?mode=json and picks the newest stable linux/<arch> archive.
  2. Compares it with the installed version, which it reads from $DIR/VERSION without running the old binary.
  3. Downloads the tarball from https://dl.google.com/go/ into a private staging directory next to the install directory.
  4. Verifies the SHA-256, inspects the archive, extracts it, and checks that go version reports the expected version.
  5. Swaps the new tree in by rename. If the swap fails, the old tree is restored.
  6. Checks that /usr/bin/go and /usr/bin/gofmt point to the new install.

Supported architectures: amd64, arm64, armv6l, 386, ppc64le, s390x, riscv64, loong64.

Quick start

Install the dependencies (Debian/Ubuntu):

sudo apt install curl jq

It also needs bash, sha256sum, tar, gzip, flock and coreutils.

./update_go.sh --check     # show installed vs. latest version, check links (no root needed)
sudo ./update_go.sh        # update /usr/local/go to the latest stable release
sudo ./update_go.sh --link # also create /usr/bin/go and /usr/bin/gofmt symlinks if missing
OptionDescription
-d, --dir DIRInstall directory (default: /usr/local/go)
-c, --checkOnly report versions and link status; change nothing
-f, --forceReinstall even if up to date, or downgrade
-l, --linkCreate /usr/bin/go and /usr/bin/gofmt symlinks if missing, or repoint a symlink no package owns
-h, --helpShow help

Root is only needed when the install directory’s parent (or /usr/bin with --link) isn’t writable by the current user. For example, this works without sudo:

./update_go.sh --dir "$HOME/sdk/go"

Security measures

Limitation: the script doesn’t verify Google’s GPG signature (.asc). Trust rests on the checksum, fetched over HTTPS from go.dev. That is the official method, but there is no second, independent check.

Why check /usr/bin/go?

An interactive shell usually finds /usr/local/go/bin or /usr/local/bin first, but sudo (via its secure_path setting), cron, systemd units and other tools with a minimal PATH use /usr/bin/go. So that link decides which Go those programs get.

If the distro package golang-go is installed, it owns /usr/bin/go and /usr/bin/gofmt. A package upgrade will quietly point them back at the distro’s older Go. The script detects this and warns. The cleanest fix is to remove the distro packages and let the script create the links:

sudo apt purge golang-go golang-src 'golang-1.*-go' 'golang-1.*-src' golang-any
sudo ./update_go.sh --link

Check first that nothing else you use depends on those packages.

Running it automatically

Example root crontab entry (sudo crontab -e) that checks weekly:

0 4 * * 1  /path/to/update_go.sh >/var/log/update_go.log 2>&1