Go Reference ยท The go Command and Tools
The go install command
Reference for go install: where binaries are installed (GOBIN, GOPATH), installing tools with @version, version queries, and how it differs from go build.
go install compiles and installs packages. For a main package it builds the program and copies the executable into an install directory, so you can run it by name from any folder. It is how Go developers install tools such as stringer, staticcheck, gopls or govulncheck.
The command comes in two forms. With a version suffix (go install example.com/[email protected]) it installs that exact version of a command from the module system, ignoring whatever module you are standing in. Without a suffix it installs the packages of the current module, which is useful for your own commands.
go install golang.org/x/tools/gopls@latest go install ./cmd/myapp # your own command, from the current module go install honnef.co/go/tools/cmd/staticcheck@v0.6.0
Try it
Installing needs a network and a shell, so the examples model the two decisions the tool makes: where a binary goes, and which version a query resolves to. The logic follows the documented rules.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Syntax
go install [build flags] [packages] go install [build flags] package@version [package@version...]
| Part | Description |
|---|---|
package |
A package or pattern in the current module (., ./cmd/...). Main packages are installed as executables; other packages are compiled into the build cache. |
package@version |
A command at a version query. Every argument must name a main package, and all arguments must use the same version. |
@latest |
The latest tagged release, ignoring pre-releases when a release exists. |
@v1.2.3, @main, @3f9a2c1 |
An exact version, a branch, or a commit. Pseudo-versions are created for untagged revisions. |
| Build flags | The same as go build: -ldflags, -tags, -trimpath, -race, -v. |
Install location: $GOBIN if set, otherwise $GOPATH/bin (first entry of GOPATH), and $HOME/go/bin when neither is set. Check it with go env GOBIN GOPATH. Add that directory to your PATH to run the tools by name.
Errors: with a version suffix, go install fails if the argument is not a main package, or if the module's go.mod contains replace or exclude directives (they are not allowed in this form). Without a suffix, installing a non-main package just compiles it.
Usage notes
go install x@versiondoes not edit yourgo.mod. It is independent of the module you are in. To track tool versions for a project, use thetooldirective ingo.mod(Go 1.24+), added withgo get -tool; thengo tool nameruns them. See go get and go mod.- No more
go getfor tools. Since Go 1.18,go getonly edits dependencies. Installing a command isgo installwith a version. - Newer toolchains are fetched automatically. If the tool's
go.modrequires a newer Go than yours, theGOTOOLCHAINsetting decides whether Go downloads it. - Install updates in place. Running the same command again with a newer version replaces the old binary. The old version is not kept.
- Own commands.
go install ./...from your module installs every command it contains. The binary is named after the directory, not the module. - Compared to
go build:buildleaves the executable in the current directory;installmoves it to the shared bin folder and also caches compiled non-main packages. Useinstallfor tools,buildfor artifacts you ship. - Uninstalling is just deleting the file from the bin directory. There is no
go uninstall. - Security. The binary you install runs with all the privileges of your user, so install tools from sources you trust, pin exact versions in CI instead of
@latest, and inspect the result withgo version -m.
Related
- go build, go get, go mod, go env
- Lessons: Dependencies, Modules, The go Command and Basic Tooling