Generate an application with a specific Rails version

2 minute read

There are few ways we can create an application with specific rails version.

In this article, we will create a new rails app of version 6.0.1 while our system may have multiple rails versions installed in our system.

Here is the list of rails versions that were already installed in my system while writing this article.

rails (6.0.2.2, 6.0.2.1, 6.0.1)

Method 1: Common method

Let’s start with most common and widely documented method:

 rails version new app_name

Code example: Let’s create an app named my_app with rails version 6.0.1.

gem install rails -v 6.0.1 #only when the gem has not been installed in the desired ruby version you are using
rails _6.0.1_ new my_app

The _6.0.1_ feature is provided by RubyGems. It checks if the first argument(ARGV Array) is a version. It then modifies the $LOAD_PATH accordingly. Ruby seeks help from RubyGems because it knows where the gems are stored.

Hence this code will fail if you are currently using a different version of Ruby and in that particular version of Ruby that particular version of the gem might has not been installed. Thank you Shiva Bhusal for pointing out the problem and solution in the comments.

Method 2: Let bundler handle the rails version

In this method we will let bundler handle rails version.

Our workflow if we wanted to setup new project with rails version 6.0.1 would be:

2.1: Make new folder and initialize bundler

# make directory for new rails app
mkdir my_app
cd my_app

# specify ruby version we want to use
echo 2.4.1 > .ruby-version

# initialise bundler (creates Gemfile)
bundler init

2.2: Specify rails version

At this stage, our Gemfile probably looks like this:

# A sample Gemfile
source "https://rubygems.org"

# gem "rails"

We have to uncomment rails gem and specify version so that our Gemfile looks like this:

# A sample Gemfile
source "https://rubygems.org"

gem "rails", '6.0.1'

Now we need to run bundle install. (This would install rails 6.0.1 if it had not been installed yet).

2.3: Initialise new rails app with specified rails version

At this point we already have minimal project with rails version 6.0.1.

Now, we can use the rails command line tool with bundle exec to force the version:

bundle exec rails new . --force # --force to overrite curent Gemfile

If you were unaware about rails runtime option force, it is used to overwrite files that already exist. Just type rails in your commandline and you will see explanation about this command.

Now, we have new rails project with our desired rails version(6.0.1).

What do you think about this flow of setting up version specific new rails project? Feel free to comment.

Happy learning!

References:

  1. https://relativkreativ.at/articles/managing-multiple-rails-versions
  2. https://coderwall.com/p/wrukbg/generate-an-application-with-a-specific-rails-version

Tags:

Categories:

Updated:

Leave a comment