Last active
July 1, 2020 15:05
-
-
Save langley/cf11f62499dfa16f8356308873e0d78f to your computer and use it in GitHub Desktop.
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/bash | |
# | |
# Use this script with sudo to install multiple golang | |
# installations. | |
if [[ $(id -u) -ne 0 ]] ; then echo "This script should be run using | |
sudo or as the root user" ; exit 1 ; | |
fi | |
# Ensure the user wants to rm the contents of /opt/golang! | |
read -p "This script will ERASE THE CONTENTS OF /opt/golang! Are you sure you want to proceed? [y/n] " -n 1 -r | |
echo # (optional) move to a new line | |
if [[ ! $REPLY =~ ^[Yy]$ ]] | |
then | |
exit 1 | |
fi | |
## Configuration and init | |
DIR="/opt/golang" | |
# Update these as appropriate for your install | |
declare -A DOWNLOADS | |
DOWNLOADS["go1.14.4"]="https://golang.org/dl/go1.14.4.linux-amd64.tar.gz" | |
DOWNLOADS["go1.13.12"]="https://golang.org/dl/go1.13.12.linux-amd64.tar.gz" | |
declare -A VERSIONS | |
# ENSURE THESE ARE THE SAME LABELS AS ABOVE! | |
VERSIONS["go1.14.4"]=11404 | |
VERSIONS["go1.13.12"]=11312 | |
# Clean-up before installing the defined versions | |
update-alternatives --quiet --remove-all go | |
# Create /opt/golang dir and download from golang downloads website | |
mkdir -p /opt/golang | |
cd /opt/golang | |
rm -rf /opt/golang/* # this syntax just ensures it's clear what is being rm'd | |
# Loop through the various versions to install, download them, extract them | |
# and move them to the right directory names, one for each version. | |
for key in ${!DOWNLOADS[@]}; do | |
echo Downloading version ${key} | |
# clean up previous file if it exists | |
rm -f ${DOWNLOADS[${key}]##*/}* | |
wget ${DOWNLOADS[${key}]} | |
targz_filename=${DOWNLOADS[${key}]##*/} | |
# below assume a tar.gz file | |
tar zxvf ${targz_filename} | |
# mv go to the release name | |
filename=${targz_filename%.*} # strips off .gz | |
filename=${filename%.*} # strips off tar | |
mv go ${filename%.*} | |
# save space, delete the download files | |
rm -f ${DOWNLOADS[${key}]##*/}* | |
done | |
# Finally use update-alternatives to install these versions as alternatives | |
for key in ${!VERSIONS[@]}; do | |
echo Installing golang version ${key} with alternatives priority ${VERSIONS[${key}]} | |
echo update-alternatives --quiet --install /usr/local/go go ${DIR}/${key} ${VERSIONS[${key}]} | |
update-alternatives --quiet --install /usr/local/go go ${DIR}/${key} ${VERSIONS[${key}]} | |
done | |
echo | |
echo "Following golang versions installed and their priorities" | |
update-alternatives --display go | |
echo | |
echo "Use: sudo update-alternatives --config go" | |
echo "To switch between golang versions" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment