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

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.

Related

Need help on setting up Beego and Bee dev tool

Go is already installed in my system.
and path variable is in .bashrc file.
# Golang
export PATH=$PATH:/usr/local/go/bin
$ go version
go version go1.16.14 linux/amd64
Now, I am willing to work with Beego and bee dev tools. For that, I installed Beego and Bee by using these commands.
$ go get -u github.com/beego/beego/v2
$ go get -u github.com/beego/bee/v2
both got successfully installed. but when I am using the command bee version it is resulting this in the Terminal.
Command 'bee' not found, did you mean:
command 'tee' from deb coreutils (8.30-3ubuntu2)
command 'see' from deb mime-support (3.64ubuntu1)
Try: sudo apt install <deb name>
I am a beginner in the programming world. I am not sure How to resolve it. Can Someone help me with this?
I had the same issue. When using the command bee is installed in home/go/bin. What I did was copy the bee file from there and paste it in usr/local/go/bin. Also I used go install instead of go get and at the end of the url I added the version I wanted, eg. #v2.0.2
You need to have GOPATH determination. If you don't, create a folder which named as go and it needs to have src,pkg,bin folders.
When you install successfully, you can see bee file in bin folder.
NOTE: You can see go environment with go env command.

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

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.

The git user is unable to execute bundle during GitLab installation

I am trying to install GitLab on a Debian Wheezy and experiencing an issue I don't understand.
Following the install guide for version 5.0:
step 1: was run as explained
step 2: I used rvm (multi-user install) instead (ruby-1.9.3-p392)
steps 3 to 5: were run as explained (MySQL chosen)
step 6: everything works fine until the "Install Gems" section, where I get the following error:
/home/git/gitlab$ sudo -u git -H bundle install --deployment --without development test postgres
sudo: unable to execute /usr/local/bin/bundle: No such file or directory
I run this as my normal user (in the sudo and rvm groups), and the git user is not a sudoer.
I think that the line sudo gem install bundler, in step 2, does not grant the git user to execute bundle properly, but I don't know how to fix this.
I probably did something wrong but cannot figure out what it is, as I tried to respect the standard instructions as close as possible.
Any clue about this? Thanks, I am losing hope...
For information, I have written to the GitLab mailing-list about this problem but did not get any answer. Moreover, last time I asked something similar on ServerFault, I have been advised to post on StackOverflow instead... hence this question :)
When I do $ which bundle, I get /usr/local/rvm/gems/ruby-1.9.3-p392/bin/bundle.
The git user has been created with the --disabled-login flag and thus I cannot login as git to run bundle.
When I do $ sudo -u git bundle, I get
sudo: unable to execute /usr/local/bin/bundle: No such file or directory
That means git user has not /usr/local/rvm/gems/ruby-1.9.3-p392/bin in its PATH, and you cannot modify its .profile or .bashrc because of the --disabled-login flag.
It seems that running sudo -i -u git bundle instead of sudo -u git bundle does the trick
That is the safest route, and will execute the command as git, but will simulate first an initial login, using the PATH defined in .profile and .bashrc of user root.
See "How does sudo search the path for executable?" for more.
On my local machine after upgrade from 6.4 to 7.7 I had such issue.
The compilation of ruby was made under root account.
So need permissions to read compiled ruby and installed bundle.
chmod ugo+rx /usr/local/bin/bundle
chmod -R ugo+rX /usr/local/lib/ruby/
In production mode You could be more strict.

Resources