Skip to content

Instantly share code, notes, and snippets.

@ReSearchITEng
Last active April 30, 2021 10:11
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 ReSearchITEng/a7e9202131118a321b33afe2e2c663fc to your computer and use it in GitHub Desktop.
Save ReSearchITEng/a7e9202131118a321b33afe2e2c663fc to your computer and use it in GitHub Desktop.
golang install in ubuntu + update-alternatives

GOLANG

sudo add-apt-repository -y ppa:longsleep/golang-backports
#sudo apt-get install -y golang-1.12
sudo apt-get install -y golang-1.15

check if you have go using alternatives:

sudo update-alternatives --query go
sudo update-alternatives --query gofmt
#sudo update-alternatives --all --skip-auto

dpkg -S /usr/bin/go
#sudo apt-get purge -y golang-go # optional but ideal (removes ubuntu official golang-go); otherwise some issues still exist
#sudo apt autoremove -y # optional
#sudo update-alternatives --install <link> <name> <path> <priority>
if [[ -s /usr/lib/go/bin/go ]]; then
  # These are the one installed when you have the golang from official ubuntu repo
  sudo update-alternatives --install /usr/bin/go go /usr/lib/go/bin/go 0
  sudo update-alternatives --install /usr/bin/go go /usr/lib/go/bin/gofmt 0
fi
#sudo update-alternatives --install /usr/bin/go go /usr/lib/go-1.12/bin/go 0
sudo update-alternatives --install /usr/bin/go go /usr/lib/go-1.15/bin/go 3
sudo update-alternatives --list go
sudo update-alternatives --install /usr/bin/gofmt gofmt /usr/lib/go-1.15/bin/gofmt 3
sudo update-alternatives --list gofmt

Env vars - YOU SHOULD NOT NEED IT, so DON'T do the BELOW !!!

when you have root:

#sudo -H gedit /etc/environment #cannot hande dynamic vars like $HOME and GOPATH needs to be absolute path

cat <<'EOF' | sudo tee -a /etc/profile.d/environment-variables.sh
GOBIN_FILE=`update-alternatives --query go | grep "Value" | cut -d" " -f2`
GOBIN_DIR=`dirname $GOBIN_FILE`
export GOROOT="${GOBIN_DIR}/../"
export GOPATH="${HOME}/.go"
export PATH="$PATH:$GOROOT/bin:$GOPATH/bin"
EOF

If you don't have root:

cat <<'EOF' | tee -a ~/.bashrc # change for other shells
GOBIN_FILE=`update-alternatives --query go | grep "Value" | cut -d" " -f2`
GOBIN_DIR=`dirname $GOBIN_FILE`
export GOROOT="${GOBIN_DIR}/../"
export GOPATH="${HOME}/.go"
export PATH="$PATH:$GOROOT/bin:$GOPATH/bin"
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment