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
- Fetches release metadata from
https://go.dev/dl/?mode=jsonand picks the newest stablelinux/<arch>archive. - Compares it with the installed version, which it reads from
$DIR/VERSIONwithout running the old binary. - Downloads the tarball from
https://dl.google.com/go/into a private staging directory next to the install directory. - Verifies the SHA-256, inspects the archive, extracts it, and checks that
go versionreports the expected version. - Swaps the new tree in by rename. If the swap fails, the old tree is restored.
- Checks that
/usr/bin/goand/usr/bin/gofmtpoint 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
| Option | Description |
|---|---|
-d, --dir DIR | Install directory (default: /usr/local/go) |
-c, --check | Only report versions and link status; change nothing |
-f, --force | Reinstall even if up to date, or downgrade |
-l, --link | Create /usr/bin/go and /usr/bin/gofmt symlinks if missing, or repoint a symlink no package owns |
-h, --help | Show 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
- Downloads: HTTPS only, TLS 1.2 or newer, no redirects to plain HTTP,
~/.curlrcignored, size limits and timeouts. - Checksum: the SHA-256 comes from the go.dev metadata and is verified before the tarball is used.
- Strict validation: the version, filename and hash from the network must
match strict patterns. The filename must be exactly
<version>.linux-<arch>.tar.gz. - Archive inspection: before extracting, the script rejects anything
other than regular files and directories under
go/: no symlinks, hardlinks, device files, absolute paths or..components. - Extraction: unpacks into a
0700staging directory with--no-same-owner, so files end up owned by root. - Version check: the new
go versionruns in a clean environment (env -i,GOTOOLCHAIN=local,GOENV=off). - Other safeguards: fixed
PATH,umask 022, strict mode (set -Eeuo pipefail), and a lock against concurrent runs. As root, it refuses to install if the parent directory isn’t owned by root or is group- or world-writable. - Links:
--linknever overwrites regular files or symlinks that belong to a distro package. It only warns about them.
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