Using Multiple Versions Of Rails

It’s quite easy to have multiple versions of rails installed as gems when working with ruby on rails. Quite frequently, you will be working on a project that uses an older version of rails than one on your machine, and all you have to do to get the correct version is:

gem install -v 1.2.3 rails --include-dependencies

Multiple versions of rails can exist independently on your machine without much of a problem. Since your individual rails projects store the version number they expect, and your projects don’t tend to depend on each other, you can have hundreds of different rails versions installed and never notice. Mostly.

If, however, you wish to start a NEW project, when you run

rails projectname

It will use the most recent version of rails. But what if, for some reason, you have rails 2.0.1 and rails 1.2.6 on your machine, and wish to generate a rails 1.2.6 application? This came up because I’m trying to teach a friend rails, and the book he has is for rails 1.2, meaning that if he uses 2.0 all of the scaffolding code (as well as other bits) from the book are incorrect. At the same time, the application he and I are working on together uses rails 2.0, so he needs both versions on his machine.

This solution works in linux (haven’t tried windows):

rails _1.2.6_ projectname

I didn’t see any documentation about this anywhere. In fact, I found it by cracking open the actual ‘rails’ script in /usr/bin and noticing that it did a regex match for a parameter that started and ended with underscores, then interpreted that as the version.

In any case, this allows you to happily generate a new rails app using an old version of rails. Nothing quite like discovering an undocumented feature by reading the source code.

13 Responses to “Using Multiple Versions Of Rails”

  1. Jason Hamm says:

    Thanks, Glad you are out there sharing info.

  2. Siva says:

    Thanks!! Its working..

  3. Christine says:

    Very helpful! Thanks!

  4. Muni says:

    The same command works in Windows as well.
    Here is an example

    rails _2.3.5_ project_name -d mysql

    It specifies to create rails project with the rails 2.3.5 version and the database as mysql.

  5. myrm says:

    Works on mac – thanks.

  6. Michael says:

    Shouldn’t a ‘sudo’ be prefixed to it? it gives me warnings such as:

    WARNING: Installing to ~/.gem since /Library/Ruby/Gems/1.8 and
    /usr/bin aren’t both writable.
    WARNING: You don’t have /Users/myname/.gem/ruby/1.8/bin in your PATH,
    gem executables will not run.

    I don’t know much about Unix, Mac and command lines, but I assume this should be done?

    I deleted that .gem directory it created with rm -r and then ran the command again with sudo. Now it didn’t give warnings.

    Thanks for such a clear, short and to-the-point tutorial!

  7. DJ says:

    helpful while trying out rails 3. thanks!

  8. karle says:

    Thanks again!

  9. [...] somewhere. I google for “multiple versions of rails” and find a brief blog post about Using Multiple Versions Of Rails that gives me this useful piece of information: your individual rails projects store the version [...]

  10. M says:

    Thanks! This was just what I needed. (I’m developing a 2.3 app and learning from the same book)

  11. DL says:

    Thanks for posting this. You’ve just saved me a ton of time and helped me with ensuring backwards compatibility of some code I’m modifying.

  12. Brad says:

    Thanks, answered my question.

  13. Thanks, this helped me. FYI, I was juggling 2.0.2 and 2.1.1, and this advice still worked. Prior to finding this post, I did in fact open up the executable myself, however I failed to notice the _’s in the regexp.

    -John

Leave a Reply