Skip to content

Instantly share code, notes, and snippets.

@alessiosavi
Last active April 26, 2021 07:31
Show Gist options
  • Save alessiosavi/0d82a8bc1504e13fa6186419039ac5ee to your computer and use it in GitHub Desktop.
Save alessiosavi/0d82a8bc1504e13fa6186419039ac5ee to your computer and use it in GitHub Desktop.
Install golang on linux from source
#!/bin/bash
golang_version="1.16.3"
golang_link="https://dl.google.com/go/go$golang_version.linux-amd64.tar.gz"
root_folder="/opt/GOLANG" # Set the tree variable needed for build the enviroinment
go_source="$root_folder/go"
go_projects="$root_folder/go_projects"
# Check if this script was alredy run
if [ -d "$root_folder" ] || [ -d "$go_source" ] || [ -d "$go_projects" ]; then
### Take action if $DIR exists ###
echo "Golang is alredy installed!"
exit 1
fi
# Be sure that golang is not alredy installed
test_already_install=$(which go)
if [ -z $test_already_install ]
then
echo "Going to install go ..."
else
echo "Seems that go is alredy installed in $(which go)"
exit
fi
sudo mkdir -p $root_folder # creating dir for golang source code
sudo chown -R $(whoami) $root_folder
cd $root_folder # entering dir
wget $golang_link #downloading golang
tar xf $(ls | grep "tar") # extract only the tar file
mkdir $go_projects
cat <<EOF >> /home/$(whoami)/.bashrc
export GOPATH="$go_projects"
export GOBIN="$go_projects/bin"
export GOROOT="$go_source"
export PATH="$PATH:$GOROOT/bin:$GOBIN"
EOF
# Load the fresh changed .bashrc env file
source /home/$(whoami)/.bashrc
# Print the golang version
go version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment