2011-08-13

Self-contained Rails with RVM and Bundler

The idea here is to create a completely self-contained Ruby on Rails 3.1 application structure, for a nice, clean starting point to begin development of a new web application. To accomplish this we'll be making use of RVM, the Ruby Version Manager, and Bundler, the gem dependency manager.

Requirements

Before you begin, you'll need an account on a Unix-like system. Any Linux, BSD or Mac OS X system should be fine.

I think you'll also want a C compiler and related "build-essential" bits installed on this system, although you may be able to avoid that requirement by using an RVM Ruby that doesn't need to be compiled.

Superuser (root) access is not required for any of this; everything resides within ~/.rvm and a single directory for the application.

It may also be possible to do something like this on Windows, but I know nothing of such evil things.

Preparation

First, you'll need to install RVM, if you haven't done so previously. Follow these instructions to do so.

Once RVM is installed and working, you'll need an RVM Ruby. In these examples, I'll be using MRI 1.9.2; if you want to use a different Ruby, change ruby-1.9.2 to whatever you like. If you already have an RVM Ruby installed, that should work fine, just make sure you specify it in the .rvmrc file which follows in the next section.

$ rvm install ruby-1.9.2

The Application

Now that you have RVM and a Ruby to go with it, you can actually create your application structure. First, you need a project directory, and an .rvmrc file within that directory. The .rvmrc is used to configure RVM to switch to a certain Ruby and gem environment when entering the directory. This can be accomplished with the following commands; be sure to replace PROJECT with the name of your project.

$ mkdir PROJECT

$ echo "rvm ruby-1.9.2@PROJECT --create" > PROJECT/.rvmrc

You can confirm that our new .rvmrc is working, like this:

$ cd PROJECT

$ gem env

All of the paths displayed by gem env should be within your home directory.

Now that you have an empty environment, you can start adding the gems you need to get some work done. First and foremost, you'll want to install Bundler, the gem dependency manager. From this point onward, all commands should be run inside your new project directory.

$ gem install bundler

With Bundler installed, you're now ready to create a Gemfile, which will describe all of the other gems you want to use. I've provided an example Gemfile which installs Rails 3.1 here; feel free to use your own or change my example to fit your needs.

$ curl -s https://raw.github.com/keeganquinn/bits/master/blog/Gemfile > Gemfile

Once your Gemfile is in place, you can run Bundler to calculate your gem dependency tree and install all of the desired gems.

$ bundle install --without production

Finally, all gems are installed - you can now run Rails to generate the actual application structure. Note that you should call bundle exec to run commands associated with gems which were installed by Bundler - this will ensure that system-installed Ruby and/or gems will not override or interfere with your nice

$ bundle exec rails new . --skip

At this point, you should have a complete, self-contained Rails application structure, ready for you to start building it into the most amazing website in the history of the Internet - or whatever it is that you wanted to build.

No comments:

Post a Comment