Search

I love vagrant and you should too. According to its web page, Vagrant is a tool for building and distributing virtualized development environments. It uses Virtual Box to provide virtualization and allows you to do really great things quickly. It’s literally become one of my favorite tools in the toolbox almost overnight. I use it to rapidly spin up test infrastructure, vet my changes and to have a much higher degree of confidence in the effects of my architecture decisions.

Getting Vagrant going is pretty simple. On MacOS, I recommend you first get a more current ruby installed using rbenv or rvm. You’ll also need a copy of Virtual Box. At present, 4.1 is supported. If in doubt, you should upgrade.

Next step is to install the vagrant gem
$ gem install vagrant

Now that vagrant is installed you can download a base box. Base boxes are standard templates that are used as building blocks for a virtual machine. There are a bunch of publicly available base boxes. You can find a listing of a bunch here. You can also optionally build your own base boxes. I’ll explain how to accomplish this task in a later post. Once you’ve selected your base box(es) of choice, you can add them as follows:

This command will download the base box from the internet and install it under $HOME/.vagrant.d/boxes/. It is important to know that this image will not be used directly by Virtual Box and that to actually make use of the base, you must create a vagrant vm. Our first step is initialize the directory we’ll be using for vagrant.

This creates a Vagrantfile in the current directory. The Vagrantfile contains configuration information for the virtual box vm. You can assign things like static addresses or set up configuration management tools like puppet or chef from this configuration file. At this point, we’ve got some config data but we haven’t actually instantiated the vm. We can do this as follows:

Cool, that seemed to work, but what have we actually accomplished? At this point we have a running VM which is based on a copy of the lucid32 base box image we downloaded. Vagrant has automatically set up some handy shared folders for us that will be helpful for moving data back and forth from the host OS to the VM. You can see that they’ve been mounted on /vagrant in the guest. It has also provisioned SSH using a port forward. You can access the VM using the following:

The real power of vagrant lies in the ability to quickly recreate environments. We can destroy our VM completely and then recreate it from the base box image using a pristine disk image using two commands.

Think about that for a second. You can trash your dev environment and start from scratch inside of a minute or two. That’s pretty amazing in and of itself, but with the integration with Chef and Puppet, things get even cooler. In part 2, we discuss building your own base boxes with VeeWee.