Skip to content

Instantly share code, notes, and snippets.

@Zate

Zate/get_go.sh

Last active Apr 28, 2021
Embed
What would you like to do?
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
@romelgomez

This comment has been minimized.

Copy link

@romelgomez romelgomez commented Aug 23, 2020

now the DLs are in golang.org server.

#!/bin/bash
#Download Latest Go

GOTOOLS=$HOME/go/tools

if [ ! -z "$1" ]; then
    GOTOOLS=$1
fi

mkdir -p $GOTOOLS

DL_HOME=https://golang.org

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- https://golang.org/dl/ | 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

unset DL_PATH_URL

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

echo "LATEST: ${LATEST}"
@Zate

This comment has been minimized.

Copy link
Owner Author

@Zate 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.

@romelgomez

This comment has been minimized.

Copy link

@romelgomez 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 https://github.com/romelgomez/golang

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

Cheers!!!

@tetafro

This comment has been minimized.

Copy link

@tetafro tetafro commented Jan 28, 2021

Here's a simple way to get latest version:

curl https://golang.org/VERSION?m=text
@rustysys-dev

This comment has been minimized.

Copy link

@rustysys-dev rustysys-dev commented Apr 28, 2021

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

#!/usr/bin/bash

GOPATH="$HOME/go"
GOUTIL="$HOME/.go"
LATEST="$(curl -s https://golang.org/VERSION?m=text)"
DL_PKG="$LATEST.linux-amd64.tar.gz"
DL_URL="https://golang.org/dl/$DL_PKG"

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"
fi

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

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)"
else
        echo "$(current_version) is up to date"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment