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

11,341

Solution 1

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.

Solution 2

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.

Solution 3

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.

Share:
11,341
topskip
Author by

topskip

I like to write software, for example for database publishing https://github.com/speedata/publisher/. Sometimes I work on my book about Go (in German).

Updated on June 07, 2022

Comments

  • topskip
    topskip about 2 years

    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?

  • topskip
    topskip almost 12 years
    I am not sure I understand what it does. My go installation works fine so far. Never had any problems and even cross compiling works fine.
  • dskinner
    dskinner almost 12 years
    do you have permission to read /usr/local/go/pkg/darwin_amd64/flag.a ?
  • topskip
    topskip almost 12 years
    yes, I can read /usr/local/go/pkg/darwin_amd64/flag.a as my main user
  • topskip
    topskip over 11 years
    ... btw: the go get should be without https:// from what I have tried. Thanks for the link to "How to Write Go"!
  • weberc2
    weberc2 over 11 years
    Isn't GOPATH a list of paths (analogous to PATH)? That's what the documentation says, if I recall correctly. Are you sure you don't mean GOROOT?
  • scaganoff
    scaganoff over 11 years
    GOROOT points to where the Go binary is installed. GOPATH is a list of Workspaces. There is more detail at golang.org/doc/code.html
  • gravitation
    gravitation almost 11 years
    I had the same issue and running ./all.bash as root fixed it. Not sure what the deal was: my permissions on the .a's were correct and it seemed to be trying to rebuild packages that were already built. Might have something to do with timestamps.
  • Jonathan Oliver
    Jonathan Oliver over 9 years
    Because of timestamp differences between the files in $GOROOT/src and $GOROOT/pkg, the pkg files are determined to be out of date and are attempting to be rebuilt. But because they're not writable by the user, there's a permission denied error. Touching each .a file fixes the problem.
  • Jonathan Oliver
    Jonathan Oliver over 9 years
    The command I ran was slightly different: find /usr/local/go -name "*.a" -exec touch -r `which go` {} \;