Skip to content

Instantly share code, notes, and snippets.

@stealthybox
Created September 24, 2019 00:51
Show Gist options
  • Save stealthybox/edefb0b57ef361119233ca1fad197e3d to your computer and use it in GitHub Desktop.
Save stealthybox/edefb0b57ef361119233ca1fad197e3d to your computer and use it in GitHub Desktop.
Home Local Go - shell script that downloads and links different versions of go into your ~/.local directory
# source this from your shell's profile
# Go Binaries linked to my homedir
export GOROOT=$HOME/.local/usr/local/go
export PATH=$PATH:$GOROOT/bin
# User Binaries
export PATH=$PATH:$HOME/go/bin
# User GOPATH for backwards-compat
export GOPATH=$HOME/go
#!/bin/sh
# Fetches go into ~/.local/usr/local/go${version}
# Links to ~/.local/usr/local/go
# Can be used idempotently
# remember to configure your GOROOT, PATH, and GOPATH
#
# Usage:
# version=1.12.9 ./install.sh
set -eu
version="${version:-1.12}"
os="${os:-linux}"
arch="${arch:-amd64}"
mirror="https://dl.google.com/go"
install_root="${install_root:-${HOME}/.local/usr/local}"
mkdir -p "${install_root}"
cd "${install_root}"
# if version already installed, link and exit
test -x "go${version}" && ln -sfn "go${version}" go && echo "linked go ${version}" && exit 0
# fetch and unpack
mkdir -p tmp
cd tmp
wget "${mirror}/go${version}.${os}-${arch}.tar.gz"
echo "unpacking..."
tar -xzf "go${version}.${os}-${arch}.tar.gz"
# move to versioned path
cd ..
mv tmp/go "go${version}"
rm -rf tmp
# link go
ln -sfn "go${version}" go
echo "downloaded and linked go ${version}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment