Alexander Bauer
Alexander Bauer

Reputation: 11083

go install always attempts to use GOROOT and GOPATH is not listed under go env

I'm having a quite frustrating problem with the GOPATH, which, despite being set in .profile, is not appearing when invoking go env, and does not appear to be affecting the go install target location.

I'm attempting to use go install to install packages, and am getting this error, which clearly shows that it is attempting to install in /usr/lib/go, rather than the intended directory of /home/me/dev/go.

$ go install github.com/songgao/colorgo
go install github.com/songgao/go.pipeline: mkdir /usr/lib/go/pkg/linux_386/github.com: permission denied

go env gives the following results.

$ go env
GOROOT="/usr/lib/go"
GOBIN=""
GOARCH="386"
GOCHAR="8"
GOOS="linux"
GOEXE=""
GOHOSTARCH="386"
GOHOSTOS="linux"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_386"
GOGCCFLAGS="-g -O2 -fPIC -m32 -pthread"
CGO_ENABLED="1"

But it most certainly is set.

$ echo $GOPATH
/home/me/dev/go

Update: I have exported GOPATH in ~/.profile and sourced it, but I'm still having the same problem.

$ export GOPATH=/home/me/dev/go
$ go env
GOROOT="/usr/lib/go"
GOBIN=""
GOARCH="386"
GOCHAR="8"
GOOS="linux"
GOEXE=""
GOHOSTARCH="386"
GOHOSTOS="linux"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_386"
GOGCCFLAGS="-g -O2 -fPIC -m32 -pthread"
CGO_ENABLED="1"

Update again: This problem has ceased since I upgraded to go1.1beta2. I'm not sure what was wrong, because GOPATH appeared to be set correctly, but it's all apparently working now.

Upvotes: 10

Views: 21931

Answers (4)

GKV
GKV

Reputation: 495

Create a soft link, then copy the go binary in /usr/local as follow:

# ln -sf /usr/local/go/bin/go /usr/bin/go

Upvotes: 0

Shane Hou
Shane Hou

Reputation: 5018

I think it's quite clear that go install everything in your GOTOOLDIR, GOTOOLDIR is also related with GOROOT:

Yours: GOROOT="/usr/lib/go"

According to Document:

Install the Go tools

The Go binary distributions assume they will be installed in /usr/local/go (or c:\Go under Windows), but it is possible to install them in a different location. If you do this, you will need to set the GOROOT environment variable to that directory when using the Go tools.

For example, if you installed Go to your home directory you should add the following commands to $HOME/.profile:

export GOROOT=$HOME/go

export PATH=$PATH:$GOROOT/bin

Windows users should read the section about setting environment variables under Windows.

You should follow these steps to change your GOROOT, instead of GOPATH, then everything should work.

If it's not work, change GOTOOLDIR as well.

Upvotes: 0

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38745

As you gave evidence for having set GOPATH, the second part of your question - GOPATH is not listed under go env - may be caused by the "go env" of your installed version. Neither go1 (linux) nor go1.0.2 (windows) versions of "go env" display GOPATH. If you look at the source (GOROOT/src/cmd/go/env.go), you'll find right at the top the list of elements to display (mkEnv) which starts with GOROOT and does not contain GOPATH.

Version go1.0.3 (windows) does display GOPATH; the corresponding list in the source, however, starts with GOARCH.

I admit that this does not solve the first part of your problem - (trying to) install into GOROOT - but perhaps the above will help you to focus on the culprit (build process).

Upvotes: 0

peterSO
peterSO

Reputation: 166570

GOPATH and workspaces

$ mkdir -p $HOME/dev/go/src
$ mkdir -p $HOME/dev/go/bin

In $HOME/.profile:

export GOPATH=$HOME/dev/go:
export PATH=$PATH:$HOME/dev/go/bin

Then reboot or log out and log in.

As soon as you have logged in, before anything else, run:

$ env | grep -i '^GO'
GOPATH=/home/me/dev/go
$ cat $HOME/.profile

What output do you get?

Upvotes: 3

Related Questions