Permission denied error for 'go.tools' - go

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

Related

Can't install golangci-lint locally

I'm using RHEL 8.6 and my Go version is the following:
$ go version
go version go1.18.3 linux/amd64
I'm trying to install locally golangci-lint and none of the described ways in the documentation are working.
What I tried:
First:
$ curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.46.2
golangci/golangci-lint info checking GitHub for tag 'v1.46.2'
golangci/golangci-lint info found version: 1.46.2 for v1.46.2/linux/amd64
golangci/golangci-lint info installed /home/acabista/go/bin/golangci-lint
$ golangci-lint --version
bash: golangci-lint: command not found...
Second:
$ go install github.com/golangci/golangci-lint/cmd/golangci-lint#v1.46.2
$ golangci-lint --version
bash: golangci-lint: command not found...
Am I missing a step? How can I make this local installation work?
If golang-ci has properly been installed, the issue is most likely that the installation directory is not in your PATH environment variable. Calling golang-ci this way should then work:
${GOPATH}/bin/golangci-lint --version
or
/home/acabista/go/bin/golangci-lint --version
To chek what is happening exactly you can check the content of the GOPATH environment variable. Its content defines where binaries are installed when a go install like command is run.
echo $GOPATH
You need to check also what is the content of the PATH variable, this one defines in which directory the shell looks for binary to execute:
echo $PATH

Golang installation

I just followed the installation guide for golang (ubuntu 16).
I extracted the archive at /etc/usr
I added env variable in /home/user/.profile
I just tested a basic go build on the hello world code.
I get the following error:
The program 'go' is currently not installed. You can install it by typing: sudo apt install golang-go
Why does it ask me to install it (again?)?
open the go documentation download
https://go.dev/dl/
choice your os and go version
download then extract the file
extract the file
open the file and open the terminal
6.Add /usr/local/go/bin to the PATH environment variable.
export PATH=$PATH:/usr/local/go/bin
then check the go version
go version
The location of the binary go is not in your path. Ubuntu does not find it and suggests to install it. Add this line to your file /etc/profile, or better $HOME/.profile:
export PATH=$PATH:/usr/local/go/bin
This is documented in the docs: https://golang.org/doc/install#install
If you want to try this solution before editing any files, you can just execute the above command and try to execute the go command in the shell.
There are paths which needs to be set correctly for you go installation to work
GOROOT points to directory where go is installed
export GOROOT=/usr/lib/go
GOPATH points to you workspace directory
export GOPATH=$HOME/go
These paths need to be added in global path variable.
export PATH=$PATH:$GOROOT/bin
You need to put the go executable in your system path. which you can do by
export PATH=$PATH:/etc/usr/go/bin
You can put the same in /home/user/.profile
just use asdf for installation. You can have several version also :D
Docs: https://asdf-vm.com/#/core-manage-asdf
downlaod the installer form enter link description here, choose intaller for linux that suit your device and then you go to your CLI and use wget or curl :
$ wget https://storage.googleapis.com/golang/go1...
and then extract the file to /usr/local :
$ tar -C /usr/local -xzf go1...
add path binary Go to PATH environment variable :
$ echo "export PATH=$PATH:/usr/local/go/bin" >> ~/.bashrc
$ source ~/.bashrc
and then use go version to check if the Go already installed
If You are using linux then open your terminal and run this command.
sudo apt install golang-go
This command will Install Go lang. in your system. ThankYou
Steps for Go installation:
sudo apt-get update && sudo apt-get -y upgrade    
wget https://dl.google.com/go/go1.17.5.linux-amd64.tar.gz
sudo tar -xvf go1.17.5.linux-amd64.tar.gz
sudo mv go /usr/local/
export GOROOT=/usr/local/go
Add in .bashrc
vi .bashrc
export GOPATH="/root/go"
export GOROOT=/usr/local/go
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH
Download latest version from https://golang.org/doc/install
tar -xzf go1.15.7.linux-amd64.tar.gz
move to /usr/lib/ to folder with version number
sudo mv go /usr/lib/go-1.15
create symkink link on /usr/bin/
ln -s /usr/lib/go-1.15/bin/go /usr/bin/go

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

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

`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