Skip to content

Instantly share code, notes, and snippets.

@lpimem
Last active February 18, 2017 22:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lpimem/2b33bf3b5704aab6b56541c14157f80f to your computer and use it in GitHub Desktop.
Save lpimem/2b33bf3b5704aab6b56541c14157f80f to your computer and use it in GitHub Desktop.
install go / golang on ubuntu
#! /bin/bash
# --------------------------------------------
# Go installization script for ubuntu.
# Tested on 16.04
#
# What it does:
# 1. Install go for the system
# 2. create GOPATH for the current user
#
# Instructions:
# 1. Modify configurations
# 2. sudo ./install_go.sh
# --------------------------------------------
# configurations
GO_ROOT_PREFIX=/usr/local
GOPATH=/home/$SUDO_USER/go
GO_VER=1.8
GO_VER_FILE_CHECKSUM=53ab94104ee3923e228a2cb2116e5e462ad3ebaeea06ff04463479d7f12d27ca
# check user privilege
if [[ "$EUID" -ne 0 ]]; then
echo "Error: please run with sudo."
exit
fi
# Download go binaries
GO_FILE=go$GO_VER.linux-amd64.tar.gz
wget -nc https://storage.googleapis.com/golang/$GO_FILE
checksum=`sha256sum $GO_FILE | awk '{print $1}'`
if [[ $checksum != $GO_VER_FILE_CHECKSUM ]]; then
echo "CHECKSUM doesn't match!"
echo "expecting: $GO_VER_FILE_CHECKSUM"
echo "got : $checksum"
exit 1
fi
echo "extracting go tools to $GO_ROOT_PREFIX/go"
tar -C $GO_ROOT_PREFIX -xzf $GO_FILE
# Setup paths and variables
if [[ $GO_ROOT_PREFIX != /usr/local ]]; then
echo "export GOROOT=$GO_ROOT_PREFIX/go" > /etc/profile.d/go.sh
echo 'export PATH=$PATH:'$GOROOT/bin >> /etc/profile.d/go.sh
echo "GOROOT is set to: $GOROOT"
else
echo 'export PATH=$PATH:'$GO_ROOT_PREFIX/go/bin > /etc/profile.d/go.sh
fi
chmod +x /etc/profile.d/go.sh
source /etc/profile.d/go.sh
# Setup GOPATH
mkdir -p $GOPATH/src
mkdir $GOPATH/bin
mkdir $GOPATH/pkg
export GOPATH=$GOPATH
export PATH=$PATH:$GOPATH/bin
echo "export GOPATH=$GOPATH" >> $HOME/.bashrc
echo 'export PATH=$PATH:$GOPATH/bin' >> $HOME/.bashrc
# Test go installization
mkdir $GOPATH/src/hello
echo 'package main
import "fmt"
func main() {
fmt.Printf("hello, world\n")
}' > $GOPATH/src/hello/hello.go
cd $GOPATH/src/hello
go build
rc=$?
if [[ $rc == 0 ]]; then
echo "Go Installization Success"
else
echo "Go Installization Failed, exit code: $rc"
fi
rm -rf $GOPATH/src/hello
chown -R $SUDO_USER: $GOPATH
chown $SUDO_USER: /home/$SUDO_USER/.wget-hsts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment