无法安装Go包。

huangapple go评论82阅读模式
英文:

Unable to install Go packages

问题

当我运行go get时,我遇到了权限被拒绝的错误,当我尝试sudo go get时,我遇到了GOPATH未设置的错误。

以下是我的$PATH、go env和which go命令的结果。

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

我该如何解决这个问题?

我正在使用来自此PPA的go1.5 https://launchpad.net/~ubuntu-lxc/+archive/ubuntu/lxd-stable (ppa:ubuntu-lxc/lxd-stable)。

英文:

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)

答案1

得分: 16

godoc似乎是go get常规做法的一个例外,因为它安装到了go安装目录($GOROOT/bin)而不是$GOPATH。所以,如果你真的需要更新godoc(为什么?),你需要做以下操作:

  1. 以root用户登录(或者使用susudo su等命令)
  2. $GOPATH设置为你普通用户的$GOPATH(例如"/home/utkbansal/go"
  3. 使用go get -u golang.org/x/tools/cmd/godoc或者go get所有工具来更新godoc
  4. 为你的$GOPATH设置适当的权限,即使用chown -R utkbansal:utkbansal $GOPATH(仍然以root用户身份)

我想这样应该可以工作。但是:为什么你想要更新godoc呢?如果你只是想要安装一个未预装的特定工具,你应该可以在没有root权限的情况下使用go get来安装它。

英文:

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.

答案2

得分: 9

mrd0ll4r对这个问题有一个很好的解释,不过我想分享一种更简单的安装godoc的方法。假设你在.bashrc(或类似的文件)中设置了$GOPATH,可以尝试以下命令:

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

使用-E标志可以保留当前的环境变量(包括$GOPATH)。

英文:

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).

答案3

得分: 6

请参考@mrd0ll4r的答案,那个更好。(https://stackoverflow.com/a/33755566/989659)

更新

由于你使用了-u标志,它首先尝试更新已安装的软件包,并在/usr/lib/go/bin/godoc中有一个二进制文件。

当你用sudo运行它时,它没有你的环境变量,所以它不再有GOPATH变量。

你可以进入root模式并从那里运行它

sudo su 
export GOROOT="/usr/lib/go"
export GOPATH="/home/utkbansal/go"
go get -u golang.org/x/tools/cmd/...
# 其他命令
exit
英文:

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

答案4

得分: 1

如果您没有 root 权限,我们可以将二进制文件构建到 $GOPATH/bin 目录中,让我以 godoc 为例进行说明:

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

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

答案5

得分: 1

你的电脑出现了相同的问题。问题是,我安装了Go语言,但没有安装godoc。最简单的解决方法是安装标准的godoc(在我的Fedora上使用dnf安装)。

sudo dnf install golang-godoc

感谢mrd0ll4r解释了问题所在。

英文:

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

答案6

得分: 0

我尝试了这个帖子中的所有答案,但无法安装godoc。

sudo apt install golang-golang-x-tools

我相信我现在安装的godoc版本比我的Go版本要旧,但我会接受这个。

英文:

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.

答案7

得分: 0

我在安装Delve时遇到了同样的问题:

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

但是我通过另一种方式解决了这个问题,与 @mrd0ll4r 不同,你不需要做任何更改。

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

这个方法很有效。

参考 使用sudo时出现命令未找到

英文:

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

答案8

得分: -1

以下是要翻译的内容:

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

sudo - 以 root 用户身份运行后续命令。

GOPATH=path_to_go_installation - 设置 sudo 会话的环境变量。

go get - 将包安装到之前设置的 GOPATH 中。

英文:

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

sudo - run followed commands as root.<br/>

GOPATH=path_to_go_installation - sets environment variable for sudo session.<br/>

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

huangapple
  • 本文由 发表于 2015年11月17日 16:29:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/33752247.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定