LinuxCommandLibrary

go-install

Install Go packages and dependencies

TLDR

Compile and install the current package

$ go install
copy

Compile and install a specific local package
$ go install [path/to/package]
copy

Install the latest version of a program, ignoring go.mod in the current directory
$ go install [golang.org/x/tools/gopls]@[latest]
copy

Install a program at the version selected by go.mod in the current directory
$ go install [golang.org/x/tools/gopls]
copy

SYNOPSIS

go install [-i] [-n] [-v] [-x] [build flags] [packages]

PARAMETERS

-i
    Install the packages even if the package is already installed (reinstalls). Deprecated.

-n
    Print the commands but do not execute them.

-v
    Verbose; print the names of packages as they are compiled.

-x
    Print the commands.

[build flags]
    Build flags are passed to the go tool build command.

[packages]
    List of packages (import paths) to install.

DESCRIPTION

The go install command compiles and installs packages.

It takes a list of import paths as arguments, resolves their dependencies, and then compiles and installs the packages and their dependencies.

The resulting binaries are placed in the $GOBIN directory, which defaults to $GOPATH/bin if $GOBIN is not set. If the current module contains a main package the compiled binary will be placed in the directory mentioned earlier.

Dependencies are installed into the $GOPATH/pkg directory.

go install is a fundamental tool for building and installing Go programs and libraries.

CAVEATS

Requires a properly configured Go environment ($GOROOT, $GOPATH, $GOBIN). May not work as expected outside of a Go module.

MODULE MODE

When working within a Go module (a directory containing a go.mod file), go install respects the module's dependency declarations. It uses the versions specified in the go.mod file and downloads dependencies as needed. This ensures reproducible builds.

BUILD TAGS

Build tags can be used to conditionally compile code based on the target operating system or architecture. Use the -tags option with go install to specify build tags.

PACKAGE PATHS

Package paths can be either standard library packages (like fmt or net/http) or import paths to packages in your project or in third-party repositories. For example: go install github.com/user/myrepo/mypackage.

HISTORY

The go install command has been a core part of the Go toolchain since the language's early days. It has evolved alongside Go, with changes primarily focused on improved dependency management and integration with the module system.

SEE ALSO

go build(1), go get(1), go mod(1)

Copied to clipboard