How do I SET the GOPATH environment variable on Ubuntu? What file must I edit?

Solution 1:

New Way:

Check out this answer.

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

Solution 2:

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.

Solution 3:

Ubuntu 14.04

export GOPATH=$HOME/go

Additionally you can add this string to file

$HOME/.bashrc