How to Easily Upgrade Go on Linux with a Simple Command

How to Easily Upgrade Go on Linux with a Simple Command
Photo by Chinmay B / Unsplash

Upgrading Go on Linux can be a straightforward process, and this guide will show you a quick and efficient method to do it using a single command. Whether you're looking to upgrade to the latest version or a specific one, following this approach will save you time and ensure you always have the latest Go version running on your system.

Why Upgrade Go?

Regularly updating Go is essential for:

  • Performance improvements: New versions bring optimizations that can enhance the performance of your applications.
  • Bug fixes: Updates address known issues and make your development environment more stable.
  • New features: Upgrading keeps you up to date with the latest features and tools provided by Go, improving productivity.
  • Security patches: The Go development team regularly releases patches that fix security vulnerabilities.

The Simple One-Line Command to Upgrade Go

Here’s a trick to upgrade Go with a single command. It will download the latest version, remove the previous one, install the new version, and verify the installation:

wget https://go.dev/dl/go1.23.1.linux-amd64.tar.gz && sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xzf go1.23.1.linux-amd64.tar.gz && go version && rm go1.23.1.linux-amd64.tar.gz

That command assume that the latest version of Go is 1.23.1.

How about make it dynamic with any latest version from Go website?

It is possible to dynamically fetch the latest Go version and update the version part in your command. You can achieve this by using a script to automatically check the latest version from the official Go downloads page and update the download link accordingly.

Here’s a bash script that does exactly that:

Create a file called upgrade_go.sh and type this script.

#!/bin/bash

# Fetch the latest version of Go from the official Go downloads page
LATEST_VERSION=$(curl -s https://go.dev/dl/ | grep -oP 'go[0-9]+\.[0-9]+\.[0-9]+\.linux-amd64\.tar\.gz' | head -n 1 | grep -oP 'go[0-9]+\.[0-9]+\.[0-9]+')

# Download the latest Go version
wget https://go.dev/dl/${LATEST_VERSION}.linux-amd64.tar.gz

# Remove the existing Go installation
sudo rm -rf /usr/local/go

# Install the latest version
sudo tar -C /usr/local -xzf ${LATEST_VERSION}.linux-amd64.tar.gz

# Verify the installed version
go version

# Clean up the downloaded tar.gz file
rm ${LATEST_VERSION}.linux-amd64.tar.gz

Explanation:

  1. LATEST_VERSION=$(curl -s https://go.dev/dl/ | grep -oP 'go[0-9]+\.[0-9]+\.[0-9]+\.linux-amd64\.tar\.gz' | head -n 1 | grep -oP 'go[0-9]+\.[0-9]+\.[0-9]+'):
    • This command fetches the latest Go version from the official Go download page using curl, then uses grep to extract the version number (e.g., go1.23.1).
  2. wget https://go.dev/dl/${LATEST_VERSION}.linux-amd64.tar.gz:
    • This part dynamically downloads the tarball of the latest Go version using the version number stored in the LATEST_VERSION variable.
  3. The rest of the script follows the same steps as the original one-liner:
    • Remove the existing Go installation.
    • Extract the new Go version.
    • Verify the installation with go version.
    • Clean up the downloaded archive.

How to Use the Script:

  1. Copy the script above into a file, for example: upgrade_go.sh.
  2. Give the script executable permissions:
chmod +x upgrade_go.sh
  1. Run the script:
./upgrade_go.sh

This will automatically fetch the latest version of Go and upgrade your installation without the need to manually adjust the version part in the command.

Easy right? Hope it helps.

Support Us

Subscribe to Buka Corner

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe