easy test environments with vagrant and virtualbox

I am probably like most developers. I prefer to code on my own system. This allows me to use my chosen tools and work in a comfortable and efficient space.

Possible differences and deviations from production environments are a downside of this. I have struggled with unearthing strange failures due to differences, such as differing or missing libraries, etc.

Virtual machines are useful in allowing work to take place in a clean environment, mimicking production far more closely. I have relied on VirtualBox for this purpose. The sad part is that I find working with both the GUI and the VBoxManage commands tedious and slow.

Recently I have embraced Vagrant and discovered that it greatly improves the ease of production like testing.

These steps are the steps to get up and running swiftly on Ubuntu 13.10:

# use the newest VirtualBox and Vagrant rather than what is
# in the repos
cd ~/Downloads
curl -O http://download.virtualbox.org/virtualbox/4.3.8/virtualbox-4.3_4.3.8-92456~Ubuntu~raring_amd64.deb
curl -O https://dl.bintray.com/mitchellh/vagrant/vagrant_1.4.3_x86_64.deb
dpkg -i virtualbox-4.3_4.3.8-92456~Ubuntu~raring_amd64.deb 
dpkg -i vagrant_1.4.3_x86_64.deb

Now that you have the basic software installed I recommend also adding the plugins for chef and vbguest. That latter one will make sure that the guests will stay up to date on the VirtualBox guest additions.

vagrant plugin install chef
vagrant plugin install vagrant-vbguest

Now vagrant is ready to fire up boxes for you. The instructions on line refer to images on line, which can be found at http://www.vagrantbox.es/. It is possible to just point to those directly, but the downloads are sizable and so I just grab the one I need and save it locally for quick reuse.

cd ~/Downloads
curl -O http://cloud-images.ubuntu.com/vagrant/saucy/current/saucy-server-cloudimg-amd64-vagrant-disk1.box

The saved image can now be used to fire up the hosts easily. I normally place them in their own directory.

mkdir test-box
cd test-box
# this will create a file called 'Vagrantfile'
vagrant init test-box ~/Downloads/saucy-server-cloudimg-amd64-vagrant-disk1.box
# now start the box, which can take a while the first time as it
# will update the VirtualBox guest addtions, etc. subsequent boots
# are quick
vagrant up
# connect to it and do your thing
vagrant ssh

The Vagrantfile can be modified to expose ports for listening services and adjust system settings such as memory etc. It's worth looking into.

Additionally, the Vagrantfile sets up a VirtualBox Shared Folder that shows up in the guest under /vagrant and others can be added.

Word of warning: if you are using the shared folder to serve files via nginx you will want to add the sendfile off; directive or you will see what is described here.