Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying rails version to use when creating a new application

People also ask

How do I use a different version of Rails?

There's nothing you need to do to manage two different Rails versions. The only thing you'll want to do is gem install rails to get the latest version and create your new project with rails new myapp . That will make sure the new project starts with Rails 5.1 (or whatever is the latest at the time).

Which version of rails should I use?

It's always recommenced to use the latest stable version of rails and other gems. As you mention 6.1. 4 is the latest version atm. So don't be afraid of this change.

How do I tell what version of Rails is installed?

To find out what the most recent Rails version is, use the command gem search rails | grep "^rails " . As I am writing this, it is 5.0. 1. Check that the correct version has been installed using bundle exec rails -v which should output Rails 5.0.


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

rails _2.1.0_ new myapp 

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


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

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