Updating golang on Ubuntu

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.

After searching I found the way to do this would be to first delete current go installation and then re-install the new version.

I found the following go installation files in the following directory:

/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

Which directory contains what? Which ones should I delete? What should be the location of new installation? It seems /usr/local is more normal. Then how come Ubuntu default is as above?

What would be the procedure to install a new version og golang?

Thanks.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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

For more detailed installation instruction: Install the latest Golang on Linux

Step 1: Remove the existing golang

sudo apt-get purge golang*

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

Step 3: Extract it in /usr/local using the following command. I am using Go 1.11.4 here. You may need to replace the filename with the actual filename based on the version you have downloaded.

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

Step 4: Create .go directory in home. (It is easy to install the necessary packages without admin privilege)

mkdir ~/.go

Step 5: Set up the following environment variables

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

Check this link on how to set environment variables permanently.

Step 6: Update the go command

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

Solution 2

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

Now you can use

sudo apt-get install golang

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

Solution 3

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.

Solution 4

For Golang 1.8 on Ubuntu 17.04 Zesty to 17.10 Artful:

# 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

For Golang 1.9 on Ubuntu 17.10 Artful to 18.04 Bionic:

# 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

For Golang 1.10 on Ubuntu 18.04 Bionic and 18.10 Cosmic:

# 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

Solution 5

To install the latest version of Go programming language in all currently supported versions of Ubuntu open the terminal and type:

sudo snap install go --classic --channel stable 

You might need to install snapd support because this is not installed by default on server installations.

sudo apt install snapd && sudo reboot

This snap provides an assembler, compiler, linker, and compiled libraries for the Go programming language. There are other channels besides stable, but the stable channel is the latest version of Go programming language. The go snap package will be updated automatically when updates are available.

Solution 6

Easy install and update

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

Solution 7

You can also use the update-golang script:

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

Solution 8

I use this script to automate Golang updates:

https://github.com/udhos/update-golang

Solution 9

Remove existing golang

sudo apt-get purge golang*

Install snap if not yet installed

sudo apt update
sudo apt install snapd

Install via snap

sudo snap install go --classic

Solution 10

Learn latest version from: https://golang.org/dl/

sudo apt-get purge golang*  # in case there is old version of go is installed

tar_name="go1.15.8.linux-amd64.tar.gz"  # get latest version from https://golang.org/dl/
wget https://golang.org/dl/$tar_name
tar -xvf $tar_name
rm -f $tar_name
sudo rm -rf /usr/local/go
sudo mv go /usr/local

# Add following lines into `.bashrc` or `.zshrc`
mkdir -p $HOME/go
export GOPATH=$HOME/go
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

Solution 11

I use this instruction to install the latest version of IMG:

  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
    

Test:

$ go version
go version go1.11.1 linux/amd64

Ref: Go-GitHub-Installation

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply