Pocket Gophers

go install vs go build


Both go install and go build will compile the package in the current directory when ran without additional arguments.

If the package is package main, go build will place the resulting executable in the current directory. go install will put the executable in $GOPATH/bin (using the first element of $GOPATH, if you have more than one).

If the package is not package main, go install and go build will compile the package, displaying any errors encountered

Starting with go1.10, the way the go compiler decides if a package needs to be rebuilt has changed. This affects how the compiler reuses results from previous compilations.

go1.10 and later

go install and go build will cache compiled dependencies in $GOTMPDIR (which uses your system's temporary directory by default). There is generally no reason to go install packages other than package main, but you also won't hurt anything.

go1.9.x and earlier

go install will install compiled dependencies in $GOPATH/pkg, which allows the compiler to reuse them in a future compilation. To achieve the same effect with go build, use go build -i; otherwise go build will discard the compiled dependencies after the build process is completed.

Dig into the Fundamentals of Go

Subscribe to receive a weekly email covering a Go fundamental. Be it the language, its tooling, or its packages, you will learn what you need to know.