Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`go get` fails with permission denied on certain go packages

Tags:

macos

sudo

go

Installing some go packages fails with permission denied error, eg.:

$ go get code.google.com/p/go.tools/cmd/cover
go install code.google.com/p/go.tools/cmd/cover: open /usr/local/go/pkg/tool/darwin_amd64/cover: permission denied

When I tried using sudo to fix the permission issue, it failed with $GOPATH not set error:

$ sudo go get code.google.com/p/go.tools/cmd/cover
Password:
package code.google.com/p/go.tools/cmd/cover: cannot download, $GOPATH not set. For more details see: go help gopath

How can I solve this?

like image 740
Vojtech Vitek Avatar asked Nov 06 '14 06:11

Vojtech Vitek


1 Answers

  1. The godoc, go tool vet, go tool cover etc. are special go.tools commands that are meant to be installed into the system path by default with the go binary. If these commands are not available, you should try reinstall go itself (or find go.tools in your packaging system).

    Note: On OS X 10.8+, try installing go using Homebrew instead of the official .pkg installer and your troubles should be gone (as of go 1.4): brew install go

  2. If you want to download a specific pkg into your $GOPATH (eg. 3rd party dependency), use go get -d <pkg> instead. Example:

    go get -d code.google.com/p/go.tools/cmd/cover
    
  3. You shouldn't need to use the sudo hammer at all, as your $GOPATH should point to a directory that you own, and thus no permission: denied error at all.

    But if you really know what you're doing, and you still want to sudo install something, you need to edit the sudoers file first to fix the root's GOPATH:

    $ sudo visudo
    

    add the following line:

    Defaults    env_keep += "GOPATH"
    

    This will make the sudo go get (root context) pick up your $GOPATH value.

like image 74
Vojtech Vitek Avatar answered Sep 24 '22 17:09

Vojtech Vitek