go install shows permission denied

NobinPegasus

Member
Joined
Feb 4, 2022
Messages
50
Reaction score
0
Credits
447
Whenever I try to install any package in go. using go install it says permission denied. Then I have to do sudo go install.
Is it normal? Or do I have to change some permissions? How can I fix it?
Code:
pegasus@pegasus:~$ go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
google.golang.org/grpc/cmd/protoc-gen-go-grpc: go install google.golang.org/grpc/cmd/protoc-gen-go-grpc: copying /tmp/go-build2657225281/b001/exe/a.out: open /home/pegasus/go/bin/protoc-gen-go-grpc: permission denied
Code:
pegasus@pegasus:~$ sudo go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
[sudo] password for pegasus:
 


From CoPilot.

When you encounter a “permission denied” error while using go install, it can be frustrating. Let’s troubleshoot this issue and find a solution:

  1. Avoid Using sudo with go install:
    • Do not use sudo with go install. Once you use sudo, it changes the permissions of associated folders, which can lead to permission issues.
    • Instead of using sudo, try the following steps.
  2. Check Permissions:
    • First, ensure that the relevant directories have the correct permissions.
    • The error message you’re encountering (go install cmd/go: copying /var/folders/.../go-build.../exe/a.out: open /usr/local/go/bin/go: permission denied) suggests that either:
      • The <PATH_TO>/cmd directory is not writable.
      • The $GOPATH/pkg/cmd/go directory is not readable for your user.
    • You can check the permissions using the ls -lah <PATH_TO>/cmd command.
  3. Possible Solutions:
    • Here are some steps you can take to resolve the issue:
      • If <PATH_TO>/cmd is writable, use sudo temporarily to install, and then change the ownership back to your user using chown.
      • Consider creating the project inside your $GOPATH instead of outside it.
      • Verify that the GOBIN environment variable is correctly set. It should not need to be set for package-level go install as long as GOPATH is set.
      • If you’re using Go modules, ensure that your project structure adheres to the module guidelines.
      • If you’re using govendor or go mod, make sure the project is within your GOPATH.
  4. Root User Permissions:
Remember, it’s essential to maintain proper permissions to avoid security risks. Troubleshoot step by step, and you’ll likely find a solution without needing to rely on sudo.

In some Linux distro's, using the vendor supplied plugins are preferable to using the golang-github plugins because all permissions are not equal for all plugins as the vendor has no control over github.

It seems that if you've ever used sudo, even one time to install a golang plugin, your permissions are probably non-standard.
 
Last edited:
You can just download the go tarball for Linux and then extract in your users homedir in the bin directory and execute it.
Code:
bin]$ ./go version
go version go1.22.1 linux/amd64
 
From CoPilot.

When you encounter a “permission denied” error while using go install, it can be frustrating. Let’s troubleshoot this issue and find a solution:

  1. Avoid Using sudo with go install:
    • Do not use sudo with go install. Once you use sudo, it changes the permissions of associated folders, which can lead to permission issues.
    • Instead of using sudo, try the following steps.
  2. Check Permissions:
    • First, ensure that the relevant directories have the correct permissions.
    • The error message you’re encountering (go install cmd/go: copying /var/folders/.../go-build.../exe/a.out: open /usr/local/go/bin/go: permission denied) suggests that either:
      • The <PATH_TO>/cmd directory is not writable.
      • The $GOPATH/pkg/cmd/go directory is not readable for your user.
    • You can check the permissions using the ls -lah <PATH_TO>/cmd command.
  3. Possible Solutions:
    • Here are some steps you can take to resolve the issue:
      • If <PATH_TO>/cmd is writable, use sudo temporarily to install, and then change the ownership back to your user using chown.
      • Consider creating the project inside your $GOPATH instead of outside it.
      • Verify that the GOBIN environment variable is correctly set. It should not need to be set for package-level go install as long as GOPATH is set.
      • If you’re using Go modules, ensure that your project structure adheres to the module guidelines.
      • If you’re using govendor or go mod, make sure the project is within your GOPATH.
  4. Root User Permissions:
Remember, it’s essential to maintain proper permissions to avoid security risks. Troubleshoot step by step, and you’ll likely find a solution without needing to rely on sudo.

In some Linux distro's, using the vendor supplied plugins are preferable to using the golang-github plugins because all permissions are not equal for all plugins as the vendor has no control over github.

It seems that if you've ever used sudo, even one time to install a golang plugin, your permissions are probably non-standard.
I'm new to Go. How can I find cmd? what is the <PATH_TO> here?
How to set GOPATH?

Code:
pegasus@pegasus:~$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/pegasus/.cache/go-build"
GOENV="/home/pegasus/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/pegasus/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/pegasus/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go-1.20"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go-1.20/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.20.3"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build917789205=/tmp/go-build -gno-record-gcc-switches"

Code:
pegasus@pegasus:~$ go env GOPATH
/home/pegasus/go

Code:
pegasus@pegasus:~$ which go
/usr/local/bin/go
pegasus@pegasus:~$ whereis go
go: /usr/bin/go /usr/lib/go /usr/local/bin/go /usr/local/go /usr/share/go /home/pegasus/go/bin/go /usr/share/man/man1/go.1.gz


Hope these snippets help you understand the issue better and suggest a fix.
 
I'm new to Go. How can I find cmd? what is the <PATH_TO> here?
You can place the go binary anywhere in your user's PATH.
Code:
echo $PATH
And if your a path isn't defined there, you can add that path in your user's bashrc or bash_profile. For example you can add $HOME/bin to your $PATH and then place the go binaries there, that way it will be owned by your user and doesn't need to be owned by root.
 

Staff online


Top