How do you install golang binaries for all users - go

From all of the Golang tutorials I have read, the way that you install a Go program is by setting the environment variable GOPATH=~/go or something similar, and then you can run go get blah and Go will download the program and put the binary in ~/go/bin. You also have to add ~/go/bin into your $PATH environment variable so that you can run the programs.
How would you go about installing a Golang program into a common location for all users, so that each user on a system did not need to add a PATH environment variable?
The only way I can see of doing it is to make a directory like /usr/local/go, and set that as my GOPATH when I install the program, and then I need to add /usr/local/go/bin into the system-wide $PATH so that all users can run the programs. Is that how it should be done?

There are more environment variables that control how go ... commands work. See: https://golang.org/cmd/go/#hdr-Environment_variables. In this case, you can set GOBIN to your desired install location. Ex:
GOBIN=/path/to/common/binaries go get blah
will put the blah binary in /path/to/common/binaries. Note that you'll likely have to run with sudo.

The cleanest way is to use symlinks, so it's obvious where the tool came from e.g.
$ ls -al /usr/local/bin/brew
lrwxr-xr-x 1 user admin 28 Nov 17 18:38 /usr/local/bin/brew ->
/usr/local/Homebrew/bin/brew
So to make a tool like godocdown global to all users:
ln -s "$GOPATH/bin/godocdown" /usr/local/bin/

Related

GO env variable PATH got messed up

I am trying to install package using go with below command:
go install fyne.io/fyne/v2/cmd/fyne#latest
This is the same as what the instruction said but ideally it should return below message
Users/name/go/bin/fyne
But when I enter the command it has below issue
fyne not found
The package author told me You likely don’t have the GOBIN path added to your PATH variable. I suspect it is golang/go#39531 that comes back to bite you.
When I execute export in the command line:
export
I am getting the below Golang path:
PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin
I thought the path above was messed up because I have done multiple times of installing and uninstalling with uninstalling using below commands
I've done uninstall via:
$ sudo rm -rf /usr/local/go
$ sudo rm /etc/paths.d/go
Althought I've tried to change via:
vim ~/.zshrc
Add a line
export PATH=$PATH:$(go env GOPATH)/bin
It's still not working.
What’s the best approach to resolve adding GOBIN to path?
Thanks!
If all you need is to add GOBIN to PATH, do:
PATH=$PATH:$(go env GOBIN)
Since GOBIN often is ~/go/bin, you usually could get away with just:
PATH=$PATH:~/go/bin
You can add that command to ~/.zshrc to make it persistent and then source it to execute it immediately:
source ~/.zshrc
After that, if your shell remains unable to find fyne, check that current PATH content includes GOBIN with:
echo $PATH
If it doesn't, something went wrong when adding GOBIN to PATH.

Installing soda cli in existing app golang

Description
Issue while installing soda cli in existing app
I downloaded the cli like the documentation
https://gobuffalo.io/en/docs/db/toolbox
Steps to Reproduce the Problem
$ go get github.com/gobuffalo/pop/...
$ go install github.com/gobuffalo/pop/soda
Expected Behavior
when i write soda -v
it must show soda version
Actual Behavior
soda: command not found
Info
OS: ubuntu 21
The problem is very probably that the path where the soda binary gets installed is not in your PATH system variable.
To know where your go binaries are installed, run:
go env | grep GOPATH
This will print:
GOPATH="/path/to/go"
Then you need to add /path/to/go/bin in your environment, through your .bashrc, .zshrc, .profile or whatever you need to have it in your environment, adding the line:
export PATH="$PATH:/path/to/go/bin"
You can do all of this in one single command:
echo "export PATH=\"\$PATH:${$(go env | grep GOPATH | cut -d '=' -f2):1:-1}/bin\"" >> .bashrc
If soda is installed inside go/bin
Just add an alias for soda
Open your terminal and write this command
alias soda="~/go/bin/soda"
To write permanent alias
sudo nano ~/.bashrc
And in the end of file write the alias
alias soda="~/go/bin/soda"

Homebrew and basic usage

I want to install ruby 2.1.2 with rbenv on my computer.
I've installed Homebrew to accomplish this.
After doing brew install rbenv
I get the following messages, of which I don't understand much:
To use Homebrew's directories rather than ~/.rbenv add to your profile:
export RBENV_ROOT=/usr/local/var/rbenv
To enable shims and autocompletion add to your profile:
if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi
==> Summary
🍺 /usr/local/Cellar/rbenv/0.4.0: 31 files, 152K, built in 3 seconds
What does the above mean and what should I do..?
Your profile is a file called .profile inside your home directory, e.g. /Users/youruser/.profile. Create that file in a text editor, and paste those two lines into it. The commands in that file will be executed when you open a new terminal window.
I think it means that if you want to use a global directory for settings etc. instead of a local user-specific directory (i.e. YOUR_HOME/.rbenv), you can add the first line to your profile file.
Moreover, if you want to enable autocompletion for commands etc, you can do the same with the second line.
I'm not sure, but I think that if you use Bash (the mac os x default), your profile file is in your home directory, named .bash_profile

Mac OS X: How do I run binaries that are outside usr/local/bin?

I have installed a program that did not automatically put its binaries into usr/local/bin for me. This means that "command not found" errors happen very often whenever I run scripts in that program. I can fix this by copy-pasting the binaries into the usr/local/bin directory, but I don't want to do this every single time, for every single binary. What would be a more efficient way to make the scripts work?
Thank you very much in advance!
Executables are simply resolved via the $PATH variable. It's set to something like
PATH="/bin:/usr/local/bin:..."
(Try $ echo $PATH.)
When you enter a command:
$ foo
each path will be tried in turn and the first matching executable will be executed.
/bin/foo
/usr/local/bin/foo
To execute something outside the default path, simply enter the whole path to the executable:
$ /home/me/bin/foo
$ cd /home/me/bin
$ ./foo
If you find that you need to do that often and want the shortcut, alter your path:
export PATH="$PATH:/home/me/bin"
(Put this in your shell startup script like ~/.profile to automate that.)
Alternatively, symlink the executable to somewhere in your path:
$ ln -s /home/me/bin/foo /usr/local/bin/foo
Add the directory containing the binary to your $PATH environment variable by editing ~/.bash_profile:
export PATH=$PATH:/your/new/path
You can also edit /etc/paths or add a file to /etc/paths.d, but you need to have admin privilege to do that.

Go fork/exec permission denied error

I recently installed Go onto our server with CentOS 6.3. The install appears to have gone fine. However I made a test "hello world" script, and when I run I get the following output.
fork/exec /tmp/go-build967564990/command-line-arguments/_obj/a.out: permission denied
Now running go env or other go commands seem to work. At first I figured it was a permission issue, however running as root user I get the same thing. An
I encountered this issue today but the solutions above did not work. Mine was fixed by simply running:
$ export TMPDIR=~/tmp/
then I was able to get the script to run with:
$ go run hello.go
hello, world
The only downside is you have to run export TMPDIR every time you want to run an application.
Kudos to Adam Goforth
Just guessing: Your nix perhaps disables for security reasons executing programs in /tmp. It might be configurable in CentOS, but I don't know that.
The alternative solution: It seems you're trying go run to execute a Go program (which is as script as C is a script). Try (assuming $GOPATH=~, the easy possibility) instead a normal build, i.e. instead of
me:~/src/foo$ go run main.go
try
me:~/src/foo$ go build # main.go should not be necessary here
me:~/src/foo$ ./foo
This approach will still use /tmp-whatever to create the binary, IIRC, but it will not attempt to execute it from there.
PS: Do not run these command as root. No need for that with correct setup.
I am using Fedora 31 and got a similar error which brought me here. I could not run the Go debugger used by Jetbrains IntelliJ Ultimate/GoLand without fork/exec & permission denied error. The solution was this:
setsebool deny_ptrace 0
See https://fedoraproject.org/wiki/Features/SELinuxDenyPtrace for details.
exec.Command return this sturct: type Cmd
standard comment:
// Dir specifies the working directory of the command.
// If Dir is the empty string, Run runs the command in the
// calling process's current directory.
Dir string
so resolve, you can pass the exec cmd's path to exec yourself command:
cmd := exec.Command(xxxxx)
cmd.Dir = xxxxPath
and then you can call Run() or other Output() func
Instead of settings TMPDIR which might affect other programs, you can set GOTMPDIR:
mkdir /some/where/gotmp
export GOTMPDIR=/some/where/gotmp
Then add export GOTMPDIR=/some/where/gotmp to your profile (i.e. .bash_profile) to make the variable permanent.
To fix this issue on my Chromebook I just remounted /tmp as executable. There may be security implications in doing this, but since go run works on other platforms I figure maybe it's not that bad (especially on a local dev machine):
sudo mount -i -o remount,exec /tmp/
I added this to my .bash_profile script.
Consider trying:
sudo mount -o remount exec /tmp

Resources