237
votes

I have two versions of rails (2.1.0 and 2.2.2) installed in my computer.

When I create a new application, is it possible to specify that I want to use the older (2.1.0) version?

7

7 Answers

516
votes

I found here an undocumented option to create a new application using an older version of Rails.

rails _2.1.0_ new myapp 
68
votes

Here is the command which I use normally:

rails _version_ new application_name

for example rails _2.1.0_ new my_app

Here is the list of all available rails versions so far:

http://rubygems.org/gems/rails/versions

27
votes

I was having some trouble using rails _version_ new application_name (the resulting project was still generated for the newest version of Rails installed.)

After a bit of digging I found an article by Michael Trojanek with an alternative approach. This works by creating a folder with a Gemfile specifying the desired version of Rails and then using bundle exec rails... so that Bundler takes care of running the appropriate version of rails. e.g. to make a new Rails 4.2.9 projects the steps are:

mkdir myapp
cd myapp
echo "source 'https://rubygems.org'" > Gemfile
echo "gem 'rails', '4.2.9'" >> Gemfile
bundle install

bundle exec rails new . --force --skip-bundle
bundle update
11
votes

As rightly pointed out by @mikej for Rails 5.0.0 or above, you should be following these steps:

Create a directory for your application along with a Gemfile to specify your desired Rails version and let bundler install the dependent gems:

$ mkdir myapp
$ cd myapp
$ echo "source 'https://rubygems.org'" > Gemfile
$ echo "gem 'rails', '5.0.0.1'" >> Gemfile
$ bundle install

Check that the correct version of rails has been installed: $ bundle exec rails -v

Now create your application, let Rails create a new Gemfile (or rather overwrite the existing one by using the --force flag) and instead of installing the bundle (--skip-bundle) update it manually:

$ bundle exec rails new . --force --skip-bundle

If you check the entry for rails in Gemfile, it should be like this:

gem 'rails', '~> 5.0.0', '>= 5.0.0.1'

You should update it to the exact version needed for the application:

gem 'rails', '5.0.0.1'

Now, the final step:

$ bundle update
7
votes

There are two ways to achieve this:

one as suggested in accepted answer:

gem install rails -v 2.1.0 #only when the gem has not been installed in the desired ruby version you are using, so that you don't get error on next step
rails _2.1.0_ new my_app

and alternative method is to create gemfile with desired rails version before initializing rails project

mkdir my_app
cd my_app
echo "source 'https://rubygems.org'" > Gemfile
echo "gem 'rails', '2.1.0'" >> Gemfile
bundle install

bundle exec rails new . --force --skip-bundle

I have written about this in details in my article

3
votes

You can generate the skeleton with either version and require the one you want in config/environment.rb:

# Specifies gem version of Rails to use when vendor/rails is not present
RAILS_GEM_VERSION = '2.1.2' unless defined? RAILS_GEM_VERSION

or use the "rails" command form the version you want anyway.

3
votes

You should also take a look at "freezing" your Rails gems into the app. This helps a lot with deployment, specially in shared hosting environments.

Just change the RAILS_GEM_VERSION variable in config/environment.rb and issue the freeze rake task:

rake rails:freeze:gems