Specifying rails version to use when creating a new application

Viewed 96007

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?

8 Answers

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

rails _2.1.0_ new myapp 

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

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

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.

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

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!

Related