I’m confused about the difference between go build and go install. I expected go install to behave like make install, placing the compiled binary in a final location (e.g., /usr/local/bin/my_app), but it seems to put files in GOROOT/bin by default.
Can I change the installation location, or do I need to write a Makefile? How does go install work exactly?
From my experience, here’s a practical approach:
-
Use go build during development, quick, in-place compilation.
-
Use go install when you want the binary installed in your bin folder so you can run it anywhere.
-
You rarely need a Makefile just to move binaries, GOBIN handles it nicely.
Extra tip: In Go 1.17+, you can do:
go install example.com/mypkg@latest
It fetches, builds, and installs the latest version of a module in one step. Super handy for CLI tools!
By default:
$GOPATH/bin (or $HOME/go/bin) is the install location.
If you want to change it, set GOBIN:
export GOBIN=/usr/local/bin
go install myapp
Now the binary will go directly to /usr/local/bin, no Makefile needed.
I do this often for CLI tools I want accessible system-wide.
I’ve been there
. The difference between go build and go install trips up a lot of people at first:
go build compiles the package in the current directory and creates a binary there (or in -o <output> if you specify). It doesn’t move it anywhere automatically.
go install compiles AND installs the binary into your $GOPATH/bin (or $GOBIN if you set it).
Example:
# just builds binary in current folder
go build main.go
# builds and installs into $GOPATH/bin
go install myapp
I usually use go install when I want the executable available globally via PATH, without worrying about where I am in the project.