Skip to content
Advertisement

go install always uses GOROOT/bin instead of GOPATH

I’m having annoying issue with go install command.

Every time I try to run it within the src directory of my GOPATH the resulted file is getting created in GOROOT/bin directory for some reason.

I verified my environmental variables in .bashrc and also run ‘go env’ (see below) and couldn’t find any issues:

.bashrc

export GOBIN=$HOME/dev/src/go/bin
export GOPATH=$HOME/dev/go-dev
export PATH=$PATH:$GOBIN:$GOPATH/bin

go env

GOARCH="amd64"
GOBIN="/home/user/dev/src/go/bin"
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/user/dev/go-dev"
GORACE=""
GOROOT="/home/user/dev/src/go"
GOTOOLDIR="/home/user/dev/src/go/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread"
CGO_ENABLED="1"

This post has a similar problem except I have GOPATH in my env (I tried the solution but it didn’t help).

When I tried to create the test library using official GoLang site and run go install I’ve got a proper file created in $GOPATH/pgk/linux_amd64 but not in the bin directory.

Am I missing something in my configuration?

Advertisement

Answer

Official documentation about the go tool:

If DIR is a directory listed in the GOPATH…

If the GOBIN environment variable is set, commands are installed to the directory it names instead of DIR/bin

There has been a discussion about this topic on the mailing list where this is further explained:

(a) If you don’t set your GOBIN env variable, you get the Go compiler binaries going in GOROOT/bin whereas the your binaries are going in GOPATH/bin. (I personally like this separation of binaries.)

(b) If you set your GOBIN to anything, then both the Go binaries and your binaries are going to GOBIN.

The solution in your case would be to not set your GOBIN.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement