Skip to content

Instantly share code, notes, and snippets.

@ShivamB25
Last active July 17, 2026 10:10
Show Gist options
  • Select an option

  • Save ShivamB25/d199453b6e566972b11ea5e629f58647 to your computer and use it in GitHub Desktop.

Select an option

Save ShivamB25/d199453b6e566972b11ea5e629f58647 to your computer and use it in GitHub Desktop.
go install script
#!/usr/bin/env bash
# Installs (or upgrades) the latest stable Go release on Linux.
set -euo pipefail
INSTALL_DIR="/usr/local"
# Detect architecture
case "$(uname -m)" in
x86_64) ARCH="amd64" ;;
aarch64) ARCH="arm64" ;;
armv7l) ARCH="armv6l" ;;
i386|i686) ARCH="386" ;;
*) echo "Unsupported architecture: $(uname -m)" >&2; exit 1 ;;
esac
echo "==> Detecting latest Go version..."
LATEST=$(curl -sL 'https://go.dev/dl/?mode=json' | python3 -c 'import sys,json; print(json.load(sys.stdin)[0]["version"])')
echo "Latest Go: $LATEST"
TARBALL="${LATEST}.linux-${ARCH}.tar.gz"
URL="https://go.dev/dl/${TARBALL}"
TMPDIR=$(mktemp -d)
trap 'rm -rf "$TMPDIR"' EXIT
echo "==> Downloading $URL"
curl -sSL -o "$TMPDIR/$TARBALL" "$URL"
echo "==> Verifying checksum"
EXPECTED_SHA=$(curl -sL 'https://go.dev/dl/?mode=json&include=all' \
| python3 -c "
import sys, json
data = json.load(sys.stdin)
for release in data:
for f in release.get('files', []):
if f['filename'] == '${TARBALL}':
print(f['sha256']); sys.exit(0)
sys.exit(1)
")
ACTUAL_SHA=$(sha256sum "$TMPDIR/$TARBALL" | awk '{print $1}')
if [ "$EXPECTED_SHA" != "$ACTUAL_SHA" ]; then
echo "Checksum mismatch! Expected $EXPECTED_SHA, got $ACTUAL_SHA" >&2
exit 1
fi
echo "Checksum OK."
echo "==> Removing any existing Go installation at $INSTALL_DIR/go"
sudo rm -rf "$INSTALL_DIR/go"
echo "==> Extracting to $INSTALL_DIR"
sudo tar -C "$INSTALL_DIR" -xzf "$TMPDIR/$TARBALL"
# Add to PATH in every rc file that's relevant, regardless of which shell
# is currently running this script (e.g. `bash install.sh` under a zsh login shell).
PROFILE_FILES=()
[ -f "$HOME/.bashrc" ] && PROFILE_FILES+=("$HOME/.bashrc")
[ -f "$HOME/.zshrc" ] && PROFILE_FILES+=("$HOME/.zshrc")
# Fall back to creating the rc file for the user's actual login shell if neither exists yet
if [ ${#PROFILE_FILES[@]} -eq 0 ]; then
case "${SHELL:-}" in
*/zsh) PROFILE_FILES+=("$HOME/.zshrc") ;;
*) PROFILE_FILES+=("$HOME/.bashrc") ;;
esac
fi
for PROFILE_FILE in "${PROFILE_FILES[@]}"; do
if ! grep -q "$INSTALL_DIR/go/bin" "$PROFILE_FILE" 2>/dev/null; then
echo "==> Adding Go to PATH in $PROFILE_FILE"
{
echo ''
echo '# Go'
echo "export PATH=\$PATH:${INSTALL_DIR}/go/bin:\$HOME/go/bin"
} >> "$PROFILE_FILE"
fi
done
echo "Added to: ${PROFILE_FILES[*]}. Open a new terminal, or 'source' the rc file matching your shell."
export PATH="$PATH:${INSTALL_DIR}/go/bin"
echo "==> Done."
go version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment