Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/go: permission denied on macOS, with go install command when I set GOOS=linux #20571

Closed
gszecsenyi opened this issue Jun 4, 2017 · 6 comments

Comments

@gszecsenyi
Copy link

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go1.8.3

What operating system and processor architecture are you using (go env)?

darwin/amd64

What did you do?

export GOOS=linux
go install

The error message is:

go install runtime/internal/sys: mkdir /usr/local/go/pkg/linux_amd64: permission denied

I also have this issue. On OSX, go1.8.3 darwin/amd64. If I set GOOS=linux, then it fails, because it want to create the pkg directory on GOROOT and not on GOPATH.
This is the error message:
go install runtime/internal/sys: mkdir /usr/local/go/pkg/linux_amd64: permission denied

GOPATH is "/Users/xxx/Documents/GitHub/developments/go/"

@gszecsenyi gszecsenyi changed the title Permission denied on osx, on go install command when I set GOOS=linux Permission denied on osx, with "go install" command when I set GOOS=linux Jun 4, 2017
@gszecsenyi
Copy link
Author

If I set back GOOS to darwin with export GOOS=darwin command, then it works again, it uses the GOPATH variable as deployment destination, again.

@davecheney
Copy link
Contributor

davecheney commented Jun 4, 2017 via email

@matthewcummings
Copy link

I'm seeing this same issue but with exporting GOOS=windows on Linux and running go get, which in turn calls go install. I have GOPATH set to a local directory but go install appears to be ignoring it. My workaround is to just run go get before changing GOOS in my build scripts.

Probably obvious but my CI job is building Windows and Linux binaries on a Linux build machine.

@davecheney
Copy link
Contributor

@matthewcummings can you please open a new issue including all the required information in the template. Please also include the output of adding the -x flag to go get/install which will show us the commands the Go tool executed.

If this turns out to be a duplicate then we’ll merge the issues. Thanks.

@agnivade
Copy link
Contributor

@gszecsenyi - Please use go build instead of go install when cross-compiling. The reasons are clearly laid out in Dave's blog post. I suggest closing this issue.

@andybons andybons changed the title Permission denied on osx, with "go install" command when I set GOOS=linux cmd/go: permission denied on macOS, with go install command when I set GOOS=linux Mar 26, 2018
@andybons
Copy link
Member

Using go build vs go install
When cross compiling, you should use go build, not go install. This is the one of the few cases where go build is preferable to go install.

The reason for this is go install always caches compiled packages, .a files, into the pkg/ directory that matches the root of the source code.

For example, if you are building $GOPATH/src/github.com/lib/pq, then the compiled package will be installed into $GOPATH/pkg/$GOOS_$GOARCH/github.com/lib/pq.a.

This logic also holds true for the standard library, which lives in /usr/local/go/src, so will be compiled to /usr/local/go/pkg/$GOOS_$GOARCH. This is a problem, because when cross compiling the go tool needs to rebuild the standard library for your target, but the binary distribution expects that /usr/local/go is not writeable.

Using go build rather that go install is the solution here, because go build builds, then throws away most of the result (rather than caching it for later), leaving you with the final binary in the current directory, which is most likely writeable by you.

Closing for now given this (very good) explanation from @davecheney’s post.

@golang golang locked and limited conversation to collaborators Mar 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants