Google

The power of Virtual Machines

It has been a few weeks I have been using Vagrant at work and on personal projects, and I have become a huge fan. Vagrant is a tool that allows you to script the creation of Virtual Machines (VMs). Using VMs and sharing them is of great value to developers: you can make sure that everyone in the team is running the exact same thing, and new team members don’t need to spend time on setting up their development environment. VMs also allow something that researchers (should) care deeply about: repeatability of experiments and results. My choice of virtualisation application goes to VirtualBox, which is a free and open source.

Sharing and versioning VMs

However, one problem that remains is that of sharing VMs across the team, and versioning VMs as the project evolves. Vagrant solves this problem by allowing you to script the creation of VMs. You can then add the script to your versioning system repository.

Vagrant works with “boxes”, which are VMs, stored in a certain format, with stuff in them already (an operating system, programmes, files). There’s a number of boxes that you can download on the internet and that may be tailored to your needs. You can also make any VM into a box after including (manually) what you need (see examples below). Vagrant uses a given box as a base for creating a VM, so the first thing to do is to add boxes to your Vagrant install.

Getting started with Vagrant

First of all, install VirtualBox then Vagrant (links above).

The following command downloads and adds a box (under the name ‘lucid32’) on which is installed a bare bones Ubuntu 10.04 (Lucid) 32-bit:

 

vagrant box add lucid32 http://files.vagrantup.com/lucid32.box

Vagrant works relative to the directory from which you call it. The following command creates a default config file (“Vagrantfile”) in the current directory:

 

vagrant init

The following command creates the VM, based on the instructions in the Vagrantfile, or boots the VM if already created, or does nothing if VM already booted:

 

vagrant up

I find it most simple to tell Vagrant to launch a shell script in which I write all the instructions to set up the machine upon creation — see example Vagrantfile.

You can then log into the VM as the ‘vagrant’ user (no password needed), through ssh:

 

vagrant ssh

Once logged, and if needed, you may log as root with:

 

sudo su

You can access at /vagrant all the files that are on the host’s system in the directory from which vagrant was launched.

When you’re done with the VM, you can either shut it down:

 

vagrant halt

Or you can remove it altogether:

 

vagrant remove

In any case, you’ll bring it back up with a ‘vagrant up’.

Example uses

I have created two boxes of my own, based on lucid32, one with Matlab and Flash Media Server. The installation of these applications cannot be scripted because there are dialogs asking you to configure your installation and to accept the software license agreement, so I have done the base installations manually and created boxes from there so that all future VMs I’ll create which will include this software will be scripted.

When you run servers on your VM, it’s important to set port forwardings (as we did in the example Vagrant file linked to above). Make sure that when you search for Vagrant boxes on the internet (for example one for Rails app development), you also get a default Vagrantfile for that box… Enjoy!