Switching gem libraries using update-alternatives

Blog ยป Switching gem libraries using update-alternatives

Posted on 15 Dec 2010 13:13

Maintaining more than one rails and gems library at your development machine using update-alternatives is possible. But note that it's better done with RVM nowadays… You can still ready this article to learn more about update-alternatives. UPDATE: Using Hobo Edge On Rails 3 describes installing gems for both Rails 2 and Rails 3 in addition to installing Hobo on Rails3 using rvm. It also solves the problem here better. Go and do it: exit(0).


I wanted to switch the whole bunch of gems while I am developing in Rails 2 or Rails 3. I want to keep both sets intact and I want to be able to switch instantly. I prefer to use different sets for Rails 2 and 3 instead of trying to do everything with one installation. update-alternatives solves the problem easily.

Caution The code below assumes that your gems are in /usr/lib/ruby/gems … However on some systems, the gems are in /var/lib/gems … Please check it first and change the code accordingly.

Just do sth like this to create your alternatives:

cd /usr/lib/ruby/
sudo cp -a gems gems.rails2.x
sudo cp -a gems gems.rails3.x
sudo mv gems gems.bak
sudo update-alternatives --install /usr/lib/ruby/gems gems /usr/lib/ruby/gems.rails2.x 50
sudo update-alternatives --install /usr/lib/ruby/gems gems /usr/lib/ruby/gems.rails3.x 50

You can check what gems alternatives you have anytime:

update-alternatives --display gems

You can switch the current alternative:

sudo update-alternatives --config gems

Then you'll see such an alternative:

Selection    Path                         Priority   Status
------------------------------------------------------------
* 0            /usr/lib/ruby/gems.rails2.x   50        auto mode
  1            /usr/lib/ruby/gems.rails2.x   50        manual mode
  2            /usr/lib/ruby/gems.rails3.x   50        manual mode

What you say? Both directories are the same? Now switch your alternative and install things accordingly…

You can continue to create alternatives as you like… I would do that before upgrading all gems for example. You can keep edge and stable collections…

I suggest to append "update-alternatives —query gems" to your ~/bashrc so that you know where you are when starting your day.

If you like this page, please spread the word: diggdel.icio.usFacebook

You can contact me if you have questions or corrections.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License