33

Is it possible to use multiple versions of rails using rbenv (e.g. 2.3 and 3.1)? This was easy with gemsets in rvm, but I'm wondering what the best way is to do it now that I've switched to rbenv (also, I'm looking for a way to do it without rbenv-gemset).

1

3 Answers 3

64

not sure if you got an answer to this, but I thought I'd offer what I did and it seemed to work.

So once you get rbenv installed, and you use it to install a specific ruby version, you can install multiple versions of rails to for that ruby.

STEP 1. Install whatever version(s) of rails you want per ruby version

% RBENV_VERSION=1.9.2-p290 rbenv exec gem install rails --version 3.0.11

By using the "RBENV_VERSION=1.9.2-p290" prefix in your command line, you're specifying which ruby rbenv should be concerned with.

Then following that with the "rbenv exec" command, you can install rails. Just use the version flag as in the example to specify which version you want. Not sure if you can install multiple versions in one shot, but I just run this command as many times as needed to install each version I want.

Note: This will all be managed within your rbenv directory, so it's perfectly safe and contained.

STEP 2. Build a new rails project by specifying the rails version you want.

% RBENV_VERSION=1.9.2-p290 rbenv exec rails _3.0.11_ new my_project

STEP 3. Don't forget to go into that project and set the local rbenv ruby version.

% cd my_project
% rbenv local 1.9.2-p290

Now if you want to delete this project, just delete it as normal.

If you want to delete / manage a rails version from rbenv gems, you can use regular gem commands, just prefix your command line with:

% RBENV_VERSION=1.9.2-p290 rbenv exec gem {some command}

And of course, you can delete a complete ruby version and all its shims, etc that are managed within rbenv pretty easily. I like how self contained everything is.

Hope this helps.

For reference, this is a pretty good walk through of at least some of this stuff:

http://ascarter.net/2011/09/25/modern-ruby-development.html

5
  • thanks for this answer, but I wonder can't you just create rails project and specify the rails version inside the project and then do bundle install? can you explain how your answer would be a better solution? I'm not that experienced so it would be helpful to know. thanks
    – Leahcim
    Jan 3, 2013 at 6:40
  • by the way, once you create the project with that prefix, do you use that prefix for every command you do in the project? prefix + bundle install? prefix + gem install {gem name} etc ?
    – Leahcim
    Jan 3, 2013 at 6:52
  • No, you don't need to continue using the prefix. My answer here was a bit superflous because RVM is a complete package manager (ruby + rails version), and I was trying to illustrate a way to do something similar with rbenv, which unlike RVM is mainly a ruby manager. The OP was asking about using multiple rails versions with rbenv, so I was describing how to install specific rails version to a specific ruby version (of which you can install multiple), and then select which one to use when creating a project with that ruby version. This would give you something similar to RVM.
    – Nathan
    Jan 10, 2013 at 5:43
  • 1
    As for installing the default and then changing it in the Gemfile... I suppose you can do that. In my mind, the configurations and other settings across rails version vary enough that it just seems cleaner to specify the version you want to install (assuming you need to fuss with versions in the first place).
    – Nathan
    Jan 10, 2013 at 5:56
  • 1
    Step 1 didn't work for me, i got the following. ERROR: Error installing rails: "rails" from rails conflicts with installed executable from railties Jan 22, 2014 at 2:48
30

There is a rbenv plugin called rbenv-gemset which should behave similar to the rvm gemset-command but since rbenv was never intended to work this way, I haven't tried it.

I usually manage Rails versions with Bundler as Nathan suggested in the comments of one of the other answers. I create a Gemfile with my desired Rails version, run bundle install, create the Rails application, let it replace the Gemfile and let Bundler take over:

mkdir my-rails-app
cd my-rails-app
echo "source 'https://rubygems.org'" > Gemfile
echo "gem 'rails', '3.2.17'" >> Gemfile
bundle install

bundle exec rails new . --force --skip-bundle
bundle update

If you want more detail, I wrote an article on my blog about it.

Hope it helps!

2
  • that is a nice article. Thanks to bundler
    – onmyway133
    Mar 23, 2016 at 8:36
  • 2
    that was the solution i was looking for +1
    – Qchmqs
    Jan 8, 2017 at 14:57
2

If you have setup ruby using rbenv the following will work.

Installing rails, the latest version (7.x as of Oct 2022)

gem install rails -v 7.0.2.4
# Find exe
rbenv rehash

To create a rails project with the latest rails version,

rails new project_1 

This will create a rails application with the latest version, to verify we can see the rails version in the Gemspec file (or) see the logs during the installation,

rails 7.x

Installing rails, 6.x.x.x version

Assuming we are going to install rails 6.0.4.8, then issue the following commands

gem install rails -v 6.0.4.8
rbenv rehash

Now, to create a rails project with 6.0.4.8 version (which is installed previously), specify the rails version along with the rails command.

rails _6.0.4.8_ new project_2

This will create a rails application with the 6.x version, to verify we can see the rails version in the Gemspec file (or) see the logs during the installation,

rails 6.x

Other notes

  1. Similarly, we can manage any no of rails versions in any number of projects.
  2. rbenv rehash Installs shims for all Ruby executables known to rbenv
  3. In this approach, you don't need to set or modify any ruby environment variables.
  4. You don't need to modify Gemspec file by yourself.
  5. The instructions work as of Oct 2022.
1
  • thanks, man. the underscore before and after the rails version really helped. Nov 12, 2022 at 10:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.