Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you use multiple rails versions with rbenv?

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).

like image 724
aciniglio Avatar asked Jan 16 '12 09:01

aciniglio


People also ask

How do I use multiple versions 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).

Can I use both RVM and Rbenv?

RVM vs Rbenv - which Ruby Version Manager should I use? The short answer for developers is: it does not really matter, just pick one and be done with it - both RVM and Rbenv get the job done.


2 Answers

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

like image 141
Nathan Avatar answered Oct 14 '22 03:10

Nathan


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!

like image 40
Michael Trojanek Avatar answered Oct 14 '22 02:10

Michael Trojanek