John S Perayil
John S Perayil

Reputation: 6345

Go : go get $GOPATH error, when GOPATH is set

When running the go get command :

sudo go get github.com/go-sql-driver/mysql

I get the following error

package github.com/go-sql-driver/mysql: cannot download, $GOPATH not set. For more details see: go help gopath

However $GOPATH is already set.

Running echo $GOPATH gives /Users/userxyz/Desktop/Code

Running go env gives

.....

GOPATH="/Users/userxyz/Desktop/Code"

...

GOROOT="/usr/local/go"

.....

I have already tried setting GOPATH as an environment variable by adding the following lines

export GOPATH="$HOME/Desktop/Code"
export PATH=$PATH:$GOPATH/bin

to the following files, alternatively

~/.profile (/etc/profile)
~/.bashrc
~/.bash_profile

Upvotes: 4

Views: 5917

Answers (5)

MrMesees
MrMesees

Reputation: 1613

In my case, I had imported an incorrect GOPATH that did not exist and an incorrect GOROOT in my PATH

setting my export GOROOT=/opt/go then export GOPATH=$HOME/work, ensuring $HOME/work existed and had access for my user seems to have solved the problem for go get {whattoget}, then ensuring export GOPATH=$HOME/work export PATH=$PATH:$GOROOT/bin:$GOPATH/bin was in my ~/.profile helped. Nothing in this thread seemed to quite fit.

Going forward I might just start using Docker to build my go projects, as it's a bit of a pain, and the documentation and errors are lengthy but don't seem to explain what is wrong in any meaningful and clear way (GOROOT and GOPATH were both setup, but PATH GOROOT and GOPATH all seemed to be a little off).

Upvotes: 0

Jake Kim
Jake Kim

Reputation: 1

Just realized after writing the response that it's almost 4 years too late but I'll leave it up just in case there are others who might need the answers...

So it seems like you might have created the directory with sudo or with root privileges. If you try $go get with a regular user you get a permissions error and when you try it with sudo you get a GOPATH unset error. I've had this problem recently, and I solved it by simply changing the ownership of the directory.

export GOPATH=/directory/of/your/goproject
chown -R ubuntu@staff $GOPATH
cd $GOPATH && go get 

Here's just so you have some env variables to double check with:

export GOPATH=/home/ubuntu/work export
PATH=$PATH:$GOROOT/bin:$GOPATH/bin

Keep in mind that there shouldn't be a need for you to declare or change $GOROOT.


Finally, you might want to use [profile.d][1] to export env variables:

cat > /etc/profile.d/setgoenv.sh <<EOL
export GOPATH=/home/ubuntu/work
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
EOL

I hope that helped.

Upvotes: 0

leixiao
leixiao

Reputation: 1

You don't need sudo. If the "Agreeing to the Xcode/iOS license requires admin privileges" stops you, just re-open Xcode and accept the license agreement of Xcode.

Upvotes: 0

Dean Elbaz
Dean Elbaz

Reputation: 2450

You just need to drop the sudo. Your environment variables are defined at the user level. if you do sudo go env you'll see that GOPATH is not set there

Upvotes: 3

warvariuc
warvariuc

Reputation: 59604

sudo go get github.com/go-sql-driver/mysql

This runs go get under root user, which does not have $GOPATH set.

Just do:

go get github.com/go-sql-driver/mysql

Generally, do:

go get

in the project folder, and it will install all dependencies. The following will install dependencies mentioned in tests:

go get -t

Upvotes: 5

Related Questions