read

If you are using Chef to manage your server deployments (and you should be!), then you'll likely have run into the situation where you install a new system Ruby and need to install a few system-wide gems to bootstrap your install (think Bundler/Rake).

However, when you install the system ruby, and use the standard Chef gem install resource:

gem_package "bundler"

then you will run into trouble as Chef will try to use the original system ruby binary when installing the gem for you.

The Fix

After some Googling, and time on StackOverflow, I've put together a workable solution. All it does it reload the Ohai resource (updating information about your system/Ruby binary), and performs the gem installation in a ruby_block so that it does not run at compile-time.


#=========================
# Install system Ruby
#=========================

## install ruby here....

#=========================
# Install gems in new ruby
#=========================

# Need to reload OHAI to ensure the newest ruby is loaded up
ohai "reload" do
  action :reload
end

["bundler", "rake", "...."].each do |gem_to_install|
  ruby_block "install #{gem_to_install} in new ruby" do
    block do
      g = Chef::Resource::GemPackage.new(gem_to_install)
      g.gem_binary "#{node['languages']['ruby']['bin_dir']}/gem"
      g.run_action(:install)
    end
    action :create
  end
end

That ought to do it! Hope it saves someone some time....

Blog Logo

Brandon Parsons


Published

Image

Brandon Parsons

My Personal Blog

Head back to the main page