Unable to install Go packages - 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)

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:
Log in as root (or su, or sudo su, or ...)
Set $GOPATH to your normal user $GOPATH ("/home/utkbansal/go")
Update godoc, using go get -u golang.org/x/tools/cmd/godoc, or all tools
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.

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

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

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

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

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.

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

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

Related

not solve the error ::protoc-gen-go-grpc: program not found or is not executable

protoc-gen-go-grpc: program not found or is not executable
Please specify a program using absolute path or make sure the program is available in your PATH system variable
--go-grpc_out: protoc-gen-go-grpc: Plugin failed with status code 1.
not solve the problem for :
ubuntu so please give me the answers
go get -u github.com/golang/protobuf/{proto,protoc-gen-go}
and
sudo apt install golang-goprotobuf-dev
and
sudo apt-get protobuf-compiler
You should be able to:
go install github.com/golang/protobuf/protoc-gen-go#latest
Then:
which protoc-gen-go
/${GOPATH}/bin/protoc-gen-go
If you haven't already, execute the following:
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc#latest
This will install protoc-gen-go-grpc executable to the location indicated by $GOBIN (default if not set is $GOPATH/bin)
$ ls $(go env GOPATH)/bin | grep 'protoc-gen-grpc-gateway'
protoc-gen-grpc-gateway
The protoc compiler is looking to run this executable, so you'll need to make sure it is resolvable via your $PATH environment variable.

Unable to install Go packages in Macbook with M1 chip

When I ran the command "go install github.com/go-delve/delve/cmd/dlv" in the delve folder, after downloading go-delve in my go path, the terminal showed the following result:
"go install github.com/go-delve/delve/cmd/dlv: mkdir /Desktop: read-only file system"
Terminal result put here
When I googled the same, I came across a website(https://www.dev2qa.com/how-to-fix-read-only-file-system-error-when-run-mkdir-command-on-macos/) which requires me to disable System Integrity Protection on my MacBook and it asks me to hold the option key or command + R immediately after Mac chimes in reboot after starting the MacBook, until I see a boot in a "console/terminal" mode.. But nothing happened when I tried doing that.
I am not knowing what to do. Please guide me through the right way to install dlv and other packages of go successfully on my Macbook.
make sure you use the right go version (1.16 or later, arm64)
$ go version
go version go1.17.1 darwin/arm64
make sure the env is right by this command
$ go env
check GOROOT GOPATH GOARCH GOBIN
try to install it directly
$ go install github.com/go-delve/delve/cmd/dlv#latest
your error occurs because you have no permission of /Desktop, and i'm sure install dlv don't need something like this, it will install to ~/go/bin

How to uninstall Go?

I tried the answer here Removed golang but go command still works?, but it didn't work (I can still run go)
Currently, when I run which go I see this output
/usr/local/go/bin/go
I think I had two installations of go as my GOPATH was pointing to another folder named gocode. I've now removed that folder, and the usr/local/go/bin/go folder.
I've also removed my GOPATH. However, I can still run go.
How do I uninstall go?
Update August 2019
Found the official uninstall docs worked as expected (on Mac OSX).
$ which go
/usr/local/go/bin/go
In summary, to uninstall:
$ sudo rm -rf /usr/local/go
$ sudo rm /etc/paths.d/go
Then, did a fresh install with homebrew using brew install go. Now, i have:
$ which go
/usr/local/bin/go
You might try
rm -rvf /usr/local/go/
then remove any mention of go in e.g. your ~/.bashrc; then you need at least to logout and login.
However, be careful when doing that. You might break your system badly if something is wrong.
PS. I am assuming a Linux or POSIX system.
I'm using Ubuntu. I spent a whole morning fixing this, tried all different solutions, when I type go version, it's still there, really annoying...
Finally this worked for me, hope this will help!
sudo apt-get remove golang-go
sudo apt-get remove --auto-remove golang-go
On a Mac-OS system
If you have used an installer, you can uninstall golang by using the same installer.
If you have installed from source
rm -rf /usr/local/go
rm -rf $(echo $GOPATH)
Then, remove all entries related to go i.e. GOROOT, GOPATH from ~/.bash_profile and run
source ~/.bash_profile
On a Linux system
rm -rf /usr/local/go
rm -rf $(echo $GOPATH)
Then, remove all entries related to go i.e. GOROOT, GOPATH from ~/.bashrc and run
source ~/.bashrc
For Windows 10:
Go to Apps in the Settings App.
Look for Go Programming Language * in the list and uninstall it.
Remove C:\Go\bin from your PATH environment variable (only if you don't plan on installing another version of golang)
Update (Sep, 2022)
The official page has changed path for the uninstallation help along with the help text. Here is what it says now.
Uninstalling Go
You can remove Go from your system using the steps
described in this topic.
Linux / macOS / FreeBSD
Delete the go directory.
This is usually
/usr/local/go.
Remove the Go bin directory from your PATH environment variable.
Under Linux and FreeBSD, edit /etc/profile or $HOME/.profile. If you
installed Go with the macOS package, remove the /etc/paths.d/go file.
Windows
The simplest way to remove Go is via Add/Remove Programs in
the Windows control panel:
In Control Panel, double-click Add/Remove Programs.
In Add/Remove
Programs, select Go Programming Language, click Uninstall, then follow
the prompts.
For removing Go with tools, you can also use the command
line:
Uninstall using the command line by running the following command:
msiexec /x go{{version}}.windows-{{cpu-arch}}.msi /q
Note: Using this
uninstall process for Windows will automatically remove Windows
environment variables created by the original installation.
Original Answer
From the official install page -
To remove an existing Go installation from your system delete the go
directory. This is usually /usr/local/go under Linux, macOS, and
FreeBSD or c:\Go under Windows.
You should also remove the Go bin directory from your PATH environment
variable. Under Linux and FreeBSD you should edit /etc/profile or
$HOME/.profile. If you installed Go with the macOS package then you
should remove the /etc/paths.d/go file. Windows users should read the
section about setting environment variables under Windows.
Use this command to uninstall Golang for Ubuntu.
This will remove just the golang-go package itself.
sudo apt-get remove golang-go
Uninstall golang-go and its dependencies:
sudo apt-get remove --auto-remove golang-go
To uninstall go on MacOS, do this:
On the terminal type which go it will; return a path like this /usr/local/go/bin/go
Go to the root folder of go which is /usr/local/go/ and type on the terminal rm -rf /usr/local/go/ . you may get permission denied depending on your system setup, so the command should be prefixed with sudo like this
sudo rm -rf /usr/local/go/
It will request for your password, just enter it.
sudo apt-get remove golang-go
sudo apt-get remove --auto-remove golang-go
This is perfect for Ubuntu 18.18
On a Mac-OS Catalina
need to add sudo before rm -rf /usr/local/go sudo rm -rf /usr/local/go
otherwise, we will run into permission denial.
sudo vim ~/.profile or sudo ~/.bash_profile remove export PATH=$PATH:$GOPATH/BIN or anything related to
go lang
If you use Zsh shell, then you need to remove the above line to ~/.zshrc file.
Hope it helps you :)
I just have to answer here after reading such super-basic advice in the other answers.
For MacOS the default paths are:
/user/bracicot/go (working dir)
/usr/local/go (install dir)
When uninstalling remove both directories.
If you've installed manually obviously these directories may be in other places.
One script I came across installed to /usr/local/.go/ a hidden folder because of permissioning... this could trip you up.
In terminal check:
echo $GOPATH
echo $GOROOT
#and
go version
For me after deleting all go folders I was still getting a go version.
Digging through my system path echo $PATH
/Users/bracicot/google-cloud-sdk/bin:/usr/local/bin:
revealed some places to check for still-existing go files such as /usr/local/bin
Another user mentioned: /etc/paths.d/go
You may also want to remove GOPATH and GOROOT environment variables.
Check .zshsrc and or .bash_profile.
Or you can unset GOPATH and unset GOROOT
In MacOS, you can just do it with brew:
brew uninstall go
brew install go
brew upgrade go
On linux we can do like this to remove go completely:
rm -rf "/usr/local/.go/"
rm -rf "/usr/local/go/"
These two command remove go and hidden .go files. Now we also have to update entries in shell profile.
Open your basic file. Mostly I open like this sudo gedit ~/.bashrc and remove all go mentions.
You can also do by sed command in ubuntu
sed -i '/# GoLang/d' .bashrc
sed -i '/export GOROOT/d' .bashrc
sed -i '/:$GOROOT/d' .bashrc
sed -i '/export GOPATH/d' .bashrc
sed -i '/:$GOPATH/d' .bashrc
It will remove Golang from everywhere. Also run this after running these command
source ~/.bash_profile
Tested on linux 18.04 also. That's All.
only tab
rm -rvf /usr/local/go/
not works well, but
sudo rm -rvf /usr/local/go/
do.
Go to the directory
cd /usr/local
Remove it with super user privileges
sudo rm -rf go

Permission denied error for 'go.tools'

When I try to install go tools I am getting permission denied for /usr/local/go/pkg/tool/linux_amd64/cover. I can accept that since it is /usr/local/ directory and need root access.
But my first doubt is why it is trying to install at this location when I set GOPATH to some other location.
$ go version
go version go1.2.1 linux/amd64
$ export GOPATH='/home/vagrant/repos/atlantis-router/vendor'
$ go get code.google.com/p/go.tools/cmd/cover
go install code.google.com/p/go.tools/cmd/cover: open /usr/local/go/pkg/tool/linux_amd64/cover: permission denied
$ go env
GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/vagrant/repos/atlantis-router/vendor"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
TERM="dumb"
CC="gcc"
GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread"
CXX="g++"
CGO_ENABLED="1"
Could anybody guide me solving this issue? What could be the reason?
Go tries to install that tool in $GOTOOLDIR, not $GOPATH, and as you can see it is set to /usr/local/go/pkg/tool/linux_amd64. Same thing happens to me here.
just run it with sudo, I guess, since there are other tools in that directory, and I think setting GOTOOLDIR to somewhere else might screw things up.
seems like this is a common issue happens for so many users . so happened also to me !
the first step is to set your environment variables for Go by these commands :
echo export GOROOT=/usr/local/go >> ~/.bashrc
echo export GOPATH=\$HOME/go >> ~/.bashrc
echo export PATH=\$PATH:\$GOROOT/bin:\$GOPATH/bin >> ~/.bashrc
and then go has the accesses
after that run this command so the system considers you as administrator:
sudo -s
then ctl+Shft+p and select Go: Instal/Upgrade tools
these steps worked for me :)

`go get` fails with permission denied on certain go packages

Installing some go packages fails with permission denied error, eg.:
$ go get code.google.com/p/go.tools/cmd/cover
go install code.google.com/p/go.tools/cmd/cover: open /usr/local/go/pkg/tool/darwin_amd64/cover: permission denied
When I tried using sudo to fix the permission issue, it failed with $GOPATH not set error:
$ sudo go get code.google.com/p/go.tools/cmd/cover
Password:
package code.google.com/p/go.tools/cmd/cover: cannot download, $GOPATH not set. For more details see: go help gopath
How can I solve this?
The godoc, go tool vet, go tool cover etc. are special go.tools commands that are meant to be installed into the system path by default with the go binary. If these commands are not available, you should try reinstall go itself (or find go.tools in your packaging system).
Note: On OS X 10.8+, try installing go using Homebrew instead of the official .pkg installer and your troubles should be gone (as of go 1.4): brew install go
If you want to download a specific pkg into your $GOPATH (eg. 3rd party dependency), use go get -d <pkg> instead. Example:
go get -d code.google.com/p/go.tools/cmd/cover
You shouldn't need to use the sudo hammer at all, as your $GOPATH should point to a directory that you own, and thus no permission: denied error at all.
But if you really know what you're doing, and you still want to sudo install something, you need to edit the sudoers file first to fix the root's GOPATH:
$ sudo visudo
add the following line:
Defaults env_keep += "GOPATH"
This will make the sudo go get (root context) pick up your $GOPATH value.
I just ran into this because I installed go with MacPorts. Vojtech Vitek's answer pointed me in the right direction but I thought I would go ahead and post the literal solution I needed.
Run sudo port install go-tools. I don't know why godoc et al are not included with the base go package, but whatever.

Resources