douju2012
douju2012
2015-11-17 08:29

无法安装Go软件包

已采纳

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)

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

8条回答

  • dqaxw44567 dqaxw44567 6年前

    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.

    点赞 评论 复制链接分享
  • dpde7365 dpde7365 4年前

    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.

    点赞 评论 复制链接分享
  • douyi1963 douyi1963 3年前

    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

    点赞 评论 复制链接分享
  • duanliao6077 duanliao6077 5年前

    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

    点赞 评论 复制链接分享
  • douchen7555 douchen7555 6年前

    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
    
    点赞 评论 复制链接分享
  • douben7493 douben7493 6年前

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

    点赞 评论 复制链接分享
  • duanfang5849 duanfang5849 5年前

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

    点赞 评论 复制链接分享
  • douhuibo5635 douhuibo5635 5年前

    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
    
    点赞 评论 复制链接分享