Where does it get GOROOT if not from the environment?

3,624 views
Skip to first unread message

David de Kloet

unread,
Apr 27, 2014, 4:58:29 PM4/27/14
to golang-nuts
I'm getting this error

$ go version
go: cannot find GOROOT directory: /usr/local/go

but

$ echo $GOROOT
/home/dskloet/go

so why does it think that GOROOT is /usr/local/go ?

I don't know if it's relevant but I had installed golang with Ubuntu Software Center (I believe that's the same as apt-get install) but that installed a very old version so I uninstalled it and just downloaded it from golang.org into my home dir.

Caleb Spare

unread,
Apr 27, 2014, 5:02:45 PM4/27/14
to David de Kloet, golang-nuts
The default is hard-coded at compile time; in the Linux distribution
it is set to /usr/local/go. You can see where GOROOT for your
installation is by using 'go env GOROOT'.

The directions for installation of the package you downloaded are here:

http://golang.org/doc/install

and they cover the installation location. You should install to
/usr/local/go rather than in your home dir. Alternatively, if you
really want to install to a non-standard location, that's also covered
on that page under "Installing to a custom location".

-Caleb
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

David de Kloet

unread,
Apr 27, 2014, 5:08:11 PM4/27/14
to Caleb Spare, golang-nuts
Thanks!

minux

unread,
Apr 27, 2014, 5:10:24 PM4/27/14
to David de Kloet, golang-nuts
On Sun, Apr 27, 2014 at 4:58 PM, David de Kloet <dsk...@gmail.com> wrote:
I'm getting this error

$ go version
go: cannot find GOROOT directory: /usr/local/go

but

$ echo $GOROOT
/home/dskloet/go
it seems you forget to export GOROOT.
export GOROOT=/home/dskloet/go

so why does it think that GOROOT is /usr/local/go ?
it's hard-coded in $GOROOT/src/pkg/runtime/zversion.go.

if you don't want to set GOROOT, you can modify that file, and use
GOROOT=/home/dskloet/go go install -a -v std
to re-install all packages.

David de Kloet

unread,
Apr 27, 2014, 5:12:54 PM4/27/14
to minux, golang-nuts
I set GOROOT from my .bash_profile but since it's hard-coded it was irrelevant.
I installed it into /usr/local/go following the instructions from Caleb so everything is fine now.
Thanks.

minux

unread,
Apr 27, 2014, 5:37:58 PM4/27/14
to David de Kloet, golang-nuts
On Sun, Apr 27, 2014 at 5:12 PM, David de Kloet <dsk...@gmail.com> wrote:
I set GOROOT from my .bash_profile but since it's hard-coded it was irrelevant.
it has hardcoded *default* value, but you can always override it with environment variable.
the problem of your previous setup is that you forget to export the variable so the
go command can't read the $GOROOT you set.
you need to export it in your .bash_profile:
export GOROOT=.... 

David de Kloet

unread,
Apr 27, 2014, 5:53:46 PM4/27/14
to minux, golang-nuts
I have

GOROOT=/home/dskloet/go

in my .bash_profile. As far as I know that accomplishes the same.

$ set | grep GOROOT
GOROOT=/home/dskloet/go

The strange thing is, even if I rename /usr/local/go now, it still works.
I wonder what sudo tar -C /usr/local -xzf go1.2.1.linux-amd64.tar.gz could have fixed, that isn't undone by renaming /usr/local/go to something else.

minux

unread,
Apr 27, 2014, 6:08:36 PM4/27/14
to David de Kloet, golang-nuts
On Sun, Apr 27, 2014 at 5:53 PM, David de Kloet <dsk...@gmail.com> wrote:
I have

GOROOT=/home/dskloet/go

in my .bash_profile. As far as I know that accomplishes the same.
it's not the same.

$ set | grep GOROOT
GOROOT=/home/dskloet/go
set will show all environment in this process (bash), but `env | grep GOROOT` will show
all environment variables exported to child processes.

without export, an environment variable won't be passed to child processes, thus even
you have GOROOT set in your .bash_profile, go command simply won't pick it up.

David de Kloet

unread,
Apr 27, 2014, 6:13:50 PM4/27/14
to minux, golang-nuts
On Mon, Apr 28, 2014 at 12:08 AM, minux <minu...@gmail.com> wrote:

On Sun, Apr 27, 2014 at 5:53 PM, David de Kloet <dsk...@gmail.com> wrote:
I have

GOROOT=/home/dskloet/go

in my .bash_profile. As far as I know that accomplishes the same.
it's not the same.

$ set | grep GOROOT
GOROOT=/home/dskloet/go
set will show all environment in this process (bash), but `env | grep GOROOT` will show
all environment variables exported to child processes.

without export, an environment variable won't be passed to child processes, thus even
you have GOROOT set in your .bash_profile, go command simply won't pick it up.

Well, it apparently it does.

 $ env | grep GOROOT
GOROOT=/home/dskloet/go
$ go env GOROOT
/home/dskloet/go

minux

unread,
Apr 27, 2014, 6:33:52 PM4/27/14
to David de Kloet, golang-nuts
then you've it exported somewhere.
if the go env GOROOT output is correct, nothing could explain your original post.
Reply all
Reply to author
Forward
0 new messages