GOBIN 未设置: 无法运行去安装

我正在尝试为 main.go 文件安装自定义软件包

go install custom.go

我得到了这个错误

go install: no install location for .go files listed on command line (GOBIN not set)

如何设置 GOBIN?

122171 次浏览

Update 2020: since Go 1.11 and the introduction of Go modules, GOPATH is not needed anymore per project, and defaults to ~/go for global tools/project you would go get.

Go 1.16 (Q1 2020) should default ABC0 to GOPATH[0]/bin.

But for now, for any project using modules, you would not have an error message like "go install: no install location ..." anymore.


Original answer 2014:

Check your GOPATH variable.
Make sure:

  • your sources are under GOPATH/src
  • you have a bin folder within your GOPATH folder.

See GOPATH environment variable (where 'DIR' is a GOPATH folder):

The bin directory holds compiled commands.
Each command is named for its source directory, but only the final element, not the entire path. That is, the command with source in DIR/src/foo/quux is installed into DIR/bin/quux, not DIR/bin/foo/quux. The "foo/" prefix is stripped so that you can add DIR/bin to your PATH to get at the installed commands.

If the GOBIN environment variable is set, commands are installed to the directory it names instead of DIR/bin. GOBIN must be an absolute path.


For instance, this thread illustrates what happen in the case where a go build is done outside of GOPATH/src:

Looks like your GOPATH is set to ~/go but you ran the go install command on ~/dev/go

See Go Build

The Go path is a list of directory trees containing Go source code. It is consulted to resolve imports that cannot be found in the standard Go tree.

If you have done go build, you can also try a go install (no custom.go): you want to install the package, not a single file.

As pervious answers pointed out, if your GOPATH env is correctly set to your workspace you don't need to set GOBIN env variable.

Please check your go environment variables by running $go env | grep -i "^GO" and look out for GOROOT and GOPATH to check if GOROOT is pointing to your GO source installation and GOPATH pointing to your workspace.

If everything is correct then navigate to the subdir where yourpkg.go resides then run $go build (without file name) first and $go install (again withour file name) second , if you don't see any error message on the screen your package is ready at your workspace/pkg/youros/../yourpackage.a

I set the GOBIN path and that worked for me

export GOBIN=[WorkspacePath]/bin

On windows with cygwin it seems to be a good idea to set up GOBIN to $GOPATH/bin.

and remember to properly escape the windows file name separator:

$ echo $GOROOT
C:\Go\


carl@rainier ~/gocode/src/github.com/user/hello
$ echo $GOPATH
C:\cygwin64\home\carl\gocode


carl@rainier ~/gocode/src/github.com/user/hello
$ echo $GOBIN
C:\cygwin64\home\carl\gocode\bin

As a beginner, I ran across this error when I was trying out various go commands (build, run, and install). In short, you cannot go install a filename.go. You can only install a package.

This was confusing, because I had learned that:

nate:~/work/src/dir $ go run hello/hello.go
hello, world.

works great. But I couldn't figure out why install wouldn't work:

nate:~/work/src/dir $ go install hello/hello.go
go install: no install location for .go files listed on command line (GOBIN not set)
nate:~/work/src/dir $ go install hello
can't load package: package hello: cannot find package "hello" in any of:
/opt/go/src/hello (from $GOROOT)
/home/ubuntu/work/src/hello (from $GOPATH)

No matter what directory I was in:

nate:~/work/src/dir $ cd hello
nate:~/work/src/dir/hello $ go install hello.go
go install: no install location for .go files listed on command line (GOBIN not set)
nate:~/work/src/dir/hello $ go install hello
can't load package: package hello: cannot find package "hello" in any of:
/opt/go/src/hello (from $GOROOT)
/home/ubuntu/work/src/hello (from $GOPATH)

This confusion is because go run only works with Go source files (filenames that end in .go) and go install only accepts packages. Packages are named by their import paths or file system path. Thus:

nate:~/work/src/dir $ go install dir/hello
nate:~/work/src/dir $ go install ./hello/
nate:~/work/src/dir/hello $ go install .

all work great. The first refers to the package by import path, (given that $GOPATH="/home/nate/work", the go tools look for source code in /home/nate/work/src), the others are interpreted as file system paths because of the leading periods.

See also the GOPATH docs.

Actually there are 2 different kinds of behavior.

go install <package>

this is documented in Compile and install packages and dependencies You don't need GOBIN if you set GOPATH correctly.

go install <gofile>

this is not documented and you need GOBIN env variable in this mode.

Regarding setting GOBIN variable version not requiring it and just relying on GOPATH:

  1. GOBIN is required if we don't have a package, i.e. the file is directly in the GOPATH directory. This is likely when we are trying out the Go features as learners

  2. For typical Go projects, the files are under the package directories. For these, GOPATH is good enough.

  3. In other words, both the following solutions would work: a. Set GOBIN explicitly as $GOPATH/bin [only for learning purposes, can avoid] b. Create a subdirectory which would be your package name and move the .go files to it

  4. I guess Go utilities should remove the above error and handle the scenario better - based on whether the argument is a directory or a source file

For *nix system, look where go is installed, executing following command:

$ which go

which output let's say:

/usr/local/go/bin/go

then add following entries in ~/.bash_profile or in ~/.zshrc:

export GOROOT=/usr/local/go
export GOPATH=$GOROOT/src //your-go-workspace
export GOBIN=$GOROOT/bin //where go-generate-executable-binaries


PATH=$PATH:$GOPATH:$GOBIN


export PATH

P.S: Don't forget to source ~/.bash_profile or ~/.zshrc, as follows:

$ source ~/.bash_profile

From https://golang.org/cmd/go/#hdr-Environment_variables:

GOBIN The directory where 'go install' will install a command.

and https://golang.org/cmd/go/#hdr-GOPATH_environment_variable:

If the GOBIN environment variable is set, commands are installed to the directory it names instead of DIR/bin. GOBIN must be an absolute path.

and https://golang.org/cmd/go/#hdr-Modules__module_versions__and_more

In module-aware mode, GOPATH no longer defines the meaning of imports during a build, but it still stores downloaded dependencies (in GOPATH/pkg/mod) and installed commands (in GOPATH/bin, unless GOBIN is set).

So, it seems basically you can use GOBIN to temporarily or permanently override the default binary install location (ie $GOPATH/bin). I had success installing a 1-file go "script" using env GOBIN=$HOME/bin/ go install testfile.go. This was done using go v1.11.

For WINDOWS users

Open a command prompt (Win + r then type cmd) or a powershell window (Win + x then type i).

Setting the GOPATH

NOTE: GOPATH must not be the same path as your Go installation.

go env -w GOPATH=c:\your-go-work

More details in the link below https://github.com/golang/go/wiki/SettingGOPATH#windows

Setting GOBIN

go env -w GOBIN=C:\somewhere\else\bin

I recommend to check the code example provided by golang. It helped me a lot. https://golang.org/doc/code.html#Command

In Windows:

go env -w GOBIN=C:\Users\yourname\go\bin

Confirm with go env command that GOBIN is set, then go install command properly saves the executable properly in the bin directory.

I too had the same trouble(GOBIN need not be set separately), make sure you have the following

  1. GOPATH is set (this also automatically sets GOBIN as $GOPATH/bin)
  2. GOPATH has the following directories bin, pkg, src
  3. custom.go is placed under src/<your_module>
  4. then run go install <your_module>
  5. you can find the check for the compiled binary(<your_module>) under bin folder

Try

export GOROOT=""

export PATH=$PATH:$GOROOT/bin