A brief overview of Vagrant

By : Dheeraj Sayala

Vagrant is a very good wrapper around Oracle's VirtualBox. It makes life easier for web developers and the like by providing a nice command-line interface to build, manage, provision, use virtual machines.

Why should you use Vagrant?

  • You may want to install different versions of software without worrying about conflicts
  • You don't want to remember which services to start/stop when you start working on a project
  • The version of Operating system you use can be different from the one on the server. So, you want a way to emulate the behavior on server
  • You want to develop locally without latency as with servers, but run everything in a server-like environment
  • You don't want to sit and type through the same steps of installation etc when you deploy to server
  • A new person joins your team and you want him to get up and running quickly without wasting a lot of time
  • Won't it be great if you can rollback to a snapshot easily when you mess things up?

Installation

Before installing vagrant, we need the following minimum requirements installed - Oracle's VirtualBox, Ruby, Rubygems

This is how easy it is, to install and get started with Vagrant on Ubuntu -

sudo apt-get install ruby rubygems

sudo apt-get install virtualbox

sudo gem install vagrant

Once everything is installed, just typing vagrant will show you all vagrant commands. Before discussing these commands, we should understand some basic terminology. A box is a packaged file which contains the files required for a virtual machine. A virtual machine is used to run an operating system within your existing OS. You can use a base box to create a virtual machine with a basic OS. vagrant box is used to manage boxes and vagrant package can be used to create a box from an existing virtual machine.

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

The first command downloads a box from the URL and saves it as lucid32 on your machine. Vagrant uses boxes like this to build and run the virtual machine. The second command creates an example Vagrantfile with a good amount of sample provisioning code commented. The third command, vagrant up, starts the virtual system.

Like a normal operating system, you can

vagrant suspend # sleep
vagrant resume  # back from sleep
vagrant halt    # shut-down

The sweetest of the commands is vagrant ssh which lets you ssh into the guest VM. You can easily get rid of this virtual machine and free up some space with vagrant destroy. All these commands should be run from the directory containing Vagrantfile.

Provisioning

A base box contains nothing but the OS and some files. If you want things like apache2, mysql, memcached installed on first boot without much intervention, you can provision using one of the supported provisioners. Chef and Puppet are popular provisioning tools with good communities. OpsCode cookbooks is a good collection of chef cookbooks by the creators of chef and the community. I found Dougal Matthews' chef_recipes good for django, postgres installation. More about provisioning here.

Some important features

  • You can enable Port forwarding asking the guest VM to forward some ports to the host. This way you can run the server from the VM and use the browser and editor on host. You can also assign a Static IP for your VM.
  • You can share files, directories between the VM and your OS. These files will be in /vagrant on the VM by default. Filesystem intensive tasks like version control are generally faster on the host system
  • When you share a box with team members, you don't even need to repackage and distribute. You can even share the Virtual Machine disk file.

I found Vagrant to be a very useful and it has already saved some 20 developer hours on one project. It seems to be a good idea to use Vagrant for every project we work on. We will discuss basic provisioning and workflow in another post.



Can we help you build amazing apps? Contact us today.

Comments

Jason 24th April, 2012

Thank you useful overview.

commmenttor
© Agiliq, 2009-2012