utkbansal
utkbansal

Reputation: 2817

Unable to install Go packages

When I run go get, I get a permission Denied error and when I try sudo go get I get a GOPATH not set error.

utkbansal@Dell:~$ go  get -u golang.org/x/tools/cmd/...
go install golang.org/x/tools/cmd/godoc: open /usr/lib/go/bin/godoc: permission denied

utkbansal@Dell:~$ sudo go  get -u golang.org/x/tools/cmd/...
package golang.org/x/tools/cmd/...: cannot download, $GOPATH not set. For more details see: go help gopath

Here are the result of my $PATH, go env and which go commands.

utkbansal@Dell:~$ which go
/usr/lib/go/bin/go

utkbansal@Dell:~$ go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/utkbansal/go"
GORACE=""
GOROOT="/usr/lib/go"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GO15VENDOREXPERIMENT=""
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"


utkbansal@Dell:~$ $PATH
bash: /usr/lib/go/bin:/home/utkbansal/miniconda/bin:/usr/local/heroku/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/local/go/bin: No such file or directory

How do I fix this?

I am using go1.5 from this PPA https://launchpad.net/~ubuntu-lxc/+archive/ubuntu/lxd-stable (ppa:ubuntu-lxc/lxd-stable)

Upvotes: 12

Views: 43370

Answers (8)

Fred
Fred

Reputation: 101

I have the same problem when installing delve

go get github.com/derekparker/delve/cmd/dlv: open /usr/local/go/bin/dlv: permission denied

But I solved it by using another way different form @mrd0ll4r, you needn't change anything.

sudo env "PATH=$PATH" go get -u github.com/derekparker/delve/cmd/dlv

It works well.

refer to command not found when using sudo

Upvotes: 0

kpg
kpg

Reputation: 7966

I tried all the answers in this thread and couldn't get godoc installed.

sudo apt install golang-golang-x-tools

I believe I now have a version of godocs which is older than my Go version but I will live with that.

Upvotes: 0

lofcek
lofcek

Reputation: 1233

Same problem appear in my computer. Problem was, that I had install go, but not godoc. And the simples was install standart godoc (on my fedora dnf install)

sudo dnf install golang-godoc

Thank mrd0ll4r for explanation what was wrong

Upvotes: 1

Daniel YC Lin
Daniel YC Lin

Reputation: 16042

If you haven't the root permission, we can build binary into $GOPATH/bin, Let me use godoc as example

go get -u golang.org/x/tools/cmd/godoc
cd $GOPATH/src/golang.org/x/tools/cmd/godoc
go build -o $GOPATH/bin/godoc

Upvotes: 1

Bart Louwers
Bart Louwers

Reputation: 942

mrd0ll4r has an excellent explanation of the problem, though I would like to share an easier way to install godoc. Assuming you set your $GOPATH in your .bashrc (or similar) try:

sudo -E go get golang.org/x/tools/cmd/godoc

With the -E flag you perserve your current environment variables (including $GOPATH).

Upvotes: 10

Srgrn
Srgrn

Reputation: 1825

See @mrd0ll4r answer which is better. ( https://stackoverflow.com/a/33755566/989659 )

UPDATE

since you used the -u flag it first tries to update the package which is already installed and have a binary at /usr/lib/go/bin/godoc

when you ran it with sudo it doesn't have your enviroment variables so it no longer has the GOPATH variable

you can enter root mode and run it from there

sudo su 
export GOROOT="/usr/lib/go"
export GOPATH="/home/utkbansal/go"
go get -u golang.org/x/tools/cmd/...
# other commands
exit

Upvotes: 6

Alex Kroll
Alex Kroll

Reputation: 481

`sudo GOPATH=path_to_go_installation go get -u golang.org/x/tools/cmd/...

sudo - run followed commands as root.

GOPATH=path_to_go_installation - sets environment variable for sudo session.

go get - installs package into GOPATH that was set on the previous step.

Upvotes: -1

mrd0ll4r
mrd0ll4r

Reputation: 886

godoc seems to be an exception to the general go get practice, because it installs to the go installation ($GOROOT/bin) instead of $GOPATH. So, if you really need to update godoc (why?), what you need to do is:

  1. Log in as root (or su, or sudo su, or ...)
  2. Set $GOPATH to your normal user $GOPATH ("/home/utkbansal/go")
  3. Update godoc, using go get -u golang.org/x/tools/cmd/godoc, or all tools
  4. Set the appropriate permissions on for your $GOPATH, i.e. chown -R utkbansal:utkbansal $GOPATH (still as root)

That should work I guess. BUT: Why would you want to update godoc? If you just want one specific tool that is not pre-installed, you should be able to go get it without root privileges.

Upvotes: 16

Related Questions