How to properly install Go Language in Ubuntu?

The problem is your GOROOT. It should be set to /usr/lib/go (not /usr/share/go).

export GOROOT=/usr/lib/go

Installation

  1. Update.

    sudo apt-get update 
    
  2. Install Go language.

    sudo apt-get install golang
    
  3. Set GOPATH to your home folder. After this action, you can use command echo $GOPATH to check the go path location.

    export GOPATH=$HOME/gopath
    
  4. For the GOPATH, it is not required to define it. But you can still define it.

    # Get the go installation root path.
    go env GOROOT
    
    # Set the result of `go env GOROOT` to system variable GOROOT.
    # In this example, the result of `go env GOROOT` is `/usr/lib/go-1.6`
    export GOROOT=/usr/lib/go-1.6
    

Checking Go Environment Variables

  1. Use command go env and you will get:

    GOARCH="amd64"
    GOBIN=""
    GOEXE=""
    GOHOSTARCH="amd64"
    GOHOSTOS="linux"
    GOOS="linux"
    GOPATH="/home/casper/gopath"
    GORACE=""
    GOROOT="/usr/lib/go-1.6"
    GOTOOLDIR="/usr/lib/go-1.6/pkg/tool/linux_amd64"
    GO15VENDOREXPERIMENT="1"
    CC="gcc"
    GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
    CXX="g++"
    CGO_ENABLED="1"
    
  2. Now you can see that:

    a. GOPATH is in /home/casper/gopath.

    b. GOROOT is in /usr/lib/go-1.6.

References

  • Install package golang
    https://github.com/golang/go/wiki/Ubuntu

  • You don’t need to set GOROOT, really
    http://dave.cheney.net/2013/06/14/you-dont-need-to-set-goroot-really

  • Getting Started - The Go Programming Language
    https://golang.org/doc/install