从源代码安装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 git@github.com: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

转载于:https://my.oschina.net/u/124197/blog/785712

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值