247

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?

John Topley
  • 113,588
  • 46
  • 195
  • 237
hectorsq
  • 74,396
  • 19
  • 43
  • 46
  • http://www.railshorde.com/blog/rails-command-to-create-new-application-with-specific-version-and-database – Animesh Jun 16 '15 at 19:11

8 Answers8

527

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

rails _2.1.0_ new myapp 
Paweł Gościcki
  • 9,066
  • 5
  • 70
  • 81
hectorsq
  • 74,396
  • 19
  • 43
  • 46
  • 1
    You should change this to the accepted answer since it works for Rails 2 and 3. Keltia's answer will no longer work if you have Rails 3 installed and want a Rails 2 app. – Peter Brown Feb 06 '11 at 15:03
  • Erroneous that having Rails 3 installed fails for _2.3.5_ -- Just tested on Mac OS X Snow Leopard with these modules installed:rails (3.0.5, 2.3.5, 2.2.2, 1.2.6) – Mike May 02 '11 at 19:41
  • 14
    This is RubyGems functionality, not Rails functionality; it's therefore not dependent on Rails version, and will work for other gems. (Thanks, this is a great answer!) – Calrion Sep 03 '11 at 01:02
  • 3
    I get error when using 3.1.3 when 3.1.3 and 3.2.0.rc1 are installed. Here is the error --- [ninad@localhost devel]$ rails `_3.1.3_` new sample_app /home/ninad/.rbenv/versions/1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems.rb:314:in `bin_path': can't find gem railties (["3.1.3"]) with executable rails (Gem::GemNotFoundException) from /home/ninad/.rbenv/versions/1.9.2-p290/bin/rails:19:in `
    '
    – Ninad Dec 25 '11 at 11:04
  • Very helpful now that 4.0.0 is out! – Jason Jul 16 '13 at 20:45
  • This also works for engines: `rails _3.2.11_ plugin new myapp` – lflores Jun 20 '14 at 05:13
  • Doesn't work with `ruby` => `2.3.2`, < `2.4`. The bug was introduced in a [backport](https://github.com/ruby/ruby/commit/996b1bfa95) solving another [issue](https://bugs.ruby-lang.org/issues/12326). It appeared in `2.4` branch and was fixed before any kind of release. In terms of `rubygems` that would be >= [`2.5.2`](https://github.com/rubygems/rubygems/pull/1407), < [`2.6.2`](https://github.com/rubygems/rubygems/pull/1527). The workaround is update `rubygems` and then binstubs: `gem update --system && gem pristine --all --only-executables`. – x-yuri Mar 27 '19 at 15:04
73

Here is the command which I use normally:

rails _version_ new application_name

for example rails _7.0.4_ new my_app

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

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

Hardik
  • 3,815
  • 3
  • 35
  • 45
28

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
mikej
  • 65,295
  • 17
  • 152
  • 131
  • I don't think there is need of `bundle update`, it will update rails too!! – Rajkaran Mishra Jul 29 '17 at 15:14
  • @devel the `bundle update` is needed because a manual update is being used (`--skip-bundle`). Rails won't be upgraded because a specific version is specified in the Gemfile (4.2.9 in the example.) – mikej Jul 30 '17 at 15:36
  • `--force` is overwriting the Gemfile – Rajkaran Mishra Jul 30 '17 at 15:41
  • Yep, but the new Gemfile that gets written _still_ specifies the version of Rails we wanted (because `bundle exec rails new...` ran the version of `rails` specified in the Gemfile that ends up being replaced.) – mikej Jul 30 '17 at 15:52
  • 1
    I did `gem 'rails', '5.0.0.1' >> Gemfile` and after running `bundle exec rails new` with --force option, its `gem 'rails', '~> 5.0.0', '>= 5.0.0.1'` in Gemfile. Now when I run `bundle update`, its updating the rails to 5.0.4 (in Gemfile.lock), but I was expecting to use rails version 5.0.0.1 – Rajkaran Mishra Jul 30 '17 at 16:04
  • @devel thanks for sticking with this! It looks like a difference between 4.x and 5.x versions. I hadn't spotted this as I've mostly used this approach for creating 4.x apps since the release of Rails 5. With 5.0.1 the generated Gemfile specifies the version using the [~>](http://guides.rubygems.org/patterns/#pessimistic-version-constraint) operator: `gem 'rails', '~> 5.0.1'`. So you might need to edit the Gemfile after the `bundle exec rails new...` if you want to stick on the exact version. – mikej Jul 30 '17 at 16:40
17

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
Rajkaran Mishra
  • 4,532
  • 2
  • 36
  • 61
9

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

Prakash
  • 581
  • 5
  • 16
3

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.

Keltia
  • 14,535
  • 3
  • 29
  • 30
3

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
Thiago Arrais
  • 33,360
  • 7
  • 30
  • 34
0

Please watch out which version of ruby you are using with Rails.

The command for making a new project for a specific version of Rail may not work for you. I had some issues about it. And the problem was the ruby version I have default which is 3.0.0. This version did not work with Rails 5. Then I installed ruby 2.7.5 and switched to it as default. Only then I was able to make projects both for Rails 5 and 7.

If you want the same environment with ruby 2.7.5

rvm install ruby-2.7.5

switch to this version as default

rvm --default use 2.7.5

install bundler and webpacker

gem install bundler
gem install webpacker

install lastest rails (which is 7)

gem install rails

test it

rails new test_app_6
cd test_app_6
rails s

check for localhost 3000

http://localhost:3000

then stop the server (control + c) and install Rails 5

gem install rails -v 5.2.6

test it

rails _5.2.6_ new test_app_5
cd test_app_5
rails s

check for localhost 3000

http://localhost:3000

You're set!

Ahmet Firat Keler
  • 2,603
  • 2
  • 11
  • 22