'Permission denied when installing go mockgen

I am trying to install the mockgen package on go, but I keep receiving a permission denied without knowing why:

go install github.com/golang/mock/[email protected]

Output:

go install github.com/golang/mock/mockgen: copying /tmp/go-build3889221292/b001/exe/a.out: open /home/nocnoc/go/bin/mockgen: permission denied

I have tried using sudo or executing it as root, but I receive this error

sudo: go: command not found

My GOROOT is /usr/local/go and GOPATH is in my home directory. GO1111MODULE is also on.

How can I fix it? Everything else seems to run fine, and only mockgen seems to fail to install.

go


Solution 1:[1]

The issue was the permissions and owners of directories ~/go and ~/go/bin. It was solved by using the chown command to change the owner of those two directories from root to my user:

drwxr-xr-x 10 root   root       4096 Aug 11  2020  go

to

drwxrwxr-x 10 nocnoc nocnoc     4096 Aug 11  2020  go

The permissions and owners of ~/go/bin are the same as those for the directory above.

Solution 2:[2]

I'm not a Linux expert, but try running:

sudo su
go install github.com/golang/mock/[email protected]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peter Mortensen
Solution 2 Peter Mortensen