#0 官网方式
-
Remove any previous Go installation by deleting the /usr/local/go folder (if it exists), then extract the archive you just downloaded into /usr/local, creating a fresh Go tree in /usr/local/go:
$ rm -rf /usr/local/go && tar -C /usr/local -xzf go1.19.3.linux-amd64.tar.gz(You may need to run the command as root or through
sudo).Do not untar the archive into an existing /usr/local/go tree. This is known to produce broken Go installations.
-
Add /usr/local/go/bin to the
PATHenvironment variable.You can do this by adding the following line to your $HOME/.profile or /etc/profile (for a system-wide installation):
export PATH=$PATH:/usr/local/go/binNote: Changes made to a profile file may not apply until the next time you log into your computer. To apply the changes immediately, just run the shell commands directly or execute them from the profile using a command such as
source $HOME/.profile. -
Verify that you've installed Go by opening a command prompt and typing the following command:
$ go version -
Confirm that the command prints the installed version of Go.
#1、最简单方法
参照官网wiki,本方法直接升级到最新版,不过网络是个大问题
sudo add-apt-repository ppa:longsleep/golang-backports
sudo apt-get update
sudo apt-get install golang-go
#2、查看go安装路径
whereis go
#3、如果需要先卸载旧版本
#先查看旧版本路径
echo $GOROOT
#一般是在/usr这个文件夹下面 如/usr/lib 或 /usr/bin /usr/local
vagrant@homestead:/usr/local$ sudo rm -rf go/
vagrant@homestead:/usr/local$ sudo rm -rf /usr/bin/go
#4 配置环境变量
#sudo vim $HOME/.profile
#sudo vim ~/.profile
sudo vim $HOME/.bashrc
###加入以下内容
#go env
export GOROOT=/usr/lib/go #设置为go安装的路径,有些安装包会自动设置默认的goroot
export GOPATH=/var/go #默认安装包的路径
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
# 启用 Go Modules 功能
export GO111MODULE=on
# 配置 GOPROXY 环境变量
export GOPROXY=https://goproxy.io
###end
source $HOME/.bashrc