Global
Install a Go package globally so that it can be run anywhere as a CLI tool
Use the go get
subcommand and pass a URL without protocol.
URL
The URL should point to directory containing a main.go
file.
This also works if there is a single Go file of another name e.g. cmd/staticcheck/staticcheck.go.
Install package from root directory
Point a repo with main.go
at the root.
$ go get REPO_URL
Here using MichaelCurrin/go-project-template.
$ go get github.com/MichaelCurrin/go-project-template
Run the command which now exists in GOBIN
.
$ go-project-template -h
Install target version
$ go get URL@TAG
$ go install URL@TAG
Note this doesn’t not work in the repo root as you’ll get an error.
Here using staticcheck.go.
$ go install honnef.co/go/tools/cmd/staticcheck@v0.2.0
$ go install honnef.co/go/tools/cmd/staticcheck@v0.2.0
$ staticcheck --version
staticcheck 2021.1 (v0.2.0)
$ go install honnef.co/go/tools/cmd/staticcheck@v0.2.1
$ staticcheck --version
staticcheck 2021.1.1 (v0.2.1)
In a module, you can omit the version.
$ cd my-go-repo
$ go get honnef.co/go/tools/cmd/staticcheck
But you’ll get an error outside a module if you use install
.
$ cd ~
$ go install honnef.co/go/tools/cmd/staticcheck
go install: version is required when current directory is not in a module
Try 'go install honnef.co/go/tools/cmd/staticcheck@latest' to install the latest version
But no issue if you use get
.
$ cd ~
$ go get honnef.co/go/tools/cmd/staticcheck
Install package from a subdirectory
$ go get REPO_URL/APP_PATH
Typically, the package will bee on GitHub repo URL and include a path to a package in the cmd
directory.
Examples
Generic example
Give script cmd/myapp/main.go
in MichaelCurrin/my-app
.
$ go get github.com/MichaelCurrin/my-app/cmd/myapp
If GOBIN
is in your PATH
, you can run the CLI tool from anywhere:
$ myapp -h
ESBuild example
Download the ESBuild CLI package.
$ go get github.com/evanw/esbuild/cmd/esbuild
Check that it is accessible.
$ type esbuild
esbuild is /Users/mcurrin/go/bin/esbuild
Run it:
$ esbuild -h
...
- GitHub repo: evanw/esbuild
- GitHub module path: cmd/esbuild
Custom domain
$ go get honnef.co/go/tools/cmd/staticcheck
Upgrade
You can’t use an upgrade flag with install
, but you can use the flag with get
as below.
$ go get -u URL
e.g.
$ go get -u github.com/MichaelCurrin/go-project-template/cmd/myapp
go: github.com/cpuguy83/go-md2man/v2 upgrade => v2.0.0
go: github.com/russross/blackfriday/v2 upgrade => v2.1.0
Check installed packages
Show names in GOBIN
, sorted by most recently updated.
$ ls -1 -t ~/go/bin
go-project-template
...
gopls
golint
goreturns
...