Last active
September 7, 2025 20:44
-
-
Save D1360-64RC14/80634897b7f2cd20dab766740328570b to your computer and use it in GitHub Desktop.
Download and install golang version into /opt/go folder. Supports version managing though alternatives command.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env bash | |
set -e | |
DOWNLOAD_URL='https://go.dev/dl/go1.25.1.linux-amd64.tar.gz' | |
# goM.mm.ff.linux-amd64.tar.gz | |
GOLANG_TAR_GZ="$(basename $DOWNLOAD_URL)" | |
# goM.mm.ff.linux-amd64 | |
GOLANG="$(basename $GOLANG_TAR_GZ .tar.gz)" | |
# M.mm.ff | |
VERSION=$(echo $GOLANG | sed -E 's/go([[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+)\..*/\1/') | |
# Mmmmfff | |
PRIORITY=$(printf '%d%03d%03d' $(echo $VERSION | tr '.' ' ')) | |
INSTALL_BASE_FOLDER='/opt/go' | |
cat <<ENDL | |
Go Download URL: $DOWNLOAD_URL | |
Go Version: $VERSION | |
Install folder: $INSTALL_BASE_FOLDER/$GOLANG | |
alternatives priority: $PRIORITY | |
ENDL | |
#################################################### | |
if [ -d "$INSTALL_BASE_FOLDER/$GOLANG" ]; then | |
echo "[ERROR] Oh, $INSTALL_BASE_FOLDER/$GOLANG seems to be already installed!" | |
exit 1 | |
fi | |
if [[ "$EUID" != 0 ]]; then | |
echo '[ERROR] Please, run this script as root!' | |
exit 1 | |
fi | |
#################################################### | |
cleanup() { | |
local exit_code=$? | |
if [[ $exit_code != 0 ]]; then | |
rm -rf "$INSTALL_BASE_FOLDER/$GOLANG" | |
echo "[INFO] Temporary files preserved at $TMP_DIR" | |
exit $exit_code | |
fi | |
rm -rf "$TMP_DIR" | |
} | |
trap cleanup EXIT | |
#################################################### | |
TMP_DIR="$(mktemp -d)" | |
mkdir -p "$INSTALL_BASE_FOLDER/$GOLANG" | |
cd $TMP_DIR | |
curl -L "$DOWNLOAD_URL" -o "$GOLANG_TAR_GZ" | |
tar xf "$GOLANG_TAR_GZ" | |
mv go "$GOLANG" | |
mv "$GOLANG" "$INSTALL_BASE_FOLDER" | |
alternatives \ | |
--install /usr/bin/go golang "$INSTALL_BASE_FOLDER/$GOLANG/bin/go" "$PRIORITY" \ | |
--follower /usr/bin/gofmt gofmt "$INSTALL_BASE_FOLDER/$GOLANG/bin/gofmt" | |
#################################################### | |
echo | |
echo '> go version' | |
go version |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment