Skip to content

Instantly share code, notes, and snippets.

@jsoques
Created April 5, 2025 14:21
Show Gist options
  • Save jsoques/caf11280be73e804ee566c788d03dc98 to your computer and use it in GitHub Desktop.
Save jsoques/caf11280be73e804ee566c788d03dc98 to your computer and use it in GitHub Desktop.
Get and install latest go in local home folder
#!/bin/bash
# Script to download the latest stable Go version for Linux amd64/arm64
# Exit on any error
set -e
echo "Fetching Go downloads page..."
# Create a temporary file for the HTML content
TEMP_HTML=$(mktemp)
trap 'rm -f $TEMP_HTML' EXIT
# Download the Go downloads page
if ! curl -s https://go.dev/dl/ -o "$TEMP_HTML"; then
echo "Error: Failed to download Go downloads page." >&2
exit 1
fi
ARCH=$(uname -p)
echo $ARCH
if [ $ARCH = "aarch64" ]; then
ARCH="arm64"
fi
if [ $ARCH = "x86_64" ]; then
ARCH="amd64"
fi
echo "Processor architecture $ARCH"
# Extract the latest stable version
LATEST_VERSION=$(grep -o 'go[0-9]\+\.[0-9]\+\.[0-9]\+' "$TEMP_HTML" | grep -v "beta\|rc" | head -1)
if [ -z "$LATEST_VERSION" ]; then
echo "Error: Failed to find the latest Go version." >&2
exit 1
fi
echo "Latest stable Go version: $LATEST_VERSION"
function current_version() {
if $(which go &> /dev/null); then
go version | grep -oP "go[0-9.]+"
else
echo "UNINSTALLED"
fi
}
if ! [ "$(current_version)" == "$LATEST_VERSION" ]; then
export GOROOT=$(go env | grep GOROOT | awk -F "=" '{print substr($2, 2, length($2) - 2)}')
export GOPATH=$(go env | grep GOPATH | awk -F "=" '{print substr($2, 2, length($2) - 2)}')
echo "updating $(current_version) to $LATEST_VERSION"
# Form the download URL
DOWNLOAD_URL="https://go.dev/dl/${LATEST_VERSION}.linux-${ARCH}.tar.gz"
FILENAME="${LATEST_VERSION}.linux-${ARCH}.tar.gz"
echo "Downloading $DOWNLOAD_URL..."
# Download the Go tarball
if ! curl -L "$DOWNLOAD_URL" -o "$FILENAME"; then
echo "Error: Failed to download Go tarball." >&2
exit 1
fi
# Verify the file exists and has content
if [ ! -s "$FILENAME" ]; then
echo "Error: Downloaded file is empty or does not exist." >&2
exit 1
fi
# Report file size
FILE_SIZE=$(du -h "$FILENAME" | cut -f1)
echo "Success! Downloaded $FILENAME ($FILE_SIZE)"
echo "File saved to $(pwd)/$FILENAME"
echo "GOROOT = $GOROOT"
echo "GOPATH = $GOPATH"
export PATH=$PATH:~/go/bin
rm -rf "$GOROOT" && tar -C $HOME/local -xzf "$FILENAME"
echo "Installing useful packages..."
echo "Air..."
go install github.com/air-verse/air@latest
echo "sqlc..."
go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest
echo "Wails..."
go install github.com/wailsapp/wails/v2/cmd/wails@latest
echo "garble..."
go install mvdan.cc/garble@latest
echo "templ..."
go install github.com/a-h/templ/cmd/templ@latest
echo "global-update..."
go install github.com/Gelio/go-global-update@latest
echo "Ready"
go-global-update
else
echo "$(current_version) is up to date"
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment