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

ID : 10164

viewed : 23

Tags : linuxubuntugolinux

Top 5 Answer for linux - How do I SET the GOPATH environment variable on Ubuntu? What file must I edit?

vote vote

100

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 
vote vote

88

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.

vote vote

73

Ubuntu 14.04

export GOPATH=$HOME/go 

Additionally you can add this string to file

$HOME/.bashrc 
vote vote

68

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/

vote vote

55

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 

Top 3 video Explaining linux - How do I SET the GOPATH environment variable on Ubuntu? What file must I edit?

Related QUESTION?