topskip
topskip

Reputation: 17335

golang: go install tries /usr/local instead of GOPATH

This is somewhat a follow-up to my last question: golang: installing packages in a local directory

I have GOPATH set to $HOME/prog/go/gopath and this path exists with three directories:

~/prog/go/gopath$ ls
bin  pkg  src

Now I try to install a module to access the redis database which asks me to run

go install

inside the source directory. But the command go install gives me

~/prog/go/gopath/src/redis (go1)$ go install
go install flag: open /usr/local/go/pkg/darwin_amd64/flag.a: permission denied
~/prog/go/gopath/src/redis (go1)$ echo $GOPATH 
<myhomedir>/prog/go/gopath

(where <myhomedir> is a valid path)

Question 1: why does go install not take $GOPATH into account? Question 2: how to convince go install to use $GOPATH?

Upvotes: 15

Views: 13177

Answers (4)

scaganoff
scaganoff

Reputation: 1850

I think the command you need is "go get":

go get github.com/alphazero/Go-Redis

will download the Go-Redis library and put it into your $GOPATH/src directory.

go install performs a compile and install on your own source code.

I must admit, I struggled with this whole concept for a bit, but a careful re-reading of "How to Write Go" and the code organization section contains what you need to know about how the go command works.

Upvotes: 3

Jay
Jay

Reputation: 1077

Similar problems here. When I check my $GOROOT, I find that all the libraries are already built there. But for some reasons, it tries to rebuild all the libraries. So I just do a little trick:

find /usr/lib/go/pkg/ -name "*.*" | sudo xargs touch

Then everything just work fine.

Upvotes: 5

boyal
boyal

Reputation: 371

The solution is remove GOROOT from your .bash_profile. Then the go command will install it to your GOPATH directory. And so strange is: when I set the GOROOT in my .bash_profile again and create a new shell, it also works.

Upvotes: 0

dskinner
dskinner

Reputation: 10857

Not sure how you setup go but it's possible that it needs to build packages from std library but can't due to permissions. You can try

cd /usr/local/go/src
sudo ./all.bash

This should build the std library and run tests to make sure everything is ok.

Make sure you have proper permissions to read and execute from $GOROOT as necessary. Personally I just download the archive from golang.org and keep it under ~/local/go and set GOROOT appropriately.

Upvotes: 6

Related Questions