This guide covers doing a system-wide Go install on a Linux machine and, more importantly, configuring it so you can install Go modules to be run as part of the user’s PATH
.
Do the install
First, we’re going to more-or-less follow the instructions at https://go.dev/doc/install, but we’re going to replace /usr/local
with /opt
(more about this at the end) and include sudo
. Follow the Download link there to get the latest version of Go for Linux.
Once downloaded, cd
to the location of that file and enter the following, being sure to remove any other old go
tarballs in the directory first:
sudo rm -rf /opt/go && sudo tar -C /opt -xzf go1.*.linux-amd64.tar.gz
On a fresh install, the rm
command is not needed, but it’s handy when upgrading to a new Go version. If you’ve previously installed to /usr/local
, you can clean that up now.
Now add the following to your /etc/profile
for a system-wide install:
# system-wide Go install
export PATH="$PATH:/opt/go/bin"
You’ll need to use sudo
to modify this file.
Add Go modules to your user’s PATH
In ~/.profile
, add the following:
# set PATH so it includes GOPATH/bin if it exists
if [ -x "$(command -v go)" ] && [ -d "$(go env GOPATH)/bin" ]; then
PATH="$(go env GOPATH)/bin:$PATH"
fi
This lets us easily use modules installed via go install
. Note that installed modules will only be accessible for the current user.
You can experiment in the current terminal by reloading your profile
files with . /etc/profile
and . ~/.profile
, or just restart.
At this point, you can do go version
and you’ll be presented with something like go version go1.24.1 linux/amd64
.
In practice: installing Govulncheck
govulncheck
is a tool that will scan your code for potential vulnerabilities using a vulnerability database. Let’s go ahead and install that:
go install golang.org/x/vuln/cmd/govulncheck@latest
Now you can check your code from your module’s root directory:
govulncheck ./...
or from some other directory:
govulncheck -C ../to/my/module/.. ./...
Updating Go
Installing a new version of Go is super simple:
- Download the latest version from https://go.dev
- In a terminal,
cd
to the folder with the file you downloaded, and delete any previously downloadedgo1*.tar.gz
files - Do
sudo rm -rf /opt/go && sudo tar -C /opt -xzf go1.*.linux-amd64.tar.gz
- Profit 💰 💰 💰
Why /opt
and not /usr/local
?
Files in /usr/local
are expected to be split into bin
and lib
folders, with only /usr/local/bin
being added to the PATH
. In contrast, /opt
is specifically for software like Go that is packaged in a single folder, with its bin
folder being manually added to the PATH
if so desired.
A more detailed explanation can be found over at Baeldung.
And that about does it! Please send any gotchas or tips you have here.