87

I want to switch between rails 2.3.10 as the "active" gem for my OS, so that I can invoke it at the command line.

Is it possible to do this? I'm not using rvm. Maybe it's time to start.

I tried gem install rails --version=2.3.10, but that just makes sure that version of the gem is installed, it doesn't put it in /usr/bin/rails.

(I do already use bundler for my apps -- but haven't needed any precise control over gems at the OS level until now)

John Bachir
  • 22,495
  • 29
  • 154
  • 227

4 Answers4

161

If your problem is to run binaries of a certain version, then:

rails --version # => the latest version
rails _2.3.10_ --version # => Rails 2.3.10

This pattern (gem-binary _gem-version_) works for any gem binary.

starball
  • 20,030
  • 7
  • 43
  • 238
Daniel Vartanov
  • 3,243
  • 1
  • 19
  • 26
  • 2
    That doesn't seem to work for all of them: `rackup _1.4.3_ --version`
    `Rack 1.1 (Release: 1.4)`
    – Chloe Jan 25 '13 at 23:44
  • 1
    rackup prints only release version (see `Rack.version` and `Rack.release` https://github.com/rack/rack/blob/master/lib/rack.rb). Try `gem install rack --version=1.3 && rackup _1.3.0_ --version`, it will print `Rack 1.1 (Release: 1.3)` – Daniel Vartanov Jan 26 '13 at 03:56
  • And just in case, here is how it works: `which rackup | xargs cat` – Daniel Vartanov Jan 26 '13 at 03:56
13

Use RVM

RVM allows you to manage different versions of Ruby and Gems. You can install a version of ruby using, for example

rvm install 1.9.2

You can then use it using:

rvm use 1.9.2

Use specific gems on a per project basis with gemsets.

If you want further namespacing you can set up gemsets; directories which will contain specific gems for a specific project.

rvm gemset create myproject

then you can use them like so:

rvm use 1.9.2@myproject

Automation

To automate the process of switching gems, pop .ruby-version and .ruby-gemset files in your project root. Pop the version of Ruby and name of the gemset you want to use inside them and RVM wil select the correct gemset when you cd into your project directory.

Installing gems into your gemset

Install your gems into your gemset in the usual way using bundler if you are using it:

bundle install

or just using the regular old:

gem install mygem

The gems will go in the right gemset.

RVM Alternatives

You might also want to check out rbenv, which does similar job.

superluminary
  • 47,086
  • 25
  • 151
  • 148
  • 1
    problem with rvm is (as you mentioned) that it handles ruby versions and gem versions. rbenv just handles ruby versions and lets bundler handle the gem versions. – daslicious Sep 22 '15 at 21:13
7

You can use RVM

Then you can also use Bundler afterwards, which manages gem dependencies fine.

In your Gemfile

gem "rails", "2.3.10"

and in your application

require 'rubygems'
require 'bundler/setup'

and you're done.

Chubas
  • 17,823
  • 4
  • 48
  • 48
  • In addition to this, if you want some scripts to only "see" the versions declared in your Gemfile, run them as "bundle exec ", e.g. `bundle exec warble` – rustyx Jan 05 '12 at 12:15
3

EDIT: Just saw your RVM mention in the post. Definitely the way to go.

You're going to want to install RVM -- it's an amazing package that will let you manage different Rubys and different sets of gems on the same machine. You can switch back and forth with total ease.

Here's the installation guide: http://rvm.beginrescueend.com/rvm/install/

Once you got everything get up, you can see all of your installed rubys at the command line with with rvm list, and switch with rvm use ruby-head, for example. RVM keeps the gems on each ruby separate, which should help with your question.

Sam Ritchie
  • 10,988
  • 4
  • 42
  • 53