There are similar questions about how to install golang with vagrant provision or how to set $GOPATH
environment variable, such that it will be available on boot.
However, this question is about how you can set $GOPATH
, etc, such that the variable is available during later steps of the provisioning script (and everything else important about golang is in-place for that matter). I'm using an ubuntu image.
Specifically, I want to run these steps in my_provision.sh
:
go get github.com/saymedia/terraform-s3-dir
go install github.com/saymedia/terraform-s3-dir
such that when I run the following command after vagrant ssh
, it will just work:
terraform-s3-dir
When the go get/install commands run in my_provision.sh
, it outputs one of two things: silence, or complaints that the GOPATH
var is not set. Either way, when I boot in, terraform-s3-dir
is not a recognised command.
I've tried setting the GOPATH
, GOROOT
and PATH
variables in several different ways:
Setting them in my_provision.sh
directly (with and without export
), echoing export commands into .bashrc. echoing exporting commands to /etc/profile.d/gopath.sh. These all results in "$GOPATH not set".
running exports in an inline provisioning script, prior to my_provision.sh
. This resulted in silent failure, unless I try to run the go get/install commands with sudo, in which case it has the $GOPATH
error, too.
If I echo the GOPATH variable in my_provision.sh
immediately before the go get/install commands, the variables show that they are set as expected.
If I run the go get/install commands manually from the shell after vagrant ssh
, then it installs successfully.
What is different about the vagrant ssh
context, vs the provisioning script context, where the go get/install commands get the desired result in the former, but not the latter?