Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang equivalent of npm install -g

If I had a compiled Golang program that I wanted to install such that I could run it with a bash command from anywhere on my computer, how would I do that? For example, in nodejs

npm install -g express 

Installs express such that I can run the command

express myapp 

and express will generate a file directory for a node application called "myapp" in whatever my current directory is. Is there an equivalent command for go? I believe now with the "go install" command you have to be in the directory that contains the executable in order to run it

Thanks in advance!

like image 620
Ryan Avatar asked Apr 15 '16 14:04

Ryan


People also ask

Is Pip similar to npm?

npm is the command-line interface to the npm ecosystem. It is battle-tested, surprisingly flexible, and used by hundreds of thousands of JavaScript developers every day. On the other hand, pip is detailed as "A package installer for Python". It is the package installer for Python.

Where does npm global install go?

Path of Global Packages in the system: Global modules are installed in the standard system in root location in system directory /usr/local/lib/node_modules project directory.

What is Npmjs?

2011-08-26. npm is two things: first and foremost, it is an online repository for the publishing of open-source Node. js projects; second, it is a command-line utility for interacting with said repository that aids in package installation, version management, and dependency management.


1 Answers

Update: If you're using Go 1.16, this answer still works, but go install has changed and is now the recommended method for installing executable packages. See Karim's answer for an explanation: https://stackoverflow.com/a/68559728/10490740

Using Go >= 1.11, if your current directory is within a module-based project, or you've set GO111MODULE=on in your environment, go get will not install packages "globally". It will add them to your project's go.mod file instead.

As of Go 1.11.1, setting GO111MODULE=off works to circumvent this behavior:

GO111MODULE=off go get github.com/usr/repo 

Basically, by disabling the module feature for this single command, it will install to GOPATH as expected.

Projects not using modules can still go get normally to install binaries to $GOPATH/bin.

There's a lengthy conversation and multiple issues logged about this change in behavior branching from here: golang/go - cmd/go: go get should not add a dependency to go.mod #27643.

like image 195
Alec Avatar answered Sep 20 '22 18:09

Alec