wget https://storage.googleapis.com/golang/go<VERSION>.src.tar.gz
tar -zxvf go<VERSION>.src.tar.gz
sudo mv go /user/local/
默認(rèn)放到/user/local/目錄下酗洒,然后進(jìn)行安裝
cd /usr/local/go/src
./all.bash
執(zhí)行之后發(fā)現(xiàn)報(bào)錯(cuò)
vagrant@homestead:/usr/local/go/src$ ./all.bash
##### Building Go bootstrap tool.
cmd/dist
ERROR: Cannot find /home/vagrant/go1.4/bin/go.
Set $GOROOT_BOOTSTRAP to a working Go tree >= Go 1.4.
為什么會(huì)報(bào)錯(cuò)找不到go1.4呢?不是還沒安裝呢嗎?原來(lái)編譯GO 1.6版本以上的需要依賴GO 1.4版本的二進(jìn)制
于是要先編譯安裝1.4版本
- 首先要確定是否安裝了 gcc和glibc-devel
$ cd ~/
$ git clone git@github.com:golang/go.git
$ cd go/
$ git checkout -b 1.4 go1.4 //將遠(yuǎn)程的go1.4分支checkout
$ cd ~/go/src
$ sudo ./all.bash
$ cp -rf ~/go/ ~/go1.4
編譯過(guò)程中遇到了報(bào)錯(cuò)
# cmd/pprof
/home/vagrant/go/pkg/linux_amd64/runtime/cgo.a(_all.o): unknown relocation type 42; compiled without -fpic?
/home/vagrant/go/pkg/linux_amd64/runtime/cgo.a(_all.o): unknown relocation type 42; compiled without -fpic?
runtime/cgo(.text): unexpected relocation type 298
runtime/cgo(.text): unexpected relocation type 298
解決方案:
$ cd ~/go1.4/src
$ sudo CGO_ENABLED=0 ./make.bash
導(dǎo)致上述問(wèn)題的原因是:CGO_ENABLED: Controls cgo usage during the build. Set it to 1 to include all cgo related files, .c and .go file with “cgo” build directive, in the build. Set it to 0 to ignore them.在構(gòu)建過(guò)程中控制cgo的使用颅围。當(dāng)設(shè)置為1蘸秘,在構(gòu)建時(shí),會(huì)包含所有cgo相關(guān)的文件刻坊,如帶有”cgo”編譯指令的.c和.go文件枷恕。當(dāng)設(shè)置為0,則忽略它們(即禁用CGO)
編譯完了go1.4版本谭胚,然后就可以編譯v1.9.2了
$ cd ~/go
$ git clean -dfx //刪除當(dāng)前目錄下所有沒有track過(guò)的文件. 不管他是否是.gitignore文件里面指定的文件夾和文件
$ git checkout -b 1.9.2 go1.9.2
$ cd ~/go/src
$ sudo ./all.bash
- 配置環(huán)境變量
在 ~/.profile文件末尾追加如下命令徐块,在打開一個(gè)回話的時(shí)候未玻,會(huì)自動(dòng)執(zhí)行~/.profile,配置環(huán)境變量
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
export GOPATH=/usr/local/go/production