Skip to content

Instantly share code, notes, and snippets.

@afonsolopez
Created June 11, 2021 05:08
Show Gist options
  • Select an option

  • Save afonsolopez/a2c535426ed283b99477663316d3e746 to your computer and use it in GitHub Desktop.

Select an option

Save afonsolopez/a2c535426ed283b99477663316d3e746 to your computer and use it in GitHub Desktop.
Script to easily install/update Golang on Ubuntu/Fedora
#!/bin/bash
echo ""
tput setaf 6;
echo "Write the version of Go that you want:"
tput sgr0;
# Asks for the desired golang version to be installed
read goversion
echo ""
tput setaf 6;
echo "Downloading Go $goversion..."
tput sgr0;
echo ""
# Download the golang installer from the official website
wget -P $HOME/Downloads https://golang.org/dl/go$goversion.linux-amd64.tar.gz
# Checks if the download was finished
if [ $? -eq 0 ]; then
tput setaf 2;
echo "Download succeeded ✓"
tput sgr0;
else
tput setaf 1;
echo "Download failed ✗"
tput sgr0;
exit 1
fi
echo ""
tput setaf 6;
echo "Checking if a previous Go installation exists..."
tput sgr0;
echo ""
# Checks if a previos golang installation exists
# If true, deletes it's folder
if [ -d "/usr/local/go" ]
then
tput setaf 2;
echo "Golang installation found ✓"
echo ""
tput setaf 6;
echo "Deleting previous installation..."
echo ""
tput sgr0;
rm -rf /usr/local/go
if [ $? -eq 0 ];
then
tput setaf 2;
echo "Older installation removed ✓"
tput sgr0;
else
tput setaf 1;
echo "Older installation cannot been removed ✗"
tput sgr0;
exit 1
fi
else
tput setaf 6;
echo "No previous golang installation was found ✗"
tput sgr0;
fi
echo ""
tput setaf 6;
echo "Installing Go $goversion..."
tput sgr0;
echo ""
# Extract the download version to the OS
tar -C /usr/local -xzf $HOME/Downloads/go$goversion.linux-amd64.tar.gz
if [ $? -eq 0 ]; then
tput setaf 2;
echo "Installation succeeded ✓"
tput sgr0;
else
tput setaf 1;
echo "Installation failed ✗"
tput sgr0;
exit 1
fi
echo ""
tput setaf 6;
echo "Deleting installation file..."
tput sgr0;
echo ""
# Removes the downloaded instalation file
rm -R $HOME/Downloads/go$goversion.linux-amd64.tar.gz
if [ $? -eq 0 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
(%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
,&%%%%%%%%%%%%%%%%%%%%%%%%%%%%% &&&
.&&&&# %%%%%%%%%%%%%%%% &&#&
"
echo "Installation completed ✓"
echo ""
tput setaf 3;
tput bold
echo "IMPORTANT! Add /usr/local/go/bin to the PATH environment variable."
tput sgr0;
echo ""
# Script to install golang on linux based systems
# Tested on Fedora 34
# Copyright (C) 2021 Afonso Lopez (afonsolopez.com)
# Permission to copy and modify is granted under the MIT license
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment