Where are globally-installed Go tools located & how to execute them (when using Go similarly to 'npm install -g')

Jun 2, 2021 08:40 · 220 words · 2 minute read

With some command line tools written in Go (like scc, a tool for counting a project’s lines of code), you’ll see install instructions like this:

go get -u github.com/boyter/scc/

But where is this package installed, how do you actually execute the command then? TLDR: In this case, the binary will be stored in /home/<USER>/go/bin/scc.

Packages like this are installed into your GOPATH. Some people will have already set up an environment value GOPATH, and thus echo $GOPATH will output this path. The binary is then stored in GOPATH/bin. But: If you have mostly used Go mostly with the newer “modules” feature, it is likely that you didn’t have a need to set this up. Then, Go will use a default GOPATH value, and you can find it out with go env GOPATH. Most likely, it will be /home/<USER>/go.

If you using tools like this often, it might make sense to add this folder to your PATH, so you can simply execute scc instead of having to specify the full path like /home/markus/go/bin/scc. For this, add this line to the end of your .bashrc/.zshrc:

export PATH="$PATH:/home/<USER>/go/bin"

Note: If your current working directory (where you executed go get) contains a go.mod file, the package will get added to the go.mod file. But it will still be available in your GOPATH/bin.