centos 下安装go环境

10,291 阅读1分钟

centos下安装go一般 有两种方式,一个是yum,一个是直接到官网下载.tar.gz包
yum方式安装比较简单 直接执行

yum install golang

然后就可以使用go语言了,这种方式有着明显的缺点,因为都是yum自己处理的依赖,不方便管理

第二种方式是到官网下载.tar.gz包
标准官网:golang.org/ 需要墙
镜像官网:golang.google.cn/dl/ 【国内推荐】

本次安装的是go1.17.2

 wget https://dl.google.com/go/go1.17.2.linux-amd64.tar.gz
 tar -zxf go1.17.2.linux-amd64.tar.gz -C /usr/local

将go添加到环境变量

vim /etc/profile

在环境变量后面加入

[root@iZgj43d7rgpp6qZ local]# vim /etc/profile
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
    umask 002
else
    umask 022
fi

for i in /etc/profile.d/*.sh /etc/profile.d/sh.local ; do
    if [ -r "$i" ]; then
        if [ "${-#*i}" != "$-" ]; then
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done

unset i
unset -f pathmunge

if [ -n "${BASH_VERSION-}" ] ; then
        if [ -f /etc/bashrc ] ; then
                # Bash login shells run only /etc/profile
                # Bash non-login shells run only /etc/bashrc
                # Check for double sourcing is done in /etc/bashrc.
                . /etc/bashrc
       fi
fi
#go 环境变量
export GO111MODULE=on
export GOROOT=/usr/local/go
export GOPATH=/home/gopath
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

重点是后面加入的

#go 环境变量
export GO111MODULE=on
export GOROOT=/usr/local/go
export GOPATH=/home/gopath
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

应用环境变量

source /etc/profile

查看下go是否安装成功

image.png ok 安装成功