Zend certified PHP/Magento developer

RubySource: Rails Development 101: RVM

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

rvm type | 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.