如何在 Ubuntu 上设置 GOPATH 环境变量? 我必须编辑什么文件?

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?

转载于:https://stackoverflow.com/questions/21001387/how-do-i-set-the-gopath-environment-variable-on-ubuntu-what-file-must-i-edit

csdnceshi55
~Onlooker Note: Go 1.8+ (Q2 2017) will set GOPATH for you: see my answer below.
接近 3 年之前 回复
weixin_41568110
七度&光 I recommend reading the answer to a more general question. I point out where to set GOPATH variable. stackoverflow.com/a/26962251/450989
接近 4 年之前 回复
csdnceshi53
Lotus@ Use GVM: github.com/moovweb/gvm
大约 4 年之前 回复
csdnceshi53
Lotus@ askubuntu.com/questions/4667/…
大约 4 年之前 回复
csdnceshi54
hurriedly% In addition to the answers on what to set it as, see this SO answer, which clarifies what the GOPATH is, and how it should be used in general
大约 4 年之前 回复
csdnceshi66
必承其重 | 欲带皇冠 What shell are you using?
接近 6 年之前 回复

21个回答

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
csdnceshi66
必承其重 | 欲带皇冠 Remember to $ source ~/.bashrc ;)
4 年多之前 回复
csdnceshi62
csdnceshi62 This script should help you github.com/buffonomics/goswitch
4 年多之前 回复
weixin_41568110
七度&光 Do not set GOROOT! dave.cheney.net/2013/06/14/you-dont-need-to-set-goroot-really
4 年多之前 回复
csdnceshi75
衫裤跑路 As stated in user2128535's answer, GOROOT should be set to /usr/lib/go
4 年多之前 回复
csdnceshi74
7*4 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).
4 年多之前 回复
weixin_41568131
10.24 The path was /usr/share/go on mine Ubuntu 14.04
接近 5 年之前 回复
weixin_41568174
from.. 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
接近 5 年之前 回复
csdnceshi68
local-host 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
大约 5 年之前 回复
csdnceshi68
local-host The path was /usr/share/go on mine Ubuntu 13.10.
大约 5 年之前 回复

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
weixin_41568110
七度&光 I read somewhere that setting GOPATH as the same as the installation directory is a bad idea. O.o
接近 2 年之前 回复

Ubuntu 14.04

export GOPATH=$HOME/go

Additionally you can add this string to file

$HOME/.bashrc
csdnceshi60
℡Wang Yan mkdir -p ~/go/bin export GOPATH=~/go export PATH=$PATH:$GOPATH/bin:
2 年多之前 回复
csdnceshi61
derek5. Your comment is confusing because it is referring to a now removed line of the original answer.
3 年多之前 回复
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.

weixin_41568134
MAO-EYE 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.
接近 6 年之前 回复

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
weixin_41568127
?yb? Helped me with JAVA_HOME thanks.
2 年多之前 回复

Shortest and current solution.

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

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

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.

weixin_41568110
七度&光 OK, I see. Thanks for your comment.
5 年多之前 回复
weixin_41568127
?yb? $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.
5 年多之前 回复
weixin_41568110
七度&光 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: golang.org/doc/install and golang.org/doc/code.html#GOPATH
5 年多之前 回复

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

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.

共21条数据 1 3 尾页
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!