How to set gemset on RVM automatically

RVM here stands for Ruby Versioning Manager, you ran read more about it here. After installing RVM , we need to create gemset, then tell our Rails application to use it. That's it : tell our Rails application to use it is not difficult but it's repetitive. We have to run this command , each time we move to our application root directory :

rvm use 1.9.2
rvm gemset use  #for example : test

The worese thing, each time you opened new tab on your console or terminal, though it will poin to the same directory, you have to run them again to make your Rails application using a gemset. And this post will show you how to automate that repetitive process.

To avoid such repetitive process, you have to (I assumed your console has been pointed to your Rails application directory) :

  • Create or modify (if there's existing one) this file  : .rvmrc
  • You can create it manually if you can find it on your application directory. Please notice that such file will be hidden by default.
  • Open that file, and add this command: rvm use 1.9.2@<gemset_name>
  • Replace 1.9.2 with the Ruby version you have installed with RVM. You can also 1.8.7 too
  • Replace <gemset_name> with the name of gemset you will use for your Rails application. I assumed you have created and installed all required gems inside it.
  • After that, point your console to upper directory with cd ..
  • Then point your console back to the application the application directory, and you will see :
==============================================================================
= NOTICE                                                                     =
==============================================================================
= RVM has encountered a new or modified .rvmrc file in the current directory =
= This is a shell script and therefore may contain any shell commands.       =
=                                                                            =
= Examine the contents of this file carefully to be sure the contents are    =
= safe before trusting it! ( Choose v[iew] below to view the contents )      =
==============================================================================
Do you wish to trust this .rvmrc file? (/some_path/.rvmrc)
y[es], n[o], v[iew], c[ancel]>

Just type y and press Enter button. You're done. Next time you point your console or terminal to your Rails application directory, the gemset will be automatically set. Therefore, there is no need to type it manually anymore.

By the way, if you haven't understood what gemset is, just imagine it likes a bedroom and gems are the things placed inside it. There may be a bed, a table, chair, sofa, etc. The same goes with gemset, there may be a devise, omniauth, fastercsv, nokogiri,etc.