AndrewF
AndrewF

Reputation: 6992

Using vagrant and chef-solo, how can I specify which version of ruby to run?

I'm using chef-solo to provision a new vagrant VM. It's Ubuntu 12.10. Chef runs fine the first time, but fails to launch anytime thereafter.

The problem is that chef-solo needs to run using ruby 1.9.3, but one of my cookbooks installs ruby 2.0.0. It links the new version of ruby to /usr/local/ruby, so the system version is still available at /usr/bin/ruby.

The shebang line in /usr/bin/chef-solo is #!/usr/bin/env ruby.

Is there a way I can set an environment variable in the Vagrantfile before chef-solo runs? Or is there another way to force vagrant to run chef-solo using /usr/bin/ruby?

UPDATE: I'm currently using the following command on the VM as a work-around:

sudo /usr/bin/ruby /usr/bin/chef-solo --config /tmp/vagrant-chef-1/solo.rb --override-runlist "my_runlist"

I'd like to force Vagrant to run something similar when I type vagrant provision on the host.

Upvotes: 1

Views: 1375

Answers (2)

tnarik
tnarik

Reputation: 159

Using the vagrant omnibus plugin you can install Chef Omnibus which has its own embedded Ruby, to guarantee that Chef runs always use the same Ruby version.

The only issue I found with this is that if your recipes rely on the system version of ruby some setups won't work correctly (for instance, a ruby installed in /usr/local/bin won't be use if you shell_out! in a cookbook, but an intallation in /usr/bin will).

Upvotes: 1

gabrtv
gabrtv

Reputation: 3588

/usr/bin/env works by using $PATH to find the executable. You should make sure that /usr/bin/ruby is at the front of your path. One way to ensure this is:

export PATH=/usr/bin:$PATH

..and then try your chef-solo run.

Here's more from: http://en.wikipedia.org/wiki/Shebang_(Unix)#Portability

Often, the program /usr/bin/env can be used to circumvent this limitation by introducing a level of indirection. #! is followed by /usr/bin/env, followed by the desired command without full path, as in this example:

!/usr/bin/env sh

This mostly works because the path /usr/bin/env is commonly used for the env utility, and it invokes the first sh found in the user's $PATH, typically /bin/sh, if the user's path is correctly configured.

Upvotes: 1

Related Questions