Last active
January 10, 2018 23:40
-
-
Save nareshganesan/322632c4d7e8b7147989f3ce43a95321 to your computer and use it in GitHub Desktop.
ubuntu install go from source
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 | |
err_report() { | |
echo "Error on line $1" | |
} | |
trap 'err_report $LINENO' ERR | |
usage() { | |
echo "Usage: $0 | |
Options: | |
$1" 1>&2; | |
exit 1; | |
} | |
force_create=0; | |
while getopts "hv" o; do | |
case "${o}" in | |
v) go_version=${OPTARG} | |
;; | |
h) usage "-v (optional) go lang version (default: 1.8.3) | |
-h help" >&2; | |
exit 0;; | |
\?) usage "-v (optional) go lang version (default: 1.8.3) | |
-h help" >&2; | |
exit 1;; | |
:) echo "Option -$OPTARG requires an argument." >&2 | |
exit 1;; | |
esac | |
done | |
if [ ! "$go_version" ] | |
then | |
go_version='1.8.3'; | |
fi | |
version="$go_version"; | |
unameOut="$(uname -s)" | |
case "${unameOut}" in | |
Linux*) machine=$(echo "$unameOut" | tr '[:upper:]' '[:lower:]'); | |
;; | |
Darwin*) machine=$(echo "$unameOut" | tr '[:upper:]' '[:lower:]'); | |
;; | |
*) machine="UNKNOWN:${unameOut}" | |
esac | |
# get the user running the script | |
user=$(who | awk '{print $1}'); | |
echo "User: "$user; | |
COMPANY_NAME=company | |
if [ $machine == 'linux' ]; then | |
echo "OS type: Linux" | |
company_home="/home/$user/$COMPANY_NAME"; | |
bashrc_path="/home/$user/.bashrc"; | |
elif [ $machine == 'darwin' ]; then | |
echo "OS type: OS X" | |
company_home="/Users/$user/$COMPANY_NAME"; | |
bashrc_path="/Users/$user/.bash_profile"; | |
else | |
echo "OS type: unknown" | |
company_home="/home/$user/$COMPANY_NAME"; | |
bashrc_path="/home/$user/.profile"; | |
fi | |
mkdir -p $company_home; | |
chown -R $user:$user $company_home; | |
function source_env { | |
source $bashrc_path; | |
} | |
if [ ! -f go$version.$machine-amd64.tar.gz ]; then | |
# download only if go source is not present | |
echo "Downloading go lang version for "${machine} | |
echo go$version.$machine-amd64.tar.gz; | |
wget https://storage.googleapis.com/golang/go$version.$machine-amd64.tar.gz; | |
chown -R $user:$user go$version.$machine-amd64.tar.gz; | |
else | |
echo "Using existing go source: go"$version.$machine-amd64.tar.gz; | |
chown -R $user:$user go$version.$machine-amd64.tar.gz; | |
fi | |
sudo tar -C /usr/local -xzf go$version.$machine-amd64.tar.gz; | |
echo "Go downloaded and extracted to /usr/local/go." | |
echo "Setting GOROOT and GOPATH in bashrc" | |
echo '# company configurations' >> $bashrc_path; | |
echo 'export COMPANY_NAME=company' >> $bashrc_path; | |
source_env | |
echo '# go configurations' >> $bashrc_path; | |
echo 'export PATH=$PATH:/usr/local/go/bin' >> $bashrc_path; | |
source_env | |
go_workspace=$company_home/go; | |
mkdir -p $go_workspace $go_workspace/src $go_workspace/pkg $go_workspace/bin | |
echo "export GOPATH=$company_home/go" >> $bashrc_path; | |
source_env | |
echo "go lang installed in the below path: " | |
eval echo "location: /usr/local/go/bin"; | |
eval echo "GOPATH: ${go_workspace}"; | |
/usr/local/go/bin/go version; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment