Skip to content

Instantly share code, notes, and snippets.

@aayushjain
Last active February 11, 2020 12:59
Show Gist options
  • Save aayushjain/8a15b0c8e7b515f80e083ab3c6b710a9 to your computer and use it in GitHub Desktop.
Save aayushjain/8a15b0c8e7b515f80e083ab3c6b710a9 to your computer and use it in GitHub Desktop.
Script to install Go inside WSL in Windows 10
#!/bin/bash
set -e
GVERSION="1.13.7"
echo "Preparing to install Go $GVERSION"
MACHINE_TYPE=`uname -m`
if [ ${MACHINE_TYPE} == 'x86_64' ]; then
ARCH="amd64"
elif [ ${MACHINE_TYPE} == 'i386' ]; then
ARCH="386"
else
echo "Architecture not supported. Copy the correct url from https://golang.org/dl/ and replace in the DOWNLOAD_URL parameter in this shell script."
exit 1 #comment out this exit line when replacing URL
fi
DOWNLOAD_URL="https://dl.google.com/go/go$GVERSION.linux-$ARCH.tar.gz"
GOPATH="$HOME/projects/go"
GOROOT="/usr/local/go"
if [ -d $GOROOT ]; then
echo "Installation directories already exist $GOROOT"
exit 1
fi
mkdir -p "$GOROOT"
chmod 777 "$GOROOT"
echo "Downloading Go from $DOWNLOAD_URL"
wget -nc --no-verbose --show-progress $DOWNLOAD_URL -O $TMPDIR/GO_Download
if [ $? -ne 0 ]; then
echo "Go download failed! Exiting."
exit 1
fi
echo "Unpacking downloaded zips"
tar -C "/usr/local" -xzf $TMPDIR/GO_Download
echo "Backing up your .bashrc"
cp -f "$HOME/.bashrc" "$HOME/.bashrc.bkp"
echo "Setting GOROOT, GOPATH and GOBIN"
touch "$HOME/.bashrc"
{
echo ''
echo '# GOLANG'
echo 'export GOROOT='$GOROOT
echo 'export GOPATH='$GOPATH
echo 'export GOBIN=$GOPATH/bin'
echo 'export PATH=$PATH:$GOROOT/bin:$GOBIN'
echo ''
} >> "$HOME/.bashrc"
echo "Updated PATH, now refreshing current shell"
. ~/.bashrc
mkdir -p "$GOPATH" "$GOPATH/src" "$GOPATH/pkg" "$GOPATH/bin" "$GOPATH/out"
chmod 777 "$GOPATH" "$GOPATH/src" "$GOPATH/pkg" "$GOPATH/bin" "$GOPATH/out"
echo "GOPATH set to $GOPATH"
echo "Doing cleanup"
rm -f $TMPDIR/GO_Download
echo "Checking if Go installed"
go version
echo "Please close the current shell instance, and relaunch it to use Go."
@aayushjain
Copy link
Author

aayushjain commented May 10, 2019

This bash script installs Go lang tools in /usr/local/go, creates $HOME/go directory and sets GOPATH environment variable to $HOME/go/bin.

Run a one-line installer for this script:

curl -s -L https://gist.github.com/aayushjain/8a15b0c8e7b515f80e083ab3c6b710a9/raw/setup_go_in_wsl.sh | sudo bash

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