167

Running sudo apt-get install golang-stable, I get Go version go1.0.3. Is there any way to install go1.1.1?

xpt
  • 20,363
  • 37
  • 127
  • 216
scc
  • 10,342
  • 10
  • 51
  • 65
  • 1
    In the end I had to install from source. Not all tests passed but it seems to be working ok. – scc Jul 05 '13 at 12:50
  • 1
    "Not all tests passed but it seems to be working ok." @sofia what version of Ubuntu are you using? I would not expect test failures unless you were running a very old version of Ubuntu or you accidentally installed "tip" vs. "stable". – voidlogic Jul 09 '13 at 02:41
  • I'm using ubuntu precise. But installing the latest version from the repo duh/golang didn't report any problem, so not sure what the problem was. – scc Jul 10 '13 at 09:22
  • I am also using precise. Did you run "hg update tip" or "hg update release". If you don't recall look back in your bash history file. Using tip might explain the test failures. – voidlogic Jul 10 '13 at 13:49
  • 1
    You can try this blog http://railskey.wordpress.com/2014/05/31/install-gogolang-on-ubuntu/ – Pravin Mishra May 31 '14 at 05:44
  • As of July 2014, I used `apt-get install -f gccgo-go` – Leahcim Jul 27 '14 at 16:28
  • It's precompiled for linux 32/64, just untar it: http://golang.org/doc/install#tarball and update your `PATH` – jpillora Aug 28 '14 at 01:35
  • Related: [Updating golang on Ubuntu](https://askubuntu.com/q/720260/78223). – kenorb Jul 09 '18 at 16:01

19 Answers19

163

[Updated (previous answer no longer applied)]

For fetching the latest version:

sudo add-apt-repository ppa:longsleep/golang-backports
sudo apt update
sudo apt install golang-go

Also see the wiki

scc
  • 10,342
  • 10
  • 51
  • 65
  • 44
    So as of 2014 July, this no longer has the latest version of Go. Current version is 1.3. After trying this on Ubuntu 12.04, I ended up with version 1.1.1. – 425nesp Jul 22 '14 at 23:29
  • 4
    Please update this. As of Sep 2014, I also end up with "go1.1.2" – Riccardo Oct 01 '14 at 19:21
  • 2
    As of 2014-10-24, I got 1.2.1 linux/amd64. – nairware Oct 25 '14 at 02:25
  • 5
    Checking in on March 29th, 2015. That ppa responds with a 404. – btleffler Mar 29 '15 at 13:49
  • 8
    I used ``ppa:evarlast/golang1.4`` – Natim May 21 '15 at 15:39
  • 2
    As of October 2015, **sudo apt-get install golang** is still not uptodate. It installed version 1.2.1. – kemicofa ghost Oct 13 '15 at 22:00
  • 1
    I don't know if it's a good idea to add unknown community repos willy-nilly It's probably better to install directly from the go website. https://golang.org/dl/ https://golang.org/doc/install Be sure to uninstall the old version first. https://golang.org/doc/install#uninstall – jorfus Jan 07 '16 at 18:22
  • This `export GOBIN=/usr/bin/go` seems like not what you want to do. `export GOBIN=$GOPATH/bin` or `export GOBIN=$HOME/workspace/go/bin` seems more likely. – a113nw Jan 26 '16 at 21:30
  • @jorfus The problem with installing directly as recommended at golang.org is that there is no automatic update mechanism to fix security issues. See [software installation - How to install a recent, managed version of golang on Ubuntu 14.04? - Ask Ubuntu](http://askubuntu.com/questions/847867/how-to-install-a-recent-managed-version-of-golang-on-ubuntu-14-04) – nealmcb Nov 10 '16 at 15:45
  • `Cannot add PPA: 'ppa:~duh/ubuntu/golang'. The user named '~duh' has no PPA named 'ubuntu/golang'.` – kenorb Jul 09 '18 at 15:56
  • 1
    As of August 2020, You need not add the ppa repo. Just use `sudo apt install golang-go` (For Ubuntu 20.04) – Trect Aug 01 '20 at 16:31
  • software-properties-common – resultsway Dec 04 '20 at 02:30
104

I like to use GVM for managing my Go versions in my Ubuntu box. Pretty simple to use, and if you're familiar with RVM, it's a nobrainer. It allows you to have multiple versions of Go installed in your system and switch between whichever version you want at any point in time.

Install GVM with:

sudo apt-get install bison mercurial
bash < <(curl -LSs 'https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer')
. "$HOME/.gvm/scripts/gvm"

and then it's as easy as doing this:

gvm install go1.1.1
gvm use go1.1.1 --default

The default flag at the end of the second command will set go1.1.1 to be your default Go version whenever you start a new terminal session.

smac89
  • 39,374
  • 15
  • 132
  • 179
noj
  • 2,542
  • 1
  • 14
  • 9
  • 1
    I like RVM and always enjoy a good tool in my kit. Glad to see as I get into Go that there's a tool like GVM :) – Brenden Oct 12 '13 at 03:49
  • 2
    gvm is same name as for grails gvm – jcalloway Sep 23 '14 at 22:54
  • 3
    I just tried using GVM under the fish shell, and a warning to those who don't use bash: GVM will be a pain to get up and running properly. A lot of its scripts don't have a shebang line for example. – Dennis Feb 13 '15 at 01:47
  • the "GVM" web page referenced `https://github.com/moovweb/gvmt` is 404 not found. And I'm reluctant to install anything that doesn't seem to have its own security update procedure – nealmcb Nov 10 '16 at 15:49
  • I do not agree that it's as easy as "gvm install" and "gvm use". – kemicofa ghost Dec 29 '16 at 10:01
43

I used following commands from GoLang official repository, it installed GoLang version 1.6 on my Ubuntu 14.04

sudo add-apt-repository ppa:ubuntu-lxc/lxd-stable
sudo apt-get update
sudo apt-get install golang

However, since this answer was created, the instructions have been updated to these, for Ubuntu 18.04, 20.04 or 22.04 (amd64, arm64 or armhf):

sudo add-apt-repository ppa:longsleep/golang-backports
sudo apt update
sudo apt install golang-go

Reference official GoLang Repo https://github.com/golang/go/wiki/Ubuntu it seems this ppa will always be updated in future.

Brōtsyorfuzthrāx
  • 4,387
  • 4
  • 34
  • 56
Pankaj Khairnar
  • 3,028
  • 3
  • 25
  • 34
27

[October 2015] Answer because the current accepted answersudo apt-get install golang isn't uptodate and if you don't want to install GVM follow these steps.

Step by step installation:

  1. Download the latest version here (OS: Linux).
  2. Open your terminal and go to your Downloads directory
  3. sudo tar -C /usr/local -xzf go$VERSION.$OS-$ARCH.tar.gz
  4. Add go to your path export PATH=$PATH:/usr/local/go/bin
  5. go version to check the current version installed
  6. Start programming.

Possible errors + fixes: (to be updated)

If you get a go version xgcc (Ubuntu 4.9.1-0ubuntu1) 4.9.1 linux/amd64 then you did something wrong, so check out this post: Go is printing xgcc version but not go installed version

Community
  • 1
  • 1
kemicofa ghost
  • 16,349
  • 8
  • 82
  • 131
18

i installed from source. there is a step-by-step tutorial here: http://golang.org/doc/install/source

mnagel
  • 6,729
  • 4
  • 31
  • 66
9

Here is the most straight forward and simple method I found to install go on Ubuntu 14.04 without any ppa or any other tool.

As of now, The version of GO is 1.7

Get the Go 1.7.tar.gz using wget

wget https://storage.googleapis.com/golang/go1.7.linux-amd64.tar.gz

Extract it and copy it to /usr/local/

sudo tar -C /usr/local -xzvf go1.7.linux-amd64.tar.gz

You have now successfully installed GO. Now You have to set Environment Variables so you can use the go command from anywhere.

To achieve this we need to add a line to .bashrc So,

sudo nano ~/.bashrc

and add the following line to the end of file.

export PATH="/usr/local/go/bin:$PATH"

Now, All the commands in go/bin will work.

Check if the install was successful by doing

go version

For offline Documentation you can do

godoc -http=:6060

Offline documentation will be available at http://localhost:6060

NOTE:

Some people here are suggesting to change the PATH variable.

It is not a good choice.

  1. Changing that to /usr/local/go/bin is temporary and it'll reset once you close terminal.

  2. go command will only work in terminal in which you changed the value of PATH.

  3. You'll not be able to use any other command like ls, nano or just about everything because everything else is in /usr/bin or in other locations. All those things will stop working and it'll start giving you error.

However, this is permanent and does not disturbs anything else.

Ishan Jain
  • 665
  • 6
  • 20
9

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

Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150
6

For the current release of Go:

The Go Programming Language

Getting Started

Download the Go distribution

Downloads

Click the link above to visit the Go project's downloads page and select the binary distribution that matches your operating system and processor architecture.

Official binary distributions are available for the FreeBSD, Linux, macOS, and Windows operating systems and the 32-bit (386) and 64-bit (amd64) x86 processor architectures.

If a binary distribution is not available for your combination of operating system and architecture you may want to try installing from source or installing gccgo instead of gc.

Installing Go from source

Community
  • 1
  • 1
peterSO
  • 158,998
  • 31
  • 281
  • 276
  • Installing from source is actually really fast. It compiles fast and the tests are optional if you choose to skip them. – Jeremy Wall Jul 05 '13 at 06:02
6

If someone is looking for installing Go 1.8 the follow this:

sudo add-apt-repository ppa:longsleep/golang-backports
sudo apt-get update
sudo apt-get install golang-go

And then install go

sudo apt-get install golang-1.8-go
rakibtg
  • 5,521
  • 11
  • 50
  • 73
  • Works flawless for me. Thanks! – Gwyneth Llewelyn Apr 21 '17 at 22:45
  • 1
    No need to install `golang-1.8-go`, it's already installed when installing `golang-go`. Actually as of end April 2017, go **1.8.1** is installed, the latest version. https://github.com/golang/go/wiki/Ubuntu – firepol Apr 26 '17 at 11:10
  • I guess you also need to set up GOROOT, GOPATH – Nam G VU Aug 08 '17 at 03:25
  • Reference for GOROOT and GOPATH setting right in other answers 1) https://stackoverflow.com/a/34384978/248616 2) https://stackoverflow.com/questions/17480044/how-to-install-the-current-version-of-go-in-ubuntu-precise#comment65480082_39052584 More official reference https://stackoverflow.com/a/10847122/248616 – Nam G VU Aug 08 '17 at 03:43
  • For each of our Go project, set GOPATH=/some/dir, PATH=${GOPATH}/bin:${PATH} – Nam G VU Aug 08 '17 at 03:46
  • With GOROOT set in .bashrc `export GOROOT=/usr/local/bin/go` – Nam G VU Aug 08 '17 at 03:46
5
  1. Download say, go1.6beta1.linux-amd64.tar.gz from https://golang.org/dl/ into /tmp

wget https://storage.googleapis.com/golang/go1.6beta1.linux-amd64.tar.gz -o /tmp/go1.6beta1.linux-amd64.tar.gz

  1. un-tar into /usr/local/bin

sudo tar -zxvf go1.6beta1.linux-amd64.tar.gz -C /usr/local/bin/

  1. Set GOROOT, GOPATH, [on ubuntu add following to ~/.bashrc]

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

  1. Verify

go version should show be

go version go1.6beta1 linux/amd64

go env should show be

GOARCH="amd64" GOBIN="" GOEXE="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOOS="linux" GOPATH="/home/vats/go" GORACE="" GOROOT="/usr/local/bin/go" GOTOOLDIR="/usr/local/bin/go/pkg/tool/linux_amd64" GO15VENDOREXPERIMENT="1" CC="gcc" GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0" CXX="g++" CGO_ENABLED="1"

suryakrupa
  • 3,852
  • 1
  • 25
  • 34
  • Hi, thank you for answer, I am following all the steps but whenever I call "go version" instead of 1.7 it bring 1.4.2 . Is there any other actions that should be done ? – Anahit Serobyan Nov 18 '16 at 07:38
4

These days according to the golang github with for Ubuntu, it's possible to install the latest version of Go easily via a snap:

Using snaps also works quite well:

# This will give you the latest version of go
$ sudo snap install --classic go

Potentially preferable to fussing with outdated and/or 3rd party PPAs

kjones
  • 1,339
  • 1
  • 13
  • 28
2

On recent Ubuntu (20.10) sudo apt-get install golang works fine; it will install version 1.14.

user2023370
  • 10,488
  • 6
  • 50
  • 83
0

Or maybe you could use this script to install Go and LiteIDE?

If you are unhappy with the answer provided, please comment instead of blindly down voting. I have used this setup for the last 4 years without any issue.

thoroc
  • 3,291
  • 2
  • 27
  • 34
0

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
Everton
  • 12,589
  • 9
  • 47
  • 59
0

This is an old question in 2022 it is almost more that 9 years. But I think it is important to automatize this installation.

Requirements:

  • This was tested on Ubuntu 18 and 20.
  • Sudoer user, you must be able to use sudo.

Installation:

  1. Create a new file using this command:
touch ~/go-installer.sh
  1. Edit the file using any of editor available i.e., vi, vim, or nano.
nano ~/go-installer.sh
  1. Past this script:
#!/bin/bash

## Define version and build
VERSION=1.19
BUILD=4

## Define package name
PKG_NAME='go'

## Define go official url
GO_URL='https://golang.org/dl/'

## Define local path
LOCAL_PATH='/usr/local'

## Define system library path
LIB_PATH='/usr/lib'

## Define system binary path
BIN_PATH='/usr/bin'

## Define EV script filename
EV_FILE='/etc/profile.d/go-bin-path.sh'

## Defile export path command
EXPORT_CMD='export PATH=$PATH:/usr/local/go/bin'

## Define script content
SCRIPT='#!/bin/sh\n'${EXPORT_CMD}

## Define user home dir
USER_HOME_DIR=$(getent passwd ${SUDO_USER:-$USER} | cut -d: -f6)

check_command() {
    [ -x "$(command -v $PKG_NAME)" ] && echo 0 || echo 1
}

test_package() {
    echo
    code=$(check_command)
    [ $code -eq 0 ] && go version || echo "Error: $PKG_NAME is not installed." >&2
    exit $code
}

is_sudoer() {
    ## Define error code
    E_NOTROOT=87 # Non-root exit error.

    ## check if is sudoer
    if ! $(sudo -l &>/dev/null); then
        echo 'Error: root privileges are needed to run this script'
        return $E_NOTROOT
    fi
    return 0
}

if is_sudoer; then
    ## Remove previous package versions
    status="$(dpkg-query -W --showformat='${db:Status-Status}' ${PKG_NAME} 2>&1)"
    if [ $? = 0 ] || [ "${status}" = installed ]; then
        sudo apt remove --purge --auto-remove -y "${PKG_NAME}"
    fi

    pkg_path="${LOCAL_PATH}/go"
    pkg_lib_path="${LIB_PATH}/go"

    ## Download the latest version available.
    [ "$BUILD" -eq "0" ] && version_build="${VERSION}" || version_build="${VERSION}.${BUILD}"
    mkdir "${USER_HOME_DIR}/tmp"
    cd "${USER_HOME_DIR}/tmp"
    wget "${GO_URL}go${version_build}.linux-amd64.tar.gz"

    ## Extract and install the downloaded version
    if [ -f "go${version_build}.linux-amd64.tar.gz" ]; then
        sudo rm -rf "${pkg_path}" && sudo tar -xzvf "go${version_build}.linux-amd64.tar.gz" -C "${LOCAL_PATH}"

        ## Add symbolic link binary files to system binary and library directories
        if [ ! -h "${pkg_lib_path}" ]; then
            sudo ln -sv "${pkg_path}" "${pkg_lib_path}"
        fi

        binaries='go gofmt'
        for i in $binaries; do
            if [ ! -h "${BIN_PATH}/${i}" ]; then
                sudo ln -sv "${pkg_lib_path}/bin/${i}" "${BIN_PATH}/${i}"
            fi
        done

        ## Test your new version.
        test_package
    else
        echo "Error: tar file does not exits." >&2
        exit 1
    fi
else
    exit $?
fi
  1. Give running permission to the file:
sudo chmod -v u+x ~/go-installer.sh
  1. Run the script:
./go-installer.sh

It will return something like this:

...
go/test/used.go
go/test/utf.go
go/test/varerr.go
go/test/varinit.go
go/test/winbatch.go
go/test/writebarrier.go
go/test/zerodivide.go
'/usr/lib/go' -> '/usr/local/go'
'/usr/bin/go' -> '/usr/lib/go/bin/go'
'/usr/bin/gofmt' -> '/usr/lib/go/bin/gofmt'

go version go1.19.4 linux/amd64
teocci@ubuntu:~$
  1. Test your installation:
go version

Now Go will be global and can run from anywhere.

Teocci
  • 7,189
  • 1
  • 50
  • 48
0

sudo add-apt-repository ppa:longsleep/golang-backports sudo apt update sudo apt install golang-go

After above commands and go environment setup You tried to run a go program it will show you go: no such tool "compile" if you are facing a same then follow below link https://stackoverflow.com/a/76550511/22128545

-1
  1. If you have ubuntu-mate, you can install latest go by:

    umake go

  2. I have a script to download and install the last go from official website

     # Change these varialbe to where ever you feel comfortable
     DOWNLOAD_DIR=${HOME}/Downloads/GoLang
     INSTALL_DIR=${HOME}/App
     function install {
        mkdir -p ${DOWNLOAD_DIR}
        cd ${DOWNLOAD_DIR}
    
        echo "Fetching latest Go version..."
        typeset VER=`curl -s https://golang.org/dl/ | grep -m 1 -o 'go\([0-9]\)\+\(\.[0-9]\)\+'`
        if uname -m | grep 64 > /dev/null; then
            typeset ARCH=amd64
        else
            typeset ARCH=386
        fi
        typeset FILE=$VER.linux-$ARCH
    
        if [[ ! -e ${FILE}.tar.gz ]]; then
             echo "Downloading '$FILE' ..."
             wget https://storage.googleapis.com/golang/${FILE}.tar.gz
        fi
    
        echo "Installing ${FILE} ..."
        tar zxfC ${FILE}.tar.gz ${INSTALL_DIR}
        echo "Go is installed"
    }
    
    install
    

Setup your GOROOT, GOPATH and PATH:

export GOROOT=${INSTALL_DIR}/go
export GOPATH=<your go path>
export PATH=${PATH}:${GOROOT}/bin:${GOPATH}/bin
Enze Chi
  • 1,733
  • 17
  • 28
-1

You can use a script from udhos/update-golang.

Here is a two-liner as example (run as root):

bash <(curl -s https://raw.githubusercontent.com/udhos/update-golang/master/update-golang.sh)
ln -vs /usr/local/go/bin/go* /usr/local/bin/
kenorb
  • 155,785
  • 88
  • 678
  • 743
-1

Best way to install Go on Ubuntu is to download required version from here. Here you could have all stable and releases, along with archived versions.

after downloading you selected version you can follow further steps, i will suggest you to download tar.gz format for ubuntu machine:

  1. first of all fully remove the older version from your local by doing this

sudo rm -rf /usr/local/go /usr/local/gocache

this will remove all the local go code base but wait something more we have to do to remove fully from local, i was missing this step and it took so much time until I understood what i am missing so here is the purge stuff to remove from list

sudo apt-get purge golang

or

sudo apt remove golang-go
  1. Now install / extract your downloaded version of go inside /usr/local/go, by hitting terminal with this

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

  1. after doing all above stuff , don't forget or check to GOROOT variable value you can check the value by go env if not set then export PATH=$PATH:/usr/local/go
  2. Better to test a small go program to make sure. write this inside /home/yourusername/go/test.php if you haven't changed set GOPATH value:
package main

import "fmt"

func main() {
    fmt.Println("hello world")
}
  1. run this by go run test.go

i hope it works for you!!

Ashutosh Rai
  • 458
  • 1
  • 7
  • 14