Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Install Golang on Linux
#!/bin/bash
## Install Golang 1.14.2 64Bits on Linux (Debian|Ubuntu|OpenSUSE|CentOS)
## http://www.linuxpro.com.br/2015/06/golang-aula-1-instalacao-da-linguagem-no-linux.html
## Run as root (sudo su)
## Thank's @geosoft1 | @gwmoura
GO_URL="https://storage.googleapis.com/golang"
GO_VERSION=${1:-"1.14.2"}
GO_FILE="go$GO_VERSION.linux-amd64.tar.gz"
# Check if user has root privileges
if [[ $EUID -ne 0 ]]; then
echo "You must run the script as root or using sudo"
exit 1
fi
GET_OS=$(cat /etc/os-release | head -n1 | cut -d'=' -f2 | awk '{ print tolower($1) }'| tr -d '"')
if [[ $GET_OS == 'debian' || $GET_OS == 'ubuntu' ]]; then
apt-get update && apt-get install -y wget git-core
fi
if [[ $GET_OS == 'opensuse' ]]; then
zypper in -y wget git-core
fi
if [[ $GET_OS == 'centos' || $GET_OS == 'amazon' ]]; then
yum -y install wget git-core
fi
cd /tmp
wget --no-check-certificate ${GO_URL}/${GO_FILE}
tar -zxf ${GO_FILE} -C /usr/local
rm -f ${GO_FILE}
echo 'export PATH=$PATH:/usr/local/go/bin
export GOPATH=$HOME/GO
export PATH=$PATH:$GOPATH/bin' >> /etc/profile
### You do not need to run commands with root or sudo
source /etc/profile
## mkdir -p $HOME/GO
## Test if Golang is working
go version
### The output is this:
## go version go1.7 linux/amd64
@geosoft1

This comment has been minimized.

Copy link

geosoft1 commented Jul 11, 2015

basicaly you do not need root rights to use go. you can extract in your /home/user directory an use it.
see this project how make the things:
https://github.com/geosoft1/tools

@jniltinho

This comment has been minimized.

Copy link
Owner Author

jniltinho commented Oct 26, 2015

@geosoft1 Script Update

@geosoft1

This comment has been minimized.

Copy link

geosoft1 commented Dec 8, 2015

remember to use wget --no-check-certificate otherwise it will not download on some distributions.

@jniltinho

This comment has been minimized.

Copy link
Owner Author

jniltinho commented Dec 13, 2015

@geosoft1 Thank's
Add my script

@ronoaldo

This comment has been minimized.

Copy link

ronoaldo commented Jun 1, 2016

Isn't --no-check-certificate a bad thing to do in a shared script? You are instructing your users to avoid validating the download source, so you are building a script that has a vulnerability for man-in-the-middle-attacks. One could tamper the Go compiled and users using your script can use a tampered go version to redistribute malicious code on ther programs. Just some toughts...

@gwmoura

This comment has been minimized.

Copy link

gwmoura commented Jun 1, 2016

@jniltinho, one suggestion:

GO_VERSION=${1:-"1.6.2"}
GO_FILE="go$GO_VERSION.linux-amd64.tar.gz"

With this change you can install a default value for Go ou another version

@jniltinho

This comment has been minimized.

Copy link
Owner Author

jniltinho commented Aug 24, 2016

@gwmoura

Thanks

Add

@random-robbie

This comment has been minimized.

Copy link

random-robbie commented Nov 1, 2017

apt-get install wget git-core

needs to be

apt-get install -y wget git-core

no need for manual approval then

@jniltinho

This comment has been minimized.

Copy link
Owner Author

jniltinho commented Dec 11, 2017

@random-robbie

Thanks !!!

@posilva

This comment has been minimized.

Copy link

posilva commented Dec 18, 2017

Replacing if [[ $GET_OS == 'centos' ]]; then by if [[ $GET_OS == 'centos' || $GET_OS == 'amazon' ]]; then would be great to use in AWS EC2.

Add some echo like Run command as non-root to init GOPATH: 'mkdir -p $HOME/GO/src' in the end would give additional instruction for the final tweaks.

Great script thank you

Pedro

@udhos

This comment has been minimized.

Copy link

udhos commented Jun 14, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.