Handling Ruby and Rails versions : Hints for beginners
How to choose the Ruby and the Rails versions for my project ?

In this article, I will show you how to create a Rails app with the Ruby version and the Rails version you want.
Let’s first create a directory for our app.
mkdir playground
cd playground/Rails needs to be installed to run the rails new command. Since Rails is a gem like any other, we must choose the Ruby version with which we want to use the rails new command. Let’s list the Ruby versions installed on our machine with rbenv :
➜ playground rbenv versions * system (set by /usr/local/opt/rbenv/version)2.3.02.3.32.4.1
We can see here that four versions of Ruby are installed. The one called “system” is a pretty old version installed by default on MacOSX. The asterisk means it is the currently active Ruby version. If we want more details about this “system” version, we can simply run :
➜ playground ruby -v ruby 2.0.0p648 (2015-12-16 revision 53162) [universal.x86_64-darwin16]
I want to use Ruby 2.4.1. As explained in rbenv documentation we can set a local application-specific Ruby version by writing the version name to a .ruby-version file in the current directory. We achieve that running :
➜ playground rbenv local 2.4.1And then we make sure it worked :
➜ playground rbenv version 2.4.1 (set by /Users/charlesmarcoin/code/playground/.ruby-version)
So far so good. Then we want to check which versions of Rails are installed on our machine for ruby 2.4.1. We can run for instance :
➜ playground rails -v Rails 5.1.3
But rails -v only tells us about the latest version of Rails installed on our machine for Ruby 2.4.1 ( Rails 5.1.3 in this case). This means that if we run rails new a Rails 5.1.3 application will be created. What if I want to create an application with an older version of Rails ?
Let’s see if there are other Rails versions installed for Ruby 2.4.1 :
➜ gem list rails | grep rails
rails (5.1.3, 5.0.5)Looks like we also have Rails 5.0.5 installed for ruby 2.4.1.
To understand how this works. Let’s switch to ruby 2.3.3 and look at our rails versions :
➜ playground rbenv local 2.3.3 ➜ playground rails -v Rails 5.1.3➜ playground gem list rails | grep railsrails (5.1.2, 5.0.5)
We can see here that we have only two versions of Rails installed for Ruby 2.3.3 which are Rails 5.1.2 and Rails 5.0.5. This means that if we run the rails new command in the context of Ruby 2.3.3, a Rails 5.1.2 application is going to be created.
Let’s switch back to Ruby 2.4.1 and create our Rails application.
➜ playground rbenv local 2.4.1 We know that there are only two versions of Rails installed for Ruby 2.4.1 on the machine : 5.1.3 and 5.05. If we want to create a Rails 5.1.3 app, we just have to run rails new. But what if we want to create a Rails 5.0.5 app ?
Starting from here, there are two ways of doing so :
Uninstall the rails versions we don’t want to use
We can use the gem uninstall rails command to remove the rails versions that we do not want to use.
➜ playground gem uninstall railsSelect gem to uninstall:1. rails-5.0.52. rails-5.1.33. All versions>
We just need to uninstall rails 5.1.3 using the gem uninstall rails command and then run rails new which will generate a Rails 5.0.5 application because it is the most recent version of the Rails gem installed on the machine for Ruby 2.4.1 :
rails newLet’s open the Gemfile and check if it worked :
gem 'rails', '~> 5.1.3'Yeah !
Use the undocumented “rails new _version_” command
This is faster but undocumented. Let’s just specify the rails version we want to use like so :
➜ playground rails _5.0.5_ new .Let’s open the Gemfile and check if it worked :
gem 'rails', '~> 5.1.3'Yeah !
You can now lock the ruby version which should be used by the app by editing the Gemfile and adding :
ruby '2.4.1'This is used by platforms like Heroku. They will run your app using the Ruby version you specified.
Hope this helps.