Smarter Go Installation with a Version Variable in One-Liner Bash Command
When you’re managing multiple systems or setting up developer environments frequently, even a simple Go installation script can benefit from a small optimization—especially when it involves repetition of version numbers.
Let’s break down an enhanced version of a common one-liner used to install Go on Linux, and why using a variable is the better way.
🔧 The Original Command
You might often see or use this:
wget https://go.dev/dl/go1.24.4.linux-amd64.tar.gz && sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xzf go1.24.4.linux-amd64.tar.gz && go version && rm go1.24.4.linux-amd64.tar.gz
It works just fine—but the version number is repeated three times, which means if you want to install a different version next time, you need to change it in three places.
This is error-prone and not elegant.
✅ The Improved Version: Use a Version Variable
Let’s introduce a version variable to clean this up:
VER=1.24.4 && wget https://go.dev/dl/go$VER.linux-amd64.tar.gz && sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xzf go$VER.linux-amd64.tar.gz && /usr/local/go/bin/go version && rm go$VER.linux-amd64.tar.gz
🔍 What Changed and Why It’s Better:
VER=1.24.4
at the start makes it easy to change the version once.- All
go$VER.linux-amd64.tar.gz
references become dynamic. - Using
/usr/local/go/bin/go version
ensures we’re testing the newly installed Go version, even if the current shell hasn’t reloaded$PATH
.
🧠 Why Use an Absolute Path for go version
?
When Go is installed to /usr/local/go
, it’s usually not in your PATH until a new shell is opened or your profile (~/.bashrc
, ~/.zshrc
, etc.) sources it.
By using:
/usr/local/go/bin/go version
…you ensure that the version check points directly to the new binary, not an older version in your PATH.
⚠️ Other Considerations
Here are a few additional things you might want to think about:
1. Clean PATH Configuration
Make sure your shell config includes:
export PATH=$PATH:/usr/local/go/bin
Otherwise, the go
command won’t be available unless you call it by full path.
2. Use a Safer TMP Directory
To avoid clutter or accidental file overwrites, consider downloading into a temporary directory:
VER=1.24.4 && TMP_DIR=$(mktemp -d) && cd $TMP_DIR && \
wget https://go.dev/dl/go$VER.linux-amd64.tar.gz && \
sudo rm -rf /usr/local/go && \
sudo tar -C /usr/local -xzf go$VER.linux-amd64.tar.gz && \
/usr/local/go/bin/go version && \
cd ~ && rm -rf $TMP_DIR
This ensures no leftover tarballs and isolates the process nicely.
3. Use sha256sum
for Integrity Check (Optional but Recommended)
Each Go release provides a SHA256 checksum file. If you’re concerned about integrity:
wget https://go.dev/dl/go$VER.linux-amd64.tar.gz.sha256
sha256sum -c go$VER.linux-amd64.tar.gz.sha256
This adds extra security and integrity validation, especially when automating.
🎯 Finally
Using a variable like VER=1.24.4
in your one-liner Go install script:
- Reduces repetition
- Prevents errors
- Makes updates easier
- Improves maintainability
If you're setting up Go across multiple environments or provisioning with scripts, this small change makes a big difference.
Keep your tools clean, your scripts DRY, and your installations versioned properly. 🧼
Comments ()