356

I am trying to do a go get:

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

and it fails with the following error:

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

when I do a go env , a list of Go values is shown as below:

ubuntu@ip-xxx-x-xx-x:~$ go env
GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH=""
GORACE=""
GOROOT="/usr/lib/go"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread"
CGO_ENABLED="1"

clearly the GOPATH is not set, how and where do I set it?

I see many threads that mention this error but none that provide an answer to my question, which file needs to be edited to provide a value for this path?

wasmup
  • 14,541
  • 6
  • 42
  • 58
David Saintloth
  • 4,061
  • 2
  • 16
  • 16

30 Answers30

370

New Way:

Check out this answer.

Note: Not for trying out a go application / binaries on your host machine using go install [repo url], in such cases you still have to use the old way.

Old Way:

Just add the following lines to ~/.bashrc and this will persist. However, you can use other paths you like as GOPATH instead of $HOME/go in my sample.

export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
user158
  • 12,852
  • 7
  • 62
  • 94
Daniel Lin
  • 3,833
  • 1
  • 11
  • 4
  • 14
    The path was `/usr/share/go` on mine Ubuntu 13.10. – tsusanka Aug 13 '14 at 21:35
  • However using `/usr/share/go` somehow crippled `go build`: `runtime: exec: "/usr/share/go/pkg/tool/linux_amd64/6g": stat /usr/share/go/pkg/tool/linux_amd64/6g: no such file or directory` so i am not really sure what's the right setup – tsusanka Aug 14 '14 at 07:42
  • 44
    Do NOT use /usr/share/go. GOPATH is go's "workspace", where it downloads packages, et cetera. Create a new directory and use that. I recommend ~/.go – Bryan Larsen Nov 27 '14 at 15:58
  • 3
    The path was `/usr/share/go` on mine Ubuntu 14.04 – Alexandre Holden Daly Dec 24 '14 at 23:33
  • Just a simple reminder: In order for these changes to take effect, don't forget to `source ~/.bashrc` if you want to use these variables immediately in your current terminal window. Otherwise you'll need to start a new terminal (or otherwise trigger processing of your `~/.bashrc` file for example by logging out and in again). – L0j1k Feb 11 '15 at 17:25
  • 16
    As stated in user2128535's answer, GOROOT should be set to /usr/lib/go – trungly Feb 20 '15 at 23:07
  • 12
    Do not set GOROOT! http://dave.cheney.net/2013/06/14/you-dont-need-to-set-goroot-really – Nate Finch May 21 '15 at 13:24
  • 16
    Remember to `$ source ~/.bashrc` ;) – eversor Jul 08 '15 at 08:19
  • This only works if your shell is bash, you really should use `/etc/environment` so `chsh` doesn't break it. Restart is required. – Jonathan Mar 07 '19 at 09:26
  • for zsh the line should be added to `~/.zshrc` – DarthCucumber Oct 21 '21 at 05:54
  • New way is only for projects, if you're installing go application using `go install` you still have to use old way. – user158 Oct 04 '22 at 05:44
130

New Way: Go Modules

Since Go 1.11, you don't have to use GOPATH anymore. Simply go to your project directory and do this once:

go mod init github.com/youruser/yourrepo
  • With this, Go creates a module root at that directory.
  • You can create as many modules as you want.
  • For step by step instructions, also see this answer.

Old Way: GOPATH

If you insist on working with GOPATH then heed this:

  • Since Go 1.8, you don't need to set your GOPATH or GOROOT.
  • GOPATH by default is under your user/home directory.

From the documentation:

If no GOPATH is set, it is assumed to be $HOME/go on Unix systems and %USERPROFILE%\go on Windows. If you want to use a custom location as your workspace, you can set the GOPATH environment variable.

Inanc Gumus
  • 25,195
  • 9
  • 85
  • 101
68

Ubuntu 14.04

export GOPATH=$HOME/go

Additionally you can add this string to file

$HOME/.bashrc
Neil
  • 24,551
  • 15
  • 60
  • 81
pasha.zhukov
  • 1,272
  • 10
  • 9
65

GOPATH should be set to a newly created empty directory. This is the go "workspace", where it downloads packages, et cetera. I use ~/.go.

For example:

mkdir ~/.go
echo "GOPATH=$HOME/.go" >> ~/.bashrc
echo "export GOPATH" >> ~/.bashrc
echo "PATH=\$PATH:\$GOPATH/bin # Add GOPATH/bin to PATH for scripting" >> ~/.bashrc
source ~/.bashrc

source: http://www.larry-price.com/blog/2013/12/15/setting-up-a-go-environment-in-ubuntu-12-dot-04/

Bryan Larsen
  • 9,468
  • 8
  • 56
  • 46
45

If for example, it is an Ubuntu, after installing the packages:

$sudo apt install golang -y

Just add the following lines to ~/.bashrc (Of your user)

export GOROOT=/usr/lib/go
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
firebitsbr
  • 770
  • 6
  • 10
  • thanks, i just uninstalled my previous installation (not used apt that time) and did what u have provided. works all good thanks :> – 1UC1F3R616 Jan 17 '21 at 19:58
20
export GOPATH=/path/desired/here

There is no need to edit any file, we can just use the command above to directly edit the Go environment variables.

k0pernikus
  • 60,309
  • 67
  • 216
  • 347
David Saintloth
  • 4,061
  • 2
  • 16
  • 16
  • 12
    This isn't persistent though. If you want this option to stay - you likely do - you'll have to add it to some file that's being loaded on login. – Cubic Jan 08 '14 at 18:03
  • 1
    Thanks! What most users don't ever think is that there are people starting with linux and if there is no documentation saying what you said(Because everybody thinks it's a known fact) how would we learn/find the information? – PlayHardGoPro Feb 21 '20 at 12:14
20

Write this code in Terminal.

export GOPATH=path/to/your/gopath/directory

Note: This will reset on every new Terminal window or system restart.

To be persistent, paste the code below in your .zshrc or .bashrc file according to your shell. Those files in your Home Directory. It will be like below.

export PATH=path/to/some/other/place/composer/for/example
export GOPATH=path/to/your/gopath/directory
export PATH=$PATH:$GOPATH/bin
Inanc Gumus
  • 25,195
  • 9
  • 85
  • 101
atilkan
  • 4,527
  • 1
  • 30
  • 35
13

For Go 1.13+:

go env -w GOPATH=$HOME/go

To set the GOPATH regardless of the GO version add the following line to your ~/.bashrc:

export GOPATH=$HOME/go

and don't forget to source your .bashrc file:

source ~/.bashrc

More options on the golang official wiki: https://github.com/golang/go/wiki/SettingGOPATH

Mustapha-Belkacim
  • 1,653
  • 1
  • 15
  • 19
12

You will need GOPATH later, too. So add it to ~/.bashrc.

  • 10
    Yes, I had this in mind, but dismissed the possibility that a guy would have an Ubuntu box with a custom shell and not know it as highly unlikely :-) – Vytautas Šaltenis Jan 09 '14 at 10:51
8

If you've setup anything that needs modification of environment variables e.g. Java, Go etc this will be very familiar.

I will assume that you have the following directory structure somewhere as your Go path:

\---[folder name]
    +---bin
    +---pkg
    \---src
  • open a new terminal
  • type sudo nano /etc/environment
  • find PATH=... and go the end of it and add a colon : after the last path then paste in your full go path e.g. /home/user/gocode

and you're done, This should make it persistent through the system.

ymg
  • 1,500
  • 2
  • 22
  • 39
  • 1
    I don't see why change $PATH if go is working for David. He needs to set $GOPATH (see the answer from Daniel Lin). David may merge both approaches (or set it in /etc/profile or $HOME/.profile). By the way, I recommend David to read these links: http://golang.org/doc/install and http://golang.org/doc/code.html#GOPATH – zk82 Jan 16 '14 at 23:34
  • 2
    @zk82 $GOPATH is an environment variable, and I don't see why editing your environment variables to add this variable to it a problem. Same goes for $JAVA_HOME and other frameworks that require such configuration. – ymg Jan 17 '14 at 01:38
4
export GOROOT=/usr/lib/go
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin

And you may want to check by $ go env

mei
  • 49
  • 1
4

Just add the following lines to ~/.bashrc

export GOROOT=/usr/lib/go
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
Ashwin Patil
  • 1,307
  • 1
  • 23
  • 27
  • 1
    I'm using mac pro 16 (Intel) Big Sur with golang version 1.17.6, worked fine for me, updated in `.bashrc` and `.bash_profile` then `source .bashrc` – ArifMustafa Jan 23 '22 at 13:56
3

This is what got it working for me on Ubuntu 15.10 using the fish shell:

# ~/.config/fish/config.fish

set -g -x PATH /usr/local/bin $PATH
set -g -x GOPATH /usr/share/go

Then I had to change the permissions on the go folder (it was set to root)

sudo chown <name>:<name> -R /usr/share/go
Tom
  • 1,986
  • 2
  • 18
  • 29
3

go path could be every where you want just create a directory and set global path variable in the name of GOPATH to your environment.

mkdir ~/go
export GOPATH=~/go
go get github.com/go-sql-driver/mysql
Jeyem
  • 274
  • 1
  • 6
3

GOPATH is an environment variable to your work-space location. GOROOT is an environment variable to your installation directory. Although GOROOT and GOPATH is automatically set (if there would not be a bug) during the installation time, to specify it manually you can follow below process. Moreover, for more information you can refer to GO's wiki page.

It is better to set GOPATH to a directory inside your home directory, e.g., $HOME/go, %USERPROFILE%\go (Windows).

  1. This is a solution mac, which is tested on macOS Sierra, ver. 10.12, and also in Gogland-EAP, which has been introduced as an IDE for Go programming by JetBrains.

On your Terminal type

vim ~/.profile

in opened document on the Terminal press i and add the following path

GOPATH=/path/to/a/directory/inside/home/directory
GOROOT=/path/to/you/go/library
PATH=$PATH:$GOPATH:$GOROOT:$GOROOT/bin

press ESC and type :x. Lastly, you should restart (close and open) your terminal or Logout and Login again.

  1. For Windows and Linux configuration, please refer to Go wiki page at Githab on Setting GOPATH-Golang.

CAUTION Do not set both GOROOT and GOPATH to the same directory, otherwise you will get a warning.

Esmaeil MIRZAEE
  • 1,110
  • 11
  • 14
3

(Ubuntu)

If you don’t set a GOPATH, the default will be used.

You have to add $GOPATH/bin to your PATH to execute any binary installed in $GOPATH/bin, or you need to type $GOPATH/bin/the-command. Add this to your ~/.bash_profile

export PATH=$GOPATH/bin:$PATH

Current GOPATH command:

go env GOPATH

Changing the GOPATH command:

export GOPATH=$HOME/your-desired-path

Shubham Jaiswal
  • 570
  • 5
  • 11
2

With Go 1.8 (Q2 2017), you won't have to edit any file if you accept the default GOPATH value (set for you)

$HOME/go

You can see the comments on issue 17262 and the associated twitter vote:

By choosing a default GOPATH, we can make our documentation easier because we can say things like

$ go get github.com/foo/bar

will check out the github.com/foo/bar repo into $HOME/go/src/github.com/foo/bar.

We don't need to distract newcomers with having to set an env var, all we need to do is put a little parenthetical note at the bottom of the page

$HOME/go is the default path to your go workspace.
If you want to change this path, set the GOPATH variable to something of your choosing.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
2

My go environment looked similar to yours.

$go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH=""
GORACE=""
GOROOT="/usr/lib/go-1.6"
GOTOOLDIR="/usr/lib/go-1.6/pkg/tool/linux_amd64"
GO15VENDOREXPERIMENT="1"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"

I resolved it with setting GOPATH to /usr/lib/go. Try it out.

export GOPATH=/usr/lib/go
export PATH=$PATH:$GOPATH/bin
NSP
  • 1,193
  • 4
  • 15
  • 26
1

At the end of the ~.profile file add::

export GOPATH="$HOME/go"
export PATH="$PATH:/usr/local/go/bin"
export PATH="$PATH:$GOPATH/bin"
1

if you are using zsh :

nano ~/.zshrc

then add to the end of the file :

export PATH=$PATH:/usr/local/go/bin

finally :

source ~/.zshrc

and open a new terminal

go version
1
export PATH=$PATH:$(go env GOPATH)/bin
Swetank
  • 111
  • 2
  • 7
1

For Ubuntu: Add the following path in .bashrc file and save the file

export PATH=$PATH:/usr/local/go/bin:$HOME/go/bin
0

This has been the most annoying thing to deal with. In hopes of saving your time.

IF go was installed as root user. The root user of your system's bash_profile text file ~/.bash_profile needs to have $GOROOT assigned to the go install directory and $GOPATH needs to be assigned to go /src directory.

  ...$# sudo su
  ...$# vi ~/.bash_profile

    ***Story continues in vi editor***

    GOROOT=$GOROOT:/usr/local/go
    GOPATH=$GOPATH:/usr/local/go/src
    ...
    [your regular PATH stuff here]
    ...

be sure the path to go binary is in your path on .bash_profile

PATH=$PATH:$HOME/bin:/usr/local/bin:/usr/local/go/bin

This PATH can be as long a string it needs to be..to add new items just separate by colon :

exit vi editor and saving bash profile (:wq stands for write and quit)

  [esc] 
  [shift] + [:] 
  :wq

You have to log out of terminal and log back in for profile to initiate again..or you can just kick start it by using export.

...$# export GOPATH=/usr/local/go/src

You can verify go env:

...$# go env

Yay!

GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/usr/local/go/src"
GORACE=""
GOROOT="/usr/local/go"

Now you can sudo and go will be able to download and create directories inside go/src and you can get down to what you were trying to get done.

example

# sudo go get github.com/..

Now you will run into another problem..you might not have git installed..that's another adventure..:)

  • **Don't do this.** You do **not** want to compile things as root and you do **not** want `go get` to put anything anywhere near `GOROOT`. – Dave C Jun 25 '15 at 14:06
  • How does one run go under a normal user? Thanks so much. Things weren't working out during install process and I ended up using a test virtual machine to try out GO. – user3151532 Jun 27 '15 at 03:09
0

On my Fedora 20 machine I added the following lines to my ~/.bashrc:

export GOROOT=/usr/lib/golang
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
Ahsan
  • 3,845
  • 2
  • 36
  • 36
  • 3
    [Don't set `GOROOT`](http://dave.cheney.net/2013/06/14/you-dont-need-to-set-goroot-really). (Unless you're in the extremely small subset of people that uses a binary distribution of Go but install it somewhere where non-standard.) – Dave C Jun 25 '15 at 14:10
0

Yet another solution: Export every GO* environment variable from go env

.bashrc:

eval $(go env | grep '^GO[A-Z0-9_]*=' | while read setenv; do
  echo "export $setenv; "
done 2> /dev/null)

[[ -n $GOPATH ]] || export GOPATH="$HOME/go/bin"
[[ -n $GOROOT ]] || export GOROOT=/usr/bin/go
export PATH="$PATH:$GOPATH/bin:$GOROOT/bin"
DMaster
  • 603
  • 1
  • 10
  • 23
0

Edit your ~/.bash_profile to add the following line:

$ export GOPATH=$HOME/work

Save and exit your editor. Then, source your ~/.bash_profile

$ source ~/.bash_profile

Note: Set the GOBIN path to generate a binary file when go install is run

$ export GOBIN=$HOME/work/bin
varun7447
  • 574
  • 1
  • 6
  • 27
Jackal
  • 2,591
  • 3
  • 25
  • 29
-1

This script will help you switch Gopaths. https://github.com/buffonomics/goswitch

Glstunna
  • 1,993
  • 3
  • 18
  • 27
-1

You can use the "export" solution just like what other guys have suggested. I'd like to provide you with another solution for permanent convenience: you can use any path as GOPATH when running Go commands.

Firstly, you need to download a small tool named gost : https://github.com/byte16/gost/releases . If you use ubuntu, you can download the linux version(https://github.com/byte16/gost/releases/download/v0.1.0/gost_linux_amd64.tar.gz).

Then you need to run the commands below to unpack it :

$ cd /path/to/your/download/directory 
$ tar -xvf gost_linux_amd64.tar.gz

You would get an executable gost. You can move it to /usr/local/bin for convenient use:

$ sudo mv gost /usr/local/bin

Run the command below to add the path you want to use as GOPATH into the pathspace gost maintains. It is required to give the path a name which you would use later.

$ gost add foo /home/foobar/bar     # 'foo' is the name and '/home/foobar/bar' is the path

Run any Go command you want in the format:

gost goCommand [-p {pathName}] -- [goFlags...] [goArgs...]

For example, you want to run go get github.com/go-sql-driver/mysql with /home/foobar/bar as the GOPATH, just do it as below:

$ gost get -p foo -- github.com/go-sql-driver/mysql  # 'foo' is the name you give to the path above.

It would help you to set the GOPATH and run the command. But remember that you have added the path into gost's pathspace. If you are under any level of subdirectories of /home/foobar/bar, you can even just run the command below which would do the same thing for short :

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

gost is a Simple Tool of Go which can help you to manage GOPATHs and run Go commands. For more details about how to use it to run other Go commands, you can just run gost help goCmdName. For example you want to know more about install, just type words below in:

$ gost help install

You can also find more details in the README of the project: https://github.com/byte16/gost/blob/master/README.md

Franco
  • 41
  • 1
  • 1
  • 5
-1

You have to update the PATH based on the terminal(bash or zsh) which you use.

  1. Open the shell script file of the terminal i.e ~/.bashrc or ~/.zshrc in an editor
   vi ~/.zshrc
      (or)
   code ~/.zshrc
  1. Update the below GOPATH if already found or add the below line.

export GOPATH=$HOME/go:/$HOME/projects/go

Here you can add one or more paths separated by a semicolon : from different locations of your GO projects on the system to the GOPATH environment variable i.e /path/1:path/2:path/3 etc.

In my case, I have added 2 paths, as shown above, one from the root $HOME/go and the other one from the projects directory :/$HOME/projects/go

Sudharshan
  • 3,634
  • 2
  • 27
  • 27
-2

As written in the official instructions:

The GOPATH environment variable specifies the location of your workspace. It defaults to a directory named go inside your home directory, so $HOME/go on Unix, $home/go on Plan 9, and %USERPROFILE%\go (usually C:\Users\YourName\go) on Windows. If you would like to work in a different location, you will need to set GOPATH to the path to that directory. (Another common setup is to set GOPATH=$HOME.) Note that GOPATH must not be the same path as your Go installation.

So for example, if you are coding in Jetbrains Webstorm (using the Go plugin), you might want to set GOPATH as /Users/<user>/WebstormProjects.

In simpler words, set it to wherever you want your Go projects to reside.

Abdul Wasae
  • 3,614
  • 4
  • 34
  • 56