Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 262
topskip Avatar asked Sep 20 '12 18:09

topskip


People also ask

Is Gopath deprecated?

GoSIG / go-sig According to the golang wiki: Go 1.17 (August 2021) will remove the GO111MODULE setting and GOPATH mode entirely, using module mode always.

Do I need to set Gopath?

You do not need to change this variable, unless you plan to use different Go versions. GOPATH is a variable that defines the root of your workspace. By default, the workspace directory is a directory that is named go within your user home directory (~/go for Linux and MacOS, %USERPROFILE%/go for Windows).

Where is Go installed by default?

Open the MSI file you downloaded and follow the prompts to install Go. By default, the installer will install Go to Program Files or Program Files (x86) . You can change the location as needed.


2 Answers

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.

like image 75
dskinner Avatar answered Oct 04 '22 16:10

dskinner


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.

like image 22
Jay Avatar answered Oct 04 '22 15:10

Jay