Specifying rails version to use when creating a new application

Ruby on-Rails

Ruby on-Rails Problem Overview


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?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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

rails _2.1.0_ new myapp 

Solution 2 - Ruby on-Rails

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

Solution 3 - Ruby on-Rails

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

Solution 4 - Ruby on-Rails

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

Solution 5 - Ruby on-Rails

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

Solution 6 - Ruby on-Rails

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.

Solution 7 - Ruby on-Rails

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

Solution 8 - Ruby on-Rails

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!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhectorsqView Question on Stackoverflow
Solution 1 - Ruby on-RailshectorsqView Answer on Stackoverflow
Solution 2 - Ruby on-RailsHardikView Answer on Stackoverflow
Solution 3 - Ruby on-RailsmikejView Answer on Stackoverflow
Solution 4 - Ruby on-RailsRajkaran MishraView Answer on Stackoverflow
Solution 5 - Ruby on-RailsPrakashView Answer on Stackoverflow
Solution 6 - Ruby on-RailsKeltiaView Answer on Stackoverflow
Solution 7 - Ruby on-RailsThiago ArraisView Answer on Stackoverflow
Solution 8 - Ruby on-RailsAhmet Firat KelerView Answer on Stackoverflow