Skip to content

Instantly share code, notes, and snippets.

@Zate
Last active February 5, 2024 18:19
Show Gist options
  • Save Zate/b3c8e18cbb2bbac2976d79525d95f893 to your computer and use it in GitHub Desktop.
Save Zate/b3c8e18cbb2bbac2976d79525d95f893 to your computer and use it in GitHub Desktop.
Shell script to download and install latest golang
#! /bin/bash
# [get_golang.sh](https://gist.github.com/n8henrie/1043443463a4a511acf98aaa4f8f0f69)
# Download latest Golang release for AMD64
# https://dl.google.com/go/go1.10.linux-amd64.tar.gz
set -euf -o pipefail
# Install pre-reqs
sudo apt-get install python3 git -y
o=$(python3 -c $'import os\nprint(os.get_blocking(0))\nos.set_blocking(0, True)')
#Download Latest Go
GOURLREGEX='https://dl.google.com/go/go[0-9\.]+\.linux-amd64.tar.gz'
echo "Finding latest version of Go for AMD64..."
url="$(wget -qO- https://golang.org/dl/ | grep -oP 'https:\/\/dl\.google\.com\/go\/go([0-9\.]+)\.linux-amd64\.tar\.gz' | head -n 1 )"
latest="$(echo $url | grep -oP 'go[0-9\.]+' | grep -oP '[0-9\.]+' | head -c -2 )"
echo "Downloading latest Go for AMD64: ${latest}"
wget --quiet --continue --show-progress "${url}"
unset url
unset GOURLREGEX
# Remove Old Go
sudo rm -rf /usr/local/go
# Install new Go
sudo tar -C /usr/local -xzf go"${latest}".linux-amd64.tar.gz
echo "Create the skeleton for your local users go directory"
mkdir -p ~/go/{bin,pkg,src}
echo "Setting up GOPATH"
echo "export GOPATH=~/go" >> ~/.profile && source ~/.profile
echo "Setting PATH to include golang binaries"
echo "export PATH='$PATH':/usr/local/go/bin:$GOPATH/bin" >> ~/.profile && source ~/.profile
echo "Installing dep for dependency management"
go get -u github.com/golang/dep/cmd/dep
# Remove Download
rm go"${latest}".linux-amd64.tar.gz
# Print Go Version
/usr/local/go/bin/go version
python3 -c $'import os\nos.set_blocking(0, '$o')'
#! /bin/bash
set -euf -o pipefail
sudo apt-get install gpg -y
curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg
sudo mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg
sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main" > /etc/apt/sources.list.d/vscode.list'
sudo apt-get update -y
sudo apt-get install code-insiders -y
sudo apt-get install libxss1 libasound2 -y
code-insiders --install-extension lukehoban.Go
code-insiders --install-extension PeterJausovec.vscode-docker
code-insiders --install-extension Zignd.html-css-class-completion
code-insiders --install-extension ecmel.vscode-html-css
code-insiders --install-extension redhat.vscode-yaml
code-insiders --install-extension codezombiech.gitignore
code-insiders --install-extension IBM.output-colorizer
code-insiders --install-extension donjayamanne.git-extension-pack
code-insiders --install-extension formulahendry.docker-extension-pack
code-insiders --install-extension foxundermoon.shell-format
code-insiders --install-extension eamodio.gitlens
code-insiders --install-extension donjayamanne.githistory
code-insiders --install-extension Shan.code-settings-sync
code-insiders --install-extension Equinusocio.vsc-material-theme
code-insiders --install-extension yzhang.markdown-all-in-one
code-insiders --install-extension anseki.vscode-color
code-insiders --install-extension shd101wyy.markdown-preview-enhanced
code-insiders --install-extension PKief.material-icon-theme
code-insiders --install-extension robertohuertasm.vscode-icons
code-insiders --list-extensions --show-versions
@lpar
Copy link

lpar commented Oct 1, 2021

Possible alternative: https://github.com/lpar/goup

@luandnh
Copy link

luandnh commented Dec 11, 2021

@Zate
Copy link
Author

Zate commented Dec 11, 2021

I just put that into something I've been using in my dotfiles etc, however right now, that site says latest is 1.17.4 and yet my download grabs 1.17.5 (obviously this will change when people read this in the futre). Point is, they VERSION on the site, and the VERSION you can download are not the same. Makes having a version call to the site be kinda.. pointless?

upGo() {
    INST_VER=$(go version | cut -d" " -f3)
    echo ${INST_VER}
    #REM_VER=$(curl -s https://go.dev/VERSION?m=text)
    REM_VER=$( wget -qO- https://golang.org/dl/ | grep 'id=\"go' | head -1 | grep -oP -e '(go[0-9\.]+)' )
    echo ${REM_VER}
    [[ $INST_VER == $REM_VER ]] && return || echo "Local: ${INST_VER} - Remote: ${REM_VER}"
    sudo rm -rf /usr/local/go
    wget -qO- https://golang.org$( wget -qO- https://golang.org/dl/ | grep -oP '/dl/go([0-9\.]+)\.linux-amd64.tar.gz' | head -1)  | sudo tar zxf - -C /usr/local
    upPath
    go version
}

Kind of annoying that go itself doesn't have a built in way to update.

@Zate
Copy link
Author

Zate commented Dec 11, 2021

Also adding, use the above function in https://github.com/Zate/ide which is a containerized remote/web based vscode install based on codeserver.

@Alchemyst0x
Copy link

In case anyone happens to not mind the fact that the URL technically points to the version of Go running on the server (which I still don't understand either...), and if you're trying to install on multiple arches (or you're just lazy like me and want a one-shot for your scripts) --

GO_DL_URL="https://go.dev$(wget --no-check-certificate -qO- https://go.dev/dl/ |
    grep -oP '\/dl\/go([0-9\.]+)\.linux-'$(dpkg --print-architecture)'\.tar\.gz' |
    head -n 1)"

Also I should add this would only work on Debian/Ubuntu-based systems, assuming dpkg is bound to Debian-based, which I think is correct. Highly specific but hopefully someone finds it helpful. :)

Thanks for the gist, it helped me out.

@andygarfield
Copy link

andygarfield commented Jan 27, 2024

This is what I'm using:

GO_ARCHIVE=$(curl -s 'https://go.dev/VERSION?m=text' | head -n 1).linux-arm64.tar.gz
GO_LINK="https://go.dev/dl/$GO_ARCHIVE"
wget $GO_LINK
sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xzf $GO_ARCHIVE
rm $GO_ARCHIVE

Just change the linux and arm64 to suit your needs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment