Jul
5
2010

Getting started with Rails 3 using RVM

In this post, I will show you how to get Rails 3 running with RVM.

Installing Rails

This a “chicken and egg” kind of problem. You’ll need to add Rails to your Gemfile, and use bundler. But until you install Rails, you won’t have access to the rails command. As of this writing, rails’ latest version is beta 4, so you will need to run the following command.

$ gem install rails --version "3.0.0.beta4"

Creating the application

Now that we have Rails installed, we can execute the rails new command.

$ rails new sample_app --skip-testunit --skip-prototype

This will create your Rails 3 application. As you can see from the command options, we skipped generating the test folder and the prototype files. The reason for this is that I use Rspec and jQuery, so there’s less cleanup to do. You can see other possible command options by executing this command:

$ rails --help

Using RVM

RVM has a really cool concept of gemsets. From the documentation:

RVM gives you compartmentalized independent ruby setups. This means that ruby, gems and irb are all separate and self-contained from system and from each other. You may even have separate named gem sets.

We will create a .rvmrc file in our application dir, and use an isolated gemset for our app, like so:

$ echo "rvm ruby-1.8.7@sample_app" > .rvmrc

Now we have a clean gemset to use with out application. You can change the ruby version to whatever you like, for instance, to 1.9.2 or head. To actually create the gemset, you can either do it manually

$ rvm gemset create "sample_app"

or export the following flag in .rvmrc:

$ rvm_gemset_create_on_use_flag=1

This way, when you change to the application directory, RVM will pick up the .rvmrc file and automagically switch to the sample_app gemset. All you need to do now is bundle install and you’re off to go.

About the Author: Pedro Sampaio

I'm an UX Software Engineer at FARO Technologies, in Portugal. I work mainly with .NET technologies, such as WPF, during the day. Off work, I develop web application in Rails. I also have experience with ASP.NET MVC, Test and Behavior Driven Development, and agile methodologies, namely Scrum.

3 Comments + Add Comment

  • Hi Pedro,

    You can avoid that “chicken and egg” kind of problem by doing the following:

    1) Create the folder for your rails app
    2) Inside your rails app folder
    – Create the Gemfile with the gems need for your app
    – Run “bundle install vendor/bundle” (install all the gems need for the app in the app vendor folder)
    – Run “bundle exec rails new .” (will use the rails version specified in the Gemfile)

    This way you don’t have to install rails in order isolate you app dependencies.

    NOTE: When creating your rails app, rails will detect that Gemfile already exists. Answer ‘n’ to avoid overriding it.

    • Nice tip. Never used bundle exec before, but that seems like a good enough reason to give it a try. Thanks!

  • [...] my previous post about Rails3 and RVM (and for my own future reference), you can create a new Rails 3 application [...]

Leave a comment