从源代码安装Go1.6到CentOS 7

在中国网络环境下从源代码安装Go1.6到CentOS 7

https://github.com/northbright/Notes/blob/master/Golang/china/install-go1.6-from-source-on-centos7-in-china.md

背景

  • 在当前的中国网络环境下,我们无法访问Google的服务的,包括Golang.org。
  • 从第三方网站下载预编译的二进制Go发行版可能存在第三方源代码注入的风险,例如之前的XcodeGhost
  • Go的权威仓库地址是https://go.googlesource.com/go,同时还有一个镜像仓库在https://github.com/golang/go
  • 至少在目前,我们还能访问github:-)

问题

  • Go1.6的编译过程需要Go1.4的二进来实现 bootstrap(自举)(简单来说: Go需要Go自身来编译)。

解决方案

  • Github的Go仓库镜像获取Go的源代码。
  • 首先编译Go1.4(只需要gccglibc-devel,不需要Go来编译)。
  • 使用编译好的Go1.4的二进制文件来编译Go1.6。

步骤

  1. 如果之前已经安装过老版本的Go,清除$GOPATH, $GOROOT变量。

  2. 安装好Git

    • 配置 Git
      • git config --global user.email "email-for-github"
      • git config --global user.name "user name"
    • 创建SSH key,添加Public Key到Github账号。
      • ssh-keygen -t rsa -b 2048
      • 复制~/.ssh/id_rsa.pub中的Public Key到github SSH settings以添加新的SSH key
  3. 安装 gccglibc-devel

    • sudo yum install gcc glibc-devel
  4. 从源代码编译安装Go1.4

    • cd ~/
    • git clone [email protected]:golang/go.git
    • cd go
    • git checkout -b 1.4.3 go1.4.3
    • cd src
    • ./all.bash
  5. 复制 ~/go 到 $GOROOT_BOOTSTRAP(默认值是~/go1.4

    • cp ~/go ~/go1.4 -rf
  6. 从源代码编译安装Go1.6

    • cd ~/go
    • git clean -dfx
    • git checkout -b 1.6 go1.6
    • cd src
    • ./all.bash
  7. 设置 $GOPATH 以及添加Go二进制路径到 $PATH

    • sudo vi /etc/profile

        # Golang Env
        export PATH=$PATH:/home/xx/go/bin
        export GOPATH=/home/xx/go-projects
      
  8. 重启和测试

    • sudo reboot
    • go version

        go version go1.6 linux/amd64

猜你喜欢

转载自my.oschina.net/u/124197/blog/785712