Virtualize This - Instant Rails in a Virtual Box
Engine Yard employees are all about virtualization. At Thanksgiving dinner, vegan Rubyist Martin Emde nibbled on a virtual turkey. Application engineer and telecommuter, Kevin Rutten, embarks on a virtual commute every morning as he ambles down the hallway to his home office. And of course, our Ruby on Rails Platform would not be possible without game changing virtualization technology like vSphere, XenServer, and Xen.
Our training team, Engine Yard University, will also be using virtualization. We decided to offer our next Zero to Rails 3 Ruby on Rails class virtually, as opposed to physically, to eliminate the expense and inconvenience of travel. In building this class, we wanted to provide a practice environment for Rails development, with zero configuration involved. But how could we do this on a student’s machine that we do not control?
The solution was Vagrant, a tool for building and distributing virtualized development environments, by Mitchell Hashimoto. Using Vagrant, we built a headless VM that runs a full Ruby on Rails stack for local development. Since it includes Ubuntu, it is a big download, weighing in at around 400 megs, but the problem it solves is worth every byte.
A week after building the Vagrant Ruby/Rails VM, we recognized that Vagrant solves many problems outside the context of our curriculum. Engine Yard has been referring people to Vagrant for a number of reasons.
With Vagrant, you get:
- A perfectly controlled development environment to be shared between staff
- Something that works on Windows (32-bit), OS/X and Linux - wherever VirtualBox runs
- A preconfigured, locally shared, folder
- The ability to edit files in the VM using TextMate, Eclipse, Notepad++, etc.
- Automatic port forwarding to easily browse an application, or other services
- SSH without the hassle of usernames or passwords
- Scriptable configuration using Chef.
Installation Instructions
If you are feeling adventurous, and want to build your own Vagrant, Mitchell Hashimoto’s documentation is friendly and informative. For your benefit, here are the instructions on how to get our Vagrant VM up and running:
- Download and install VirtualBox
- Install Ruby. For Mac, Ruby and Rubygems are already installed. For Windows use the RubyInstaller.
- On a command line, install Vagrant:
(gem install vagrant)
- Download Engine Yard University’s Vagrant environment and unzip it.
- On a command line, use CD to change directories into the folder you created via unzip.
- Load the VM
(vagrant box add eyu package.box)
- Start the environment
(vagrant up)
- Login (no username / password needed) using:
vagrant ssh
Note: Windows users need to do a bit of Putty configuration described here. - Test your environment by creating a Rails app
(rails new zagnut)
- Change directories to your Rails app
(cd zagnut)
- Start the application
script/rails server
- Open a browser and navigate to the http://localhost:3033
- You will see the standard welcome to Rails web page
- In the command line, stop the server by pressing ctrl-c
- Exit the environment
(exit)
- Shutdown the environment
(vagrant destroy)
Congratulations!
You just installed an Ubuntu server, complete with a full Ruby/Rails stack, logged into it, launched a Rails app, and browsed it using your local browser.
You may have noticed that the web server in Ubuntu used port 3000, but your browser pointed to port 3033. This and other ports are forwarded via configurations in the file named Vagrantfile
located in the Vagrant folder created from the .zip archive.
Also, the Rails application files you created are on your local drive, not the virtual machine. You can view these files in the shared directory, which resides in the Vagrant folder created from the zip archive. This is handy because you can edit files locally using the text editor of your choice.
Note: For those familiar with Vagrant, you might wonder why we did not store a .box file on S3, rather than a .zip archive. Great question! When packaging a Vagrant box, you have the option of putting configuration settings inside of the box instead of an external file. It appears that the current Vagrant release was subverting our attempts here. So, we just took a different road, and put the config file (Vagrantfile) in the .zip instead.
Share your thoughts with @engineyard on Twitter