go-install
Install Go packages and dependencies
TLDR
Compile and install the current package
Compile and install a specific local package
Install the latest version of a program, ignoring go.mod in the current directory
Install a program at the version selected by go.mod in the current directory
SYNOPSIS
go install [build flags] [packages]
go install [build flags] path@version
PARAMETERS
-o output
Forces the output file or directory name. For go install, this is less common as it typically places output in $GOBIN.
-ldflags 'flags'
Arguments to pass to the linker during compilation.
-tags 'tag,tag'
A comma-separated list of build tags to satisfy during the build process, influencing conditional compilation.
-race
Enable data race detection for the compiled program. This adds runtime overhead but helps find concurrency issues.
-trimpath
Remove all file system paths from the compiled executable, making it more portable and reproducible.
-v
Print the names of packages as they are compiled and installed, providing verbose output.
-x
Print the commands as they are executed by the Go toolchain, showing underlying system calls.
-mod={readonly|vendor|mod}
Controls module downloading and vendoring behavior. 'readonly' (default) ensures the go.mod file is not modified.
-gcflags 'flags'
Arguments to pass to the Go compiler during compilation.
DESCRIPTION
The go install command is a fundamental tool in the Go ecosystem, primarily used to compile and install Go packages or commands. When invoked, it compiles the specified package(s) and places the resulting executable(s) into the directory specified by the $GOBIN environment variable. If $GOBIN is not set, it defaults to $GOPATH/bin. For non-executable packages (libraries), it caches their compiled forms in $GOCACHE, making subsequent builds faster.
Unlike go build, which typically compiles an executable in the current working directory, go install focuses on placing the compiled output into a standardized, executable path, making it readily available for use. Since Go 1.16, go install has become the official and recommended way to install arbitrary Go commands from remote modules, using the syntax path@version (e.g., go install example.com/cmd@latest). This allows developers to easily fetch, build, and install specific versions of tools without needing to clone repositories or manage local module dependencies manually.
CAVEATS
* Executable Requirement: For go install to produce an executable, the target package must be a main package (i.e., contain a main() function).
* Environment Variables: Its behavior heavily relies on environment variables like $GOBIN and $GOPATH for determining installation locations.
* Module Awareness (Go 1.16+): The path@version syntax requires Go modules to be enabled and typically an internet connection to fetch remote modules.
* Permissions: Installing to system-wide paths (e.g., if $GOBIN is set to a protected directory like /usr/local/bin) may require elevated privileges (e.g., using sudo).
* Tooling vs. Libraries: While it compiles all packages, its primary user-facing function is to install command-line tools. Libraries are generally consumed via go get or module dependencies directly within projects.
ENVIRONMENT VARIABLES
Important environment variables that influence go install's behavior:
- $GOBIN: Specifies the absolute path where compiled executables are placed. If not set, it defaults to $GOPATH/bin.
- $GOPATH: Defines the Go workspace root. Its bin subdirectory is the default target for installations if $GOBIN is not set.
- $GOCACHE: The directory where Go caches build artifacts. go install leverages this for faster subsequent builds and package compilation.
- $GOROOT: Specifies the location of the Go SDK installation itself.
MODULE-AWARE INSTALLATION
Since Go 1.16, go install supports installing specific versions of tools directly from module paths, regardless of the current working directory's module. The syntax path@version (e.g., go install github.com/spf13/cobra-cli@latest or go install golang.org/x/tools/cmd/goimports@v0.1.0) allows fetching, building, and installing a command without adding it as a dependency to your current module. This is particularly useful for managing globally installed command-line tools.
HISTORY
The go install command has been a core part of the Go toolchain since its early days, initially used primarily to install Go programs from within the $GOPATH workspace. With the introduction of Go modules in Go 1.11 and especially with Go 1.16, its role expanded significantly. Go 1.16 officially deprecated go get for installing commands and designated go install (with the path@version syntax) as the preferred and modern way to install executables from remote Go modules directly, without modifying the current module's go.mod file.
SEE ALSO
go build(1), go run(1), go mod(1), go clean(1), go env(1)