linux安装go环境教程,Linux下一键安装Go语言最新版本环境的教程

有点 Shell 基础,而且经常安装 Golang 的环境,为了方便自己,就写了个一键安装 golang 的环境教程,之前写过一次,但是不够智能,现在添加了几个功能。

1、可以自定义 Golang 版本号;

2、自动判断系统的相关信息(如32位or64位系统)及下载对应的地址

MY_DIY_GO_VERSION 为自定义的 golang 版本号,如:1.6.2

###第一种方式: 直接通过我写好的脚本一键安装(一键安装)。

截止发文时,Go 版本号为:1.7rc3

### Ubuntu/CentOS:

> 脚本默认版本号

Wget方式:wget -qO- https://down.zzzzy.com/shell.install.go | sh

Curl方式:curl -SL https://down.zzzzy.com/shell.install.go | sh

> 自定义版本号

Wget方式:wget -qO- https://down.zzzzy.com/shell.install.go | bash /dev/stdin MY_DIY_GO_VERSION

Curl方式:curl -SL https://down.zzzzy.com/shell.install.go | bash /dev/stdin MY_DIY_GO_VERSION

注意:

建议在上面的 URL 后添加一些参数,防止脚本的缓存过旧。

如:https://down.zzzzy.com/shell.install.go?abc

###第二种方式:

将下面的代码复制到Linux服务器下任意一个目录,并将文件名改成 "xxx.sh",再执行 sh xxx.sh MY_DIY_GO_VERSION

#!/bin/bash

# Author: Skiychan

# Link: https://www.skiy.net

#

#

# Project home page:

# https://github.com/skiy

# Set Golang Version

go_version="1.7rc3"

# DIY version

if [ -n "$1" ] ;then

go_version=$1

fi

# Printf Version Info

clear

printf "

#########################################

# Author Skiychan #

# Link http://www.skiy.net #

#########################################

"

# Check if user is root

[ $(id -u) != "0" ] && { echo "${CFAILURE}Error: You must be root to run this script${CEND}"; exit 1; }

echo=echo

for cmd in echo /bin/echo; do

$cmd >/dev/null 2>&1 || continue

if ! $cmd -e "" | grep -qE '^-e'; then

echo=$cmd

break

fi

done

# Set Color

CSI=$($echo -e "\033[")

CEND="${CSI}0m"

CDGREEN="${CSI}32m"

CRED="${CSI}1;31m"

CGREEN="${CSI}1;32m"

CYELLOW="${CSI}1;33m"

CBLUE="${CSI}1;34m"

CMAGENTA="${CSI}1;35m"

CCYAN="${CSI}1;36m"

CSUCCESS="$CDGREEN"

CFAILURE="$CRED"

CQUESTION="$CMAGENTA"

CWARNING="$CYELLOW"

CMSG="$CCYAN"

#Check Linux Version

if [ -f /etc/redhat-release -o -n "`grep 'Aliyun Linux release' /etc/issue`" ];then

OS=CentOS

[ -n "`grep ' 7\.' /etc/redhat-release`" ] && CentOS_RHEL_version=7

[ -n "`grep ' 6\.' /etc/redhat-release`" -o -n "`grep 'Aliyun Linux release6 15' /etc/issue`" ] && CentOS_RHEL_version=6

[ -n "`grep ' 5\.' /etc/redhat-release`" -o -n "`grep 'Aliyun Linux release5' /etc/issue`" ] && CentOS_RHEL_version=5

elif [ -n "`grep bian /etc/issue`" ];then

OS=Debian

Debian_version=`lsb_release -sr | awk -F. '{print $1}'`

elif [ -n "`grep Ubuntu /etc/issue`" ];then

OS=Ubuntu

Ubuntu_version=`lsb_release -sr | awk -F. '{print $1}'`

else

echo "${CFAILURE}Does not support this OS, Please contact the author! ${CEND}"

kill -9 $$

fi

# Check Linux Bit

if [ $(getconf WORD_BIT) = '32' ] && [ $(getconf LONG_BIT) = '64' ] ; then

sys_bit="amd64"

else

sys_bit="386"

fi

printf "

#########################################

# System: %s #

# Bit: %s #

# Golang %s Install #

#########################################

\n" $OS $sys_bit $go_version

# Install Curl Lib for Linux

if [ $OS = 'CentOS' ];then

yum update -y

yum install -y curl

else

apt-get update -y

apt-get install -y curl

fi

# Download Golang Package, Then Unzip to /usr/local

GO_DOWNLOAD=https://storage.googleapis.com/golang/go$go_version.linux-$sys_bit.tar.gz

printf "

download url: %s

\n" $GO_DOWNLOAD

rm go.tar.gz -rf

curl --retry 10 --retry-delay 60 --retry-max-time 60 -C - -SL -o go.tar.gz $GO_DOWNLOAD && \

#curl -SL -o go.tar.gz http://golangtc.com/static/go/go$go_version/$go_version.linux-amd64.tar.gz && \

tar -C /usr/local/ -zxf go.tar.gz && \

rm go.tar.gz -rf

# Create GOPATH

mkdir -p /data/go

# Set ENV for Golang

cat <> /etc/profile

export GOROOT=/usr/local/go

export GOPATH=/data/go

export PATH=\$GOROOT/bin:\$PATH

EOF

# Make Env Is Enable

source /etc/profile

go env

go version

# Printf Tip

printf "

##############################################

# 安装成功,请再次执行 source /etc/profile #

##############################################

"

###注意

安装成功,请再次执行 source /etc/profile

你可能感兴趣的:(linux安装go环境教程)