Article

Dead Simple Rails Dependency Management with install_gems

Posted  by Blake Watters.

PublicCategorized as Software Development.

Tagged with capistrano, rails and ruby.
For the last six months or so I’ve been using a script called install_gems to manage the Gem configuration for my production servers and development environments. The setup is very simple:
  1. Grant your deployment user rights to run gem via sudo. This can be accomplished by adding the following to /etc/sudoers on Unix platforms:

    deploymentuser ALL=(ALL) NOPASSWD: /usr/bin/gem
  2. Create two Gem configuration files under the config directory of your Rails root. These files should be named gems.yml and developer_gems.yml. The gems.yml file contains a YAML formatted Array of Arrays corresponding to a particular Gem and a GemConfigure compatible versioning string. Here is an example RAILS_ROOT/config/gems.yml file:

    [‘rails’, ’= 1.1.6’]
    [‘facets’, ‘>= 1.7.46’]
    [‘mysql’, ‘>= 2.7’]
    [‘rmagick’, ‘>= 1.14.1’]
    [‘chronic’, ‘>= 0.1.5’]

    The developer_gems.yml file contains a similarly formatted list of Gems that are only required for developers of the app. This may include mocking libraries, deployment tools, etc. Here’s a sample RAILS_ROT/config/developer_gems.yml file:

    [‘production_log_analyzer’, ‘>= 1.3.0’]
    [‘action_profiler’, ‘>= 1.0.0’]
    [‘sql_dep_graph’, ‘>= 1.0.0’]
    [‘capistrano’, ‘>= 1.2.0’]
    [‘flexmock’, ‘>= 0.4.3’]
  3. Next, download install_gems (328 Bytes) and place it in your RAILS_ROOT/script/ directory and make it executable.
  4. Then add the following lines to your environment.rb file:
  5. Load our dependent gems
    require ‘gemconfigure’
    gem_config = open("#{RAILS_ROOT}/config/gems.yml") { |f| YAML.load(f.read) }
    Gem.configure(gem_config)
  6. Finally, configure Capistrano to run install_gems when deployments occur. Here’s an example task:

    task :after_update_code, :roles => :app do run "#{release_path}/script/install_gems
    end

Congratulations and welcome to dependency management nirvana! You can now upgrade your local Gems by running script/install_gems (use the -d switch to load developer Gems as well). When you change the YAML file, your servers will be automagically brought up to date on the next deployment. All dependent libraries will be automatically loaded when your Rails processes start. Pretty slick, eh?

I’ve been meaning to bundle this up into a Rails plugin and will do so when the mood strikes me or there’s enough popular demand.

Enjoy!


Arrow_down Hide comments

Powered by Near-TimeTerms of Services | Privacy Policy | Security Policy |