dub stylee dub stylee - 7 months ago 29
Ruby Question

How to install rvm/ruby/rails via chef?

First, I would like to point out that I have read the question located here: Installing RVM/Ruby 1.9.3 via Chef

I am trying to set up a Ruby on Rails environment in Windows using VirtualBox/Vagrant. The installation of VirtualBox and Vagrant are pretty self-explanatory, but delving into the configuration of Vagrant and things like Chef become less clear.

I am following instructions from a blog here: http://manuelvanrijn.nl/blog/2013/07/23/developing-ruby-on-rails-on-windows/

Which include installation of the librarian-chef gem to manage cookbooks, and using the following Cheffile:

#!/usr/bin/env ruby
#^syntax detection

site 'http://community.opscode.com/api/v1'

cookbook 'apt'
cookbook 'git'
cookbook 'sqlite'
cookbook 'mysql'
cookbook 'postgresql'
cookbook 'database', :git => 'git://github.com/manuelvanrijn/cookbook-database.git', :ref => 'grant-roles'
cookbook 'nodejs'
cookbook 'build-essential'
cookbook 'ruby_build'
cookbook 'rbenv', :git => 'git://github.com/fnichol/chef-rbenv.git', :ref => 'v0.7.2'


My problem is that launching my Vagrant box initially, and the base box (precise64) that I am using comes with ruby 1.8.7p358.

In order to get a functioning Rails site, I needed to install rvm, install a newer version of Ruby, then install Rails before I was able to run
rails new rails_site


I have found a cookbook for rvm here: https://github.com/fnichol/chef-rvm

So what I am hoping to accomplish is, add the rvm cookbook, automatically install a specific version of Ruby, and then install Rails, so that I can have a functional development environment out of the box. I know that I can add the cookbook by adding a line at the end of my Cheffile, but beyond that, how do I also instruct rvm to install a specific version of Ruby, then after that, to install Rails as well?

Answer

To install rvm, I have used cookbook you specified. What you need is to add it to Cheffile:

cookbook 'rvm', :git => 'git://github.com/fnichol/chef-rvm.git', :ref => '24ecbb0'

I have used ref, because last version wasn't working properly(don't know if it was fixed, but you can try my way, then switch and tryout newer). Next step is to add rvm to the role you are using for your node:

'recipe[rvm::user]',

after you specified this role in nodefile(in your tutorial it is Vagrantfile), you can configure rvm installation like this:

  'rvm' => {
    'installer_url' => 'https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer',
    'branch' => 'none',
    'version' => '1.17.10',
    'user_installs' => [{        
      'user' => 'someuser',
      'default_ruby' => 'ruby-1.9.3-p286@mygemsetname'
    }]
  }

thats it. Rvm should be installed for someuser with ruby-1.9.3-p286@mygemsetname gemset. To install Rails in specific dir and other custom actions you will need to learn how to write own cookbooks, it is very easy, you will need to log-in as someuser, cd to dir you need and execute gem install, then rails new(of course if you want to install rvm/rails as a user, not systemwide)

UPD: To install custom gems you can use chef-rvm or your own cookbook:

./site_cookbooks/mycookbook/recipes/default.rb

execute 'install mysql2 gem' do
  command 'gem install mysql2'
  not_if 'gem list | grep mysql2'
end