Hi, this post describes how to use rbenv and bundler to handle multiple ruby versions and gems installed with each (rails for example), there’s an alternative to that (RVM) that does something similar but I preferred rbenv as I think it’s simpler.
rbenv basically lets you have multiple ruby versions on your machine and you choose which version to use with which project through many ways; the simplest of which is having a .ruby-version file in the root of your project which states what version is it using.
First install rbenv, follow the instructions here: https://github.com/sstephenson/rbenv#installation
Add the ruby-build plugin to rbenv, this plugin “provides an rbenv install
command to compile and install different versions of Ruby on UNIX-like systems”. Follow instructions here to install it: https://github.com/sstephenson/ruby-build
Now install the version you want of ruby, to avoid problems related to readline library which “provides a set of functions for use by applications that allow users to edit command lines as they are typed in” you can first install readline
sudo apt-get install libreadline-dev
Then install the ruby version with readline configurations*
CONFIGURE_OPTS="--with-readline-dir=/usr/include/readline" rbenv install 1.9.3-p484
- Assuming that readline was installed to /usr/include/readline
-
This will install ruby version 1.9.4-p484
-
When I didn’t do the readline configurations I got this error
uninitialized constant Debugger::LocalInterface::Readline (NameError)
Now you have to call the rehash command of rbenv to catch up the new ruby version installed
rbenv rehash
Add a .ruby-version file in the root of your project to tell rbenv what ruby version are you using for it, just write the version name inside the file
1.9.4-p484
You’ll need to install bundler to your local gems for the first time only
gem install bundler
If you don’t have the gem “debugger-ruby_core_source” install it before bundling to avoid errors such as
No source for ruby-1.9.3-p484 provided with debugger-ruby_core_source gem
gem install debugger-ruby_core_source
Now you can run bundle in your project
bundle
with rbenv each ruby version installed has it’s own set of local gems, so if you want to switch between rails 3.x on ruby 1.9.x and rails 4.x on ruby 2.x you have to install two versions of ruby using rbenv, then in the directory where you’ll create your project specify which ruby version you’re using:
rbenv local 1.9.3-p484 rbenv local 2.1.2
Then according to what ruby version are you using and the local gems installed with it you’ll get the rails version.