Skip to content

Instantly share code, notes, and snippets.

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
# [](
# Download latest Golang release for AMD64
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
echo "Finding latest version of Go for AMD64..."
url="$(wget -qO- | 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
# 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
# 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 | gpg --dearmor > microsoft.gpg
sudo mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg
sudo sh -c 'echo "deb [arch=amd64] 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
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
Copy link

now the DLs are in server.

#Download Latest Go


if [ ! -z "$1" ]; then

mkdir -p $GOTOOLS


echo "Finding latest version of Go for AMD64..."

# ..:: Get the file path e.g:
# /dl/go1.15.linux-amd64.tar.gz
DL_PATH_URL="$(wget --no-check-certificate -qO- | grep -oP '\/dl\/go([0-9\.]+)\.linux-amd64\.tar\.gz' | head -n 1)" 

latest="$(echo $DL_PATH_URL | grep -oP 'go[0-9\.]+' | grep -oP '[0-9\.]+' | head -c -2 )"

echo "Downloading latest Go for AMD64: ${latest}"

wget --no-check-certificate --continue --show-progress "$DL_HOME$DL_PATH_URL" -P $GOTOOLS


LATEST="$(find $GOTOOLS -name "go*" -type f | head -n 1)"

echo "LATEST: ${LATEST}"

Copy link

Zate commented Aug 23, 2020

Awesome thanks for the update. I've been meaning to move this into an ansible task as part of setting up code-server, so thanks for sharing your code.

Copy link

romelgomez commented Nov 13, 2020

Hi @Zate, thanks to you at first because, your code was the inspiration that I needed.

This is mi repo

The goal is to have programing env all set and update in the local computer to start testing with golang.


Copy link

tetafro commented Jan 28, 2021

Here's a simple way to get latest version:


Copy link

Updated for my needs based on comment by @tetafro and a non-root install for fedora.


LATEST="$(curl -s"

mkdir -p "$GOUTIL"

if ! [ -f "$GOUTIL/$DL_PKG" ]; then
        echo "Downloading latest Go for Linux AMD64: ${LATEST}"
        wget --no-check-certificate --continue --show-progress "$DL_URL" -P "$GOUTIL"

function current_version() {
        if $(which go &> /dev/null); then
                go version | grep -oP "go[0-9.]+"
                echo "UNINSTALLED"

if ! [ "$(current_version)" == "$LATEST" ]; then
        echo "updating $(current_version) to $LATEST"
        rm -rf "$GOPATH" && tar -C $HOME -xzf "$GOUTIL/$DL_PKG"
        echo "updated golang to version $(current_version)"
        echo "$(current_version) is up to date"

Copy link

lpar commented Oct 1, 2021

Possible alternative:

Copy link

luandnh commented Dec 11, 2021

Copy link

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
    REM_VER=$( wget -qO- | 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-$( wget -qO- | grep -oP '/dl/go([0-9\.]+)\.linux-amd64.tar.gz' | head -1)  | sudo tar zxf - -C /usr/local
    go version

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

Copy link

Zate commented Dec 11, 2021

Also adding, use the above function in which is a containerized remote/web based vscode install based on codeserver.

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="$(wget --no-check-certificate -qO- |
    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.

Copy link

andygarfield commented Jan 27, 2024

This is what I'm using:

GO_ARCHIVE=$(curl -s '' | head -n 1).linux-arm64.tar.gz
wget $GO_LINK
sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xzf $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