Rails Intro, Deep Dive: RVM

Glenn Goodrich
Ruby Editor
This entry is part 1 of 15 in the series Loccasions

Loccasions

This post marks the first in a series to help you start from nothing and end with a Rails application. Although there are many posts/series on this subject, I feel like basic Rails tutorials, especially in the wake of Rails 3.1 changes, fall into a more-the-merrier category. Even so, this series will focus on setting things up in a proper manner (for those systems that support it) and will fly a little lower than the typical 50,000 foot level of many tutorials. Here are the items you will have learned by the end of the series:

  • Setting up Ruby Version Manager (RVM) to maintain sandboxed development environments
  • Installing Ruby 1.9.2
  • Installing Rails 3.1
  • Creating a rails application
  • What Rails IDEs exist, and their pros/cons
  • Creating a resource for your application to create/retrieve/update/delete
  • Modifying a view template
  • What to do next

This series ends where a lot of single post series end, but my goal is to make sure that you have much more information when you get to the end. Furthermore, while Rails is often touted as a good beginner’s web development framework, there are rumblings in the community that Rails has outgrown that moniker and the changes in Rails 3.1 are a result of the community being much more mature and in need of a more advanced web framework.

We are going to focus on Rails 3.1 (currently RC4) and will highlight some of the changes at 3.1 as we go. Also, in the vein of the previously linked Intridea article, I will make assumptions that you are comfortable on the command line, meaning, ‘curl’ is not only a Canadian verb. Don’t worry, I know you are able to hang with this series.

Let’s get started with the first step. I can’t stress enough how invaluable RVM is to Ruby and Rails development. In a nutshell, RVM basically allows you to create as many Ruby sandboxes as you need for development or projects or whatever. You can separate versions of Ruby as well as sets of gems (called, funny enough, “gemsets”) so you can do this tutorial without horking your base Ruby or gems. Then, you can just delete the gemset and/or the version of Ruby if you don’t want it anymore or create a new Rails 3.0.8 application so you can live in the present. Ruby development starts with RVM, so learning how to use it is a best practice you should pick up now. Unfortunately for my Windows friends, you don’t have an RVM. There is Pik, but it doesn’t have gemsets, which is probably THE feature that makes RVM so valuable.

Installing RVM

First, let’s get some terminology out of the way. RVM refers to the different interpreters as “rubies”. Each rubie has one or more gemsets associated with it. You cannot have a single gemset serving two different rubies, but you can import/export or copy gemsets between rubies. Here, we are going to use RVM to install the latest 1.9.2 rubie and create a gemset for our Rails applications.

Now that we are speaking the same language, let’s install RVM. Alas, the installation of RVM is a bit involved. Looking at the prerequisites, most of the things you’ll need are core to Mac OSX and Linux. If you have not installed git, then you should do so now, as git is the source control of most open source and Rails developers. Also, you’ll need the gcc compiler to allow RVM to compile different Ruby interpreters in your environment. For Mac users, this means installing XCode (you can install Xcode 3 for free or pay $5 for Xcode 4 in the Mac App Store. Either one is fine with RVM.). On Linux, make sure you have make and the C compiler, which you can install with

sudo apt-get install build-essential

and curl

sudo apt-get install curl

Ok, that should handle the prereqs.

There are a couple of ways to install RVM, either single-user or multi-user. We will install it in the single-user fashion, which is the way to go for developers. The multi-user install of RVM is more for server administrators, allowing for the system wide install of rubies and gemsets.

Installing RVM is just running a bash script at the command line. So, fire up your terminal and type:

bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)

this will run the rvm install bash script in your current session, installing in your home directory at ~/.rvm. Also, the output of the script will have some instructions for your .bashrc (or .profile or .bash_profile) startup scripts. RVM has to load into your shell environment when you open a terminal, so add this to the end of your startup script:


[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"

If you are interested there is a good explanation of what that statement does on startup. Once you have modified the startup script, you can either reload your startup script

source ~/.bash_profile

or close your terminal and open a new one. Now type

type rvm | head -1

and you should see

rvm is a function

Now, we can go get some rubies. (YAR! That makes me feel like a pirate!)

First, let’s review our choices:

rvm list known
RVM Known Interpreters

RVM Known Interpreters

Wow. I had no clue there were that many. Let’s install them all..BWAHAHAHAHAHA…no, wait, (smooths back hair) let’s just install one. I vote for 1.9.2, and my vote is the only one that counts.

 

rvm install 1.9.2
Installing Ruby 1.9.2

Installing Ruby 1.9.2

As you can see, this installs the latest patch level of MRI (Matz’s Ruby Interpreter), which is 180 in this case. With RVM, you can target patch levels or the latest (head) stable build. Either one serves our purposes here, so p180 it is. When the install is complete, RVM will install the “default” gemsets, which you can define in ~/.rvm/gemsets/default.gems. Currently, all I have in there is rake, but you can add others as needed.

We have to tell RVM that we want to use that newly loaded 1.9.2 Ruby interpreter. This is done with

rvm use 1.9.2

Awesome. Now, if you type gem list, you should just see the default gems. In my case, I get

RVM Gem List

RVM Gem List

This validates that rake is the only gem in my current rubie. Obviously, we’re going to want Rails installed, but before we do that, let’s create a gemset for this tutorial called “rubysource”.

 

rvm gemset create rubysource

RVM tells us that our gemset is created, now we have to use that. Can you guess how that’s done? If you said,

rvm gemset use rubysource

then you’re a winner! So, as we’re working, how do we know what rubie and gemset combination is the current one? If only RVM had an easy way to give us that kind of (hint, hint) info.

rvm info
RVM Info

RVM Info

That command gives us all kinds of great information, like which interpreter we’re using, the current gemset, where the binaries for the current rubie reside, and the relevent Ruby environment variables. It’s worth noting that the syntax for indicating rubie and gemset is rubie@gemset, which you can also use as a shortcut when switching rubies/gemsets. For example, if you type

rvm use 1.9.2@rubysource

it will switch the current ruby to 1.9.2 and the current gemset to rubysource. For homework, go figure out how to use that shortcut to automatically create the gemset if it does not already exist. (Pop quiz on Friday)

So, that is RVM in a (relatively) quick post. In the next post of the series, we’ll get through installing Rails 3.1, as well as creating our Rails app. In the meantime, feel free to play with RVM and get comfortable using it for all your Ruby development.

Loccasions

Rails Intro, Deep Dive: Installing Rails, Part One >>

Win an Annual Membership to Learnable,

SitePoint's Learning Platform

  • Jonatan

    I look forward for more post from you guys, i’m just about to start learning Ruby, and love reading educational articles that will help me in the future :)

  • http://www.woobl.net Johann Smith

    Thanks for the article, just Evernote clipped this page for later reading

  • Les

    Still falling well short of the capabilities of what PHP can do; Ruby on Rails is still (trying?) playing catch up on PHP and what can be accomplished… have you not heard of Facebook?

    Lets begin again… PHP is thee platform for web development, all others are pretenders. Sorry!

  • OJS

    Well said sir …I am pleased that you have started your introduction with intimations to the effect that “ ruby/ rails is not as simple in practice as it may be in theory and not a panacea for all the problems of novice developers seeking to start in building web apps . I have found as a novice developer that to be very much the case once you stay from the “blog in fifteen minutes path” ..It all for me falls apart when it comes to matching up gems and their necessary dependencies with a particular version of the ruby interpreter . Its easy to choose a gem from recommendations on forums /tutorials or on Github that look like solving a need in an application you are seeking to develop only to find that you don’t have the tier of dependencies of other gems installed;and installed in the right place !!.These missing gems prove necessary to the correct functioning of the gem you particularly want. and before you mention it ..no “bundle install” has not proved for me to be a “cure all” for this dilemma either ) Then even if you can get it set up on a development machine there is the potential of a possible mismatch with the ruby version you are deploying to on your hosts server . I am interested to see how you will steer a straight path around the gems trapdoors such as the ones I have encountered so far {ie “Rmagick”}
    to arrive safely at a successful deployment ( I hope you don’t just do a heroku deployment as that is avoiding the pain of those of us who want/need to deploy someplace else . Now my question… if we are going to deploy to a production server with an Enterprise Version of the ruby interpreter should we not be developing with a EV ruby sandbox so that we can stand a better chance of deploying successfully .?
    Look forward to seeing and learning some more foundational principles that might help me get my , abandoned due to “I don’t have enough time or knowledge to sort this “,ruby rails project back riding on the rails . Thanks for your starter post I will look out for the next one

  • Glenn

    Hey OJS,

    This series will likely take a few turns down paths (like deployment) that I hope will cover some or all of what you need. Deployment is a few posts away, but we’ll get there.

    Thanks for the comment.

  • Jason

    Great start. I learned more in this article about RVM then I did from a book I just purchased. I have already recommended to someone who is just starting out. Can’t wait for more.

  • http://giving.childrenshospital.org Adam Cassel

    Thanks so much for a terriffic series! Coming from .Net/Windows to OSX and Ruby/ROR/Sinatra, is a steep learning curve. Your environment setup and installation guidance with using RVM as the first article is a prescious ‘gem ‘ indeed!

  • typingduck

    Thanks for the article.

    (typo for “rvm type | head -1″, I assume it’s s/rvm type/type rvm/)

    • http://www.ruprict.net/ Glenn Goodrich

      Yup, nice catch typingduck…I fixed the article. Thanks!

  • Backslider

    A few things missing here. If you find that ruby won’t install, try first installing GIT

    • http://www.ruprict.net/ Glenn Goodrich

      I mention the need to install git when I mention the prerequisites.

      Did you find other things that are missing?