Blog

Virtualize This – Instant Rails in a Virtual Box

By | December 3rd, 2010 at 12:12AM

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.

  • Alex

    Can't donload zip file!

  • Steve

    Same here

  • Porta

    Same problem downloading the file. Got an "Access Denied" error

  • andy

    What about fog? Isn't fog an EngineYard project for provisioning virtual machines?

  • mshe

    Should be fixed now. Thanks for catching.

  • Mitchell Hashimoto

    Andy,

    Fog is an EngineYard project for controlling cloud resources, whereas Vagrant is a tool for managing local virtual machines. They work well together hand-in-hand.

    Best,
    Mitchell

  • http://jasonnoble.org Jason Noble

    [default] The guest additions on this VM do not match the install version of
    VirtualBox! This may cause things such as forwarded ports, shared
    folders, and more to not work properly. If any of those things fail on
    this machine, please update the guest additions and repackage the
    box.

    Guest Additions Version: 3.2.10
    VirtualBox Version: 3.2.12

    Just a warning though, everything still worked. Thanks for making it so easy.

  • http://engineyard.com Matt Reider

    Interesting! Thanks for the catch. Fortunately the guest addition seems to be compatible.

  • Ron Green

    This is awesome. Thank you.

  • Lumbergh

    I'm honestly not sure how you will edit files in an Ubuntu VM with TextMate?

  • http://aelogica.com/ Steven Smith

    This looks great and I am excited to try it but I received the same warning and found that ssh was not working out of the box:

    $ vagrant ssh
    Warning: Permanently added '[localhost]:2222' (RSA) to the list of known hosts.
    vagrant@localhost's password:

    It wants a password which means it didn't pick up any ssh identities. Even supplying the following explicitly:

    $ ssh -i /Users/steve/.rvm/gems/ruby-1.9.2-head@rails3/gems/vagrant-0.6.8/keys/vagrant -p 2222 localhost
    steve@localhost's password:

    …still prompts for password.

  • http://zabouti.tumblr.com George Entenman

    Can this be used in VMware?

  • http://zabouti.tumblr.com George Entenman

    If VirtualBox works like VMware, the VM os can share files the host computer. That's what I do on my Mac: I edit from the Mac side and run my code from the ubuntu side.

  • http://engineyard.com Matt Reider

    That's odd. Perhaps that goes back to the Guest Additions problem.

    Vagrant's uname and pword are always "vagrant" / "vagrant"

  • tdahl

    You can also map folders to the host operating system in VirtualBox.

  • max

    thank you so much for this.

  • andy

    I would be great if Vagrant and Fog had the same API, so you could test locally and then deploy to the cloud w/o changing provisioning systems.

  • geemus

    That would be awesome. It is something that I (maintainer of fog) and mitchellh (maintainer of vagrant) have discussed. I think there will be something along these lines at some point, but it may just take a while to round up all the pieces to make it happen.

  • http://www.fxexchangerate.com/ fxgeorges

    I love virtualized solutions like this.
    Also check out the TurnKey Linux Ruby on Rails Appliance. They have a ton of other appliances for other uses, too.

  • James Turnbull

    Don't forget that since Vagrant 0.6.9 you can also use Vagrant with Puppet! :)