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/