ubuntu下安装go环境,包括GOROOT和GOPATH配置
虽然之前安装过,但是还不够熟练,这次做个记录,方便以后翻看。
环境:在一台全新的Ubuntu机器上安装go。
2022.6.14
需求:用root安装go环境,全局配置,所以用户都能用。
wget -c https://go.dev/dl/go1.17.11.linux-amd64.tar.gz # 下载amd64,可通过 $ cat /proc/version 查看
tar xfz go1.17.2.darwin-amd64.tar.gz -C /usr/local
# 配置环境变量生效顺序,查看 https://www.jianshu.com/p/ba2907d8668f
/etc/profile,这也是linux默认的shell主环境变量,每个用户登录都会加载这个文件
/etc/profile.d/ ,自动定义开机启动的脚本可以放在这个目录下
$HOME/.bash_profile,用户个人的环境变量文件
在$HOME/.bash_profile文件中,会加载$HOME/.bashrc(也是用户个人的环境变量文件,包含在$HOME/.bash_profile中),如果有环境变量就加载,如果没有就不加载
在$HOME/.bashrc文件中,又会加载/etc/bashrc(这是全局环境变量),同理,有环境变量就加载,没有就不加载
#添加Gopath路径 export GOROOT=/usr/local/go export GOPATH=/data/go export PATH=$GOPATH/bin:$GOROOT/bin:$PATHsource /etc/profile #所有人都可以用,自己修改后要source才生效
$ cat /proc/version
Linux version 4.15.0-1065-aws (buildd@lgw01-amd64-035) (gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)) #69-Ubuntu SMP Thu Mar 26 02:17:29 UTC 2020
升级go版本
- 下载,tar.gz文件。
- 运行以下解压命令,覆盖旧版本。就成功了。
tar xfz go1.17.2.darwin-amd64.tar.gz -C /usr/local
1.下载Go语言安装包
官网:下载地址
2. 获取安装包
wget https://studygolang.com/dl/golang/go1.17.linux-amd64.tar.gz
# 解压文件
tar xfz go1.17.linux-amd64.tar.gz -C /usr/local
3. 配置环境变量
go语言的环境变量配置,需要两个值:
GOROOT
是系统上安装Go软件包的位置GOPATH
是工作目录的位置
#修改~/.profile vim ~/.profile #添加Gopath路径 export GOROOT=/usr/local/go export GOPATH=/data/go export PATH=$GOPATH/bin:$GOROOT/bin:$PATH # 激活配置 source ~/.profile
注意:尝试修改 ~/.bashrc,添加环境变量,并没有生效。
4.验证是否配置成功
$ go version
go version go1.17 linux/amd64
继续安装make
apt install make
sudo apt-get update
sudo apt-get install build-essential
安装与删除
apt install golang-go # 安装之后发现是1.10版本,太老,没法用 apt rmove golang-go # 卸载
早期版本
$ go version
Command 'go' not found, but can be installed with:
snap install go # version 1.16.2, or
apt install golang-go
apt install gccgo-go
See 'snap info go' for additional versions.
$ snap install go
error: This revision of snap "go" was published using classic confinement and thus may perform
arbitrary system changes outside of the security sandbox that snaps are usually confined to,
which may put your system at risk.
If you understand and want to proceed repeat the command including --classic.
虽然1.16.2是最新的,但我安装了1.15稳定版
$ snap install go --channel=1.15/stable --classic
go (1.15/stable) 1.15.10 from Michael Hudson-Doyle (mwhudson) installed
root@ip-172-31-44-132:/data# go version
Command 'go' is available in '/snap/bin/go'
The command could not be located because '/snap/bin' is not included in the PATH environment variable.
go: command not found
$ cd ~
$ vi .profile
添加
export PATH=$PATH:/snap/bin
$ source .profile
$ go version
go version go1.15.10 linux/amd64
$ make GenesisHeight=1121818 install
Command 'make' not found, but can be installed with:
apt install make
apt install make-guile
$ apt install make
$ apt install make-guile
$ make GenesisHeight=1121818 install
$ apt install gcc
$ gcc --version
$ make GenesisHeight=1121818 install
编译成功。
$ okexchaind version --long
okexchaind: command not found
添加环境变量:
$ vi .profile
添加
export PATH=$PATH:/root/go/bin
$ source .profile
命令行成功:
name: okexchain
server_name: okexchaind
client_name: okexchaincli
version: v0.16.8.3
commit: bd2ab2abdceb46c4cbdfef5c64af92b6b2711fb4
build_tags: netgo
go: go version go1.15.10 linux/amd64