在Ubuntu上更新golang

收藏

I have Ubuntu 14.4.03. The default golang version which came with it was 1.2.1. I would like to update golang to 1.4 or higher.

搜索后,我发现执行此操作的方法是先删除当前的go安装,然后重新安装新版本。

我在以下目录中找到了以下go安装文件:

/usr/share/go
/usr/share/go/src/cmd/go
/usr/share/go/src/pkg/go
/usr/lib/go
/ur/lib/go/pkg/linux_amd64/go
/ur/lib/go/pkg/linux_amd64_race/go
/usr/bin/go

哪个目录包含什么?我应该删除哪些?新安装的位置应该在哪里?看来/ usr / local更正常。那么Ubuntu的默认默认值是多少呢?

安装新版本的golang的程序是什么?

谢谢。

回复
  • 少心怡 回复

    对于Ubuntu 17.04 Zesty到17.10 Artful上的Golang 1.8:

    # sudo apt update
    # sudo apt install golang-1.8 golang-1.8-doc
    # for bin in /usr/lib/go-1.8/bin/* ; do
        sudo update-alternatives --install /usr/bin/$(basename $bin) $(basename $bin) $bin 0
        sudo update-alternatives --set $(basename $bin) $bin
      done
    

    对于Ubuntu 17.10 Artful到18.04 Bionic上的Golang 1.9:

    # sudo apt update
    # sudo apt install golang-1.9 golang-1.9-doc
    # for bin in /usr/lib/go-1.9/bin/* ; do
        sudo update-alternatives --install /usr/bin/$(basename $bin) $(basename $bin) $bin 0
        sudo update-alternatives --set $(basename $bin) $bin
      done
    

    对于Ubuntu 18.04 Bionic和18.10 Cosmic上的Golang 1.10:

    # sudo apt update
    # sudo apt install golang-1.10 golang-1.10-doc
    # for bin in /usr/lib/go-1.10/bin/* ; do
        sudo update-alternatives --install /usr/bin/$(basename $bin) $(basename $bin) $bin 0
        sudo update-alternatives --set $(basename $bin) $bin
      done
    

  • et_et 回复

    要在当前所有受支持的Ubuntu版本中安装最新版本的Go编程语言,请打开终端并输入:

    sudo snap install go --classic --channel stable 
    

    该快照为Go编程语言提供了汇编程序,编译器,链接器和已编译的库。除了稳定版之外,还有其他渠道,但是稳定版渠道是Go编程语言的最新版本。可用更新时,go snap软件包将自动更新。

  • cet 回复

    我使用此说明来安装最新版本的

    1. Remove the existing Go version:

      sudo apt-get purge golang*
      
    2. Install the latest version of Go:

      sudo add-apt-repository ppa:longsleep/golang-backports
      sudo apt-get update
      sudo apt-get install golang-go
      
    3. Create the .profile file in the home path with this content:

      # ~/.profile: executed by the command interpreter for login shells.
      # This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
      # exists.
      # see /usr/share/doc/bash/examples/startup-files for examples.
      # the files are located in the bash-doc package.
      
      # the default umask is set in /etc/profile; for setting the umask
      # for ssh logins, install and configure the libpam-umask package.
      #umask 022
      
      # if running bash
      if [ -n "$BASH_VERSION" ]; then
          # include .bashrc if it exists
          if [ -f "$HOME/.bashrc" ]; then
              . "$HOME/.bashrc"
          fi
      fi
      
      # set PATH so it includes user's private bin directories
      PATH="$HOME/bin:$HOME/.local/bin:$PATH"
      
    4. Set Go workspace to the environment variable:

      GOPATH=~/.go
      
    5. Apply the .profile configuration:

      source ~/.profile
      

    测试:

    $ go version
    go version go1.11.1 linux/amd64
    

    Ref: Go-GitHub-Installation

  • 怪我瞎i 回复

    您还可以使用update-golang脚本:

    update-golang is a script to easily fetch and install new Golang releases with minimum system intrusion

    git clone https://github.com/udhos/update-golang
    cd update-golang
    sudo ./update-golang.sh
    

  • 自娱自乐 回复

    易于安装和更新

    Use Go Version Manager

    # Add basic packages
    sudo apt-get install curl git mercurial make binutils bison gcc build-essential
    
    # Install GVM
    bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)
    
    # Install which version you want to install
    gvm install go1.9
    
    # Use it as default version
    gvm use go1.9 --default
    

  • Honey 回复

    First remove your current golang installation with this command, you don't need to manually remove files installed by apt-get,

    sudo apt-get purge golang
    

    For an easy install of golang 1.4 you can use this PPA

    sudo add-apt-repository ppa:evarlast/golang1.4
    sudo apt-get update
    

    现在您可以使用

    sudo apt-get install golang
    

    This will install version 1.4, here is a link to the PPA.

  • yeahlife 回复

    I have found an alternative(/personal) repo with latest versions, which is still alive, then got the go-1.8 and linked it from the path in the OP's question. Here be bash it in:

    sudo add-apt-repository ppa:gophers/archive
    sudo apt-get update
    
    sudo apt-get install golang-1.8
    
    sudo ln /usr/lib/go-1.8/bin/go /usr/bin/go1.8
    

    Now I can just say go1.8 instead of go and thus go with the latest&greatest.

    The repo offers many other versions so you'd just change the version number to ex. 1.4 in the script/commands above.

  • sit_in 回复

    ppa:evarlast/golang1.4 is not working for me. The manual way of installing Go is given below.

    步骤1:移除现有的golang

    sudo apt-get purge golang*
    

    Step 2: Download the latest version from the official site. Click Here

    步骤3:使用以下命令将其解压缩到/ usr / local中。我在这里使用Go 1.11.4。您可能需要根据下载的版本将文件名替换为实际的文件名。

    tar -C /usr/local -xzf go1.11.4.linux-amd64.tar.gz
    

    步骤4:在主目录中创建.go目录。 (无需管理员权限即可轻松安装必要的软件包)

    mkdir ~/.go
    

    步骤5:设置以下环境变量

    GOROOT=/usr/local/go
    GOPATH=~/.go
    PATH=$PATH:$GOROOT/bin:$GOPATH/bin
    

    Check this AskUbuntu answer for how to set environment variables permanently.

    步骤6:更新go命令

    sudo update-alternatives --install "/usr/bin/go" "go" "/usr/local/go/bin/go" 0
    sudo update-alternatives --set go /usr/local/go/bin/go
    

    Step 7: Test the golang version

    go version