164
~/src/go-statsd-client> echo $GOPATH
/Users/me/gopath
~/src/go-statsd-client> echo $GOROOT
/usr/local/Cellar/go/1.1.1\
~/src/go-statsd-client> go install
go install: no install location for directory /Users/me/src/go-statsd-client outside GOPATH

No matter what structure the project is in this always fails with the same message. Go build works perfectly.

Here is my go env

GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/me/gopath"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.1.1"
GOTOOLDIR="/usr/local/Cellar/go/1.1.1/pkg/tool/darwin_amd64"
CC="gcc"
GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread -fno-common"
CGO_ENABLED="1"

This is on Mac OSX Mountain Lion and go was installed with homebrew.

Rot-man
  • 18,045
  • 12
  • 118
  • 124
tgandrews
  • 12,349
  • 15
  • 43
  • 55

12 Answers12

212

For any OS X users and future me, you also need to set GOBIN to avoid this confusing message on install and go get

mkdir bin 
export GOBIN=$GOPATH/bin
IKavanagh
  • 6,089
  • 11
  • 42
  • 47
Benjamin Wootton
  • 2,137
  • 1
  • 10
  • 2
  • 4
    You should not need to [set `GOBIN`](https://github.com/golang/go/wiki/InstallTroubleshooting). If setting it to [`$GOPATH/bin`](https://golang.org/cmd/go/#hdr-GOPATH_environment_variable) makes a difference, then something else is wrong with your setup or you're doing something you shouldn't. – Dave C Sep 02 '15 at 18:32
  • I also got the same error message but found that GOPATH was set but not exported in bash. export GOPATH solved the problem – Sathya Nov 21 '15 at 07:00
  • 32
    @DaveC I too have the same problem on OS X. Setting the GOBIN fixed it. – nJoshi Dec 27 '15 at 03:18
  • 2
    Thank you so much, it looks like something with the official OS X package is wrong indeed requiring you to set GOBIN, still valid for 10.11.4 (El Capitan) – Jonas D. Apr 30 '16 at 22:09
  • 4
    This is correct fix for Ubuntu 16.04 as well, with binary package from [http://golang.org/dl/](http://golang.org/dl/) – suside May 17 '16 at 06:19
  • I had the same issue when using the golang docker image on Gitlab CI. exporting the `GOBIN` made the difference. – NobodysNightmare Oct 31 '16 at 12:29
  • 1
    This worked perfectly on maOS Sierra 10.12.3 Thanks! – Greg Hilston Feb 16 '17 at 19:42
  • 1
    Same issue on OSX, Thank You! – Greg Aug 31 '17 at 17:41
  • I had to set it on MacOS High Sierra just now to make it work. `go version go1.7.5 darwin/amd64` installed via brew. – derFunk Nov 15 '17 at 08:50
  • Still had to do this as of go1.12.4 – prolink007 Apr 30 '19 at 20:20
  • Still have to set GOBIN in Catalina 10.15. – John P Nov 04 '19 at 07:12
  • thanks , I was missing the GOBIN. after setting GOBIN, install worked for as I am setting up my workspace in a different folder than GOROOT. – enthusiast Feb 27 '20 at 10:46
  • On Ubuntu 20.04 I ran into this error when I didn't have GOBIN set, even though GOPATH was at the default location, and contained a 'bin' directory. – PePa Sep 22 '21 at 06:16
119

When you provide no arguments to go install, it defaults to attempting to install the package in the current directory. The error message is telling you that it cannot do that, because the current directory isn't part of your $GOPATH.

You can either:

  • Define $GOPATH to your $HOME (export GOPATH=$HOME).
  • Move your source to within the current $GOPATH (mv ~/src/go-statsd-client /User/me/gopath).

After either, going into the go-statsd-client directory and typing go install will work, and so will typing go install go-statsd-client from anywhere in the filesystem. The built binaries will go into $GOPATH/bin.

As an unrelated suggestion, you probably want to namespace your package with a domain name, to avoid name clashing (e.g. github.com/you/go-statsd-client, if that's where you hold your source code).

Gustavo Niemeyer
  • 22,007
  • 5
  • 57
  • 46
  • 5
    In my case this error was caused by there not being a `$GOPATH/bin` directory. Creating this folder was that was needed -- I didn't need to set `GOBIN` env var. – xentek Jan 21 '15 at 07:20
  • 4
    @RobertReiz while I share your frustration, I should point out that most other languages do in fact use these, but maybe they're not as visible. For example, Python (a language famous for its simplicity) has `PYTHONHOME` instead of `GOROOT` and `PYTHONPATH` instead of `GOPATH`. Even (GC)C has `LIBRARY_PATH`, `C_INCLUDE_PATH`, etc. – Hut8 Jan 31 '16 at 00:48
  • What argumen can I give to go install to install it where GOPATH point and not in the current directory ? I have 6 project using the same dependency and I don't want to install it 6 times, specially because I need to keep them in sync. – Sistr Apr 16 '16 at 11:59
  • 1
    I get this error after `export GOPATH=$PWD` (standing at the root of a Golang clone tree), your answer can't possibly explain the error in this case. – ulidtko Jul 01 '16 at 11:19
  • 3
    For me, putting it in $GOPATH was not enough. It needed to be in a directory under $GOPATH/src/ – Thomas Nov 05 '16 at 11:15
  • 1
    This didn't solve problem for me. But this one did. http://stackoverflow.com/questions/26134975/go-install-no-install-location-for-directory-outside-gopath#answer-27428046 – toshi0383 Nov 15 '16 at 12:32
  • Hope this helps. OSX is actually case-insensitive. Though `/my/gopath` and `/my/Gopath` mean the same thing for OSX, but go seems to treat them differently. So, before `go install`, make sure the `pwd` command in current directory has the same case as the one you have in `GOPATH`. – Yang Apr 18 '17 at 13:06
  • I had to make `$home/my-work-space/bin`, then `export GOBIN=` the new bin directory. – Ashley Coolman Oct 21 '17 at 12:51
  • 12
    I'm sure there is some very good reason to justify the existence of `GOPATH`, but from the outside looking in, this seems completely mindless. Why do I have to mutate global environment variables just to build a package locally – Alexander Apr 24 '18 at 22:07
  • @Alexander you can set the environment variable in just the one shell where you run the command if you want, you don't have to set it globally – rakslice Jun 06 '18 at 09:21
  • 2
    I don't get it. I have a location where I checkout my code say `~/work`. Isn't it weird to also have to store dependent source files there? Why couldn't I just work here and go retrieve dependencies in `~/go`? – Alper Dec 27 '18 at 11:00
30

You are using go install on a directory outside the GOPATH folder. Set your GOBIN env variable, or move src folder inside GOPATH.

GOPATH/
     bin/
     src/
       go-statsd-client/

More info: GO BUILD Source code, line 296

Andrea Di Persio
  • 3,246
  • 2
  • 24
  • 23
27

You need to setup both GOPATH and GOBIN. Make sure you have done the following (please replace ~/go with your preferred GOPATH and subsequently change GOBIN). This is tested on Ubuntu 16.04 LTS.

export GOPATH=~/go 

mkdir ~/go/bin

export GOBIN=$GOPATH/bin

The selected answer did not solve the problem for me.

Nik
  • 2,885
  • 2
  • 25
  • 25
Zhenhua
  • 2,389
  • 15
  • 10
7

You'll want to have 3 directories inside your chosen GOPATH directory.

GOPATH
     /bin
     /src
       /someProgram
        program.go
       /someLibrary
        library.go
     /pkg

Then you'll run go install from inside either someProgram (which puts an executable in bin) or someLibrary (which puts a library in pkg).

Earl Zedd
  • 1,101
  • 10
  • 11
3

I had this problem on Windows.

My problem was that my %GOPATH% environment variable was set to

C:\Users\john\src\goworkspace

instead of

C:\Users\john\src\goworkspace\

Adding the missing trailing slash at the end fixed it for me.

eckza
  • 2,212
  • 3
  • 22
  • 28
  • 1
    holy cow you just saved me from a potentially multi-hour headache. I'm on Ubuntu but had the same problem. Was extremely diligent about folder names, folder structure, prefacing with src/github.com/xyabz/ and everything but go install kept giving me errors. – fIwJlxSzApHEZIl Oct 07 '16 at 05:15
3

For what it's worth, here's my .bash_profile, that works well for me on a mac with Atom, after installing go with Homebrew:

export GOROOT=`go env GOROOT`
export GOPATH=/Users/yy/Projects/go
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOBIN
JRun
  • 3,328
  • 6
  • 27
  • 41
2

In my case (OS X) it was because I have set GOPATH to /home/username/go (as per the book) instead of /Users/username/go

catamphetamine
  • 4,489
  • 30
  • 25
2

I'm on Windows, and I got it by giving command go help gopath to cmd, and read the bold text in the instruction,

that is if code you wnat to install is at ..BaseDir...\SomeProject\src\basic\set, the GOPATH should not be the same location as code, it should be just Base Project DIR: ..BaseDir...\SomeProject.

The GOPATH environment variable lists places to look for Go code. On Unix, the value is a colon-separated string. On Windows, the value is a semicolon-separated string. On Plan 9, the value is a list.

If the environment variable is unset, GOPATH defaults to a subdirectory named "go" in the user's home directory ($HOME/go on Unix, %USERPROFILE%\go on Windows), unless that directory holds a Go distribution. Run "go env GOPATH" to see the current GOPATH.

See https://golang.org/wiki/SettingGOPATH to set a custom GOPATH.

Each directory listed in GOPATH must have a prescribed structure:

The src directory holds source code. The path below src determines the import path or executable name.

The pkg directory holds installed package objects. As in the Go tree, each target operating system and architecture pair has its own subdirectory of pkg (pkg/GOOS_GOARCH).

If DIR is a directory listed in the GOPATH, a package with source in DIR/src/foo/bar can be imported as "foo/bar" and has its compiled form installed to "DIR/pkg/GOOS_GOARCH/foo/bar.a".

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.

Here's an example directory layout:

GOPATH=/home/user/go

/home/user/go/
    src/
        foo/
            bar/               (go code in package bar)
                x.go
            quux/              (go code in package main)
                y.go
    bin/
        quux                   (installed command)
    pkg/
        linux_amd64/
            foo/
                bar.a          (installed package object)

..........

if GOPATH has been set to Base Project DIR and still has this problem, in windows you can try to set GOBIN as Base Project DIR\bin or %GOPATH%\bin.

yu yang Jian
  • 6,680
  • 7
  • 55
  • 80
1

Careful when running

export GOPATH=$HOME

Go assume that your code exists in specific places related to GOPATH. So, instead, you can use docker to run any go command:

docker run -it -v $(pwd):/go/src/github.com/<organization name>/<repository name> golang

And now you can use any golang command, for example:

go test github.com/<organization name>/<repository name> 
Omer Levi Hevroni
  • 1,935
  • 1
  • 15
  • 33
0

In windows, my cmd window was already open when I set the GOPATH environment variable. First I had to close the cmd and then reopen for it to become effective.

lode
  • 504
  • 6
  • 21
-1

On OSX Mojave 10.14, go is typically installed at /usr/local/go.

Hence, setup these ENVs and you should be good to go.

export GOPATH=/usr/local/go && export GOBIN=/usr/local/go/bin

Also, add these to your bash_profile or zsh_profile if it works.

echo "export GOPATH=/usr/local/go && export GOBIN=/usr/local/go/bin" >> ~/.bash_profile && source ~/.bash_profile

A Null Pointer
  • 2,261
  • 3
  • 26
  • 28