Vagrant - A Developer's Best Friend

Vagrant - A Developer's Best Friend

Have you ever broken your development environment and dreaded of the hours it will take to rebuild your environment? I certainly have, on multiple occasions. This is where Vagrant comes to the rescue! Vagrant is an excellent tool to build disposable, lightweight development environments. It leverages the power of Virtual Machines (VM) to allow you to easily build customizable images, so you can quickly bring up a custom environment.

To start, simply download the Vagrant client and a 'provider' like VirtualBox or VMware. I would recommend you use VirtualBox as your provider as we start out, as it is free and works on every OS. I'll assume we're using VirtualBox from here on out.

After having installed both Vagrant and a provider, you can start the Virtual Machine with two commands:

$ vagrant init hashicorp/precise32
$ vagrant up

That's it! You now have a VM running. Specifically it is using the image hashicorp/precise32 which is built on Ubuntu 12.04 LTS 32bit.


I mentioned that you can customize your environment and this is where a Vagrantfile comes in to play. A Vagrantfile is simply a bunch of Ruby commands used to configure the VM. You can generate a Vagrantfile with the command vagrant init. If you poke around in the file, you'll notice that is has a bunch of sample configuration that can be used to further customize the VM.

One important block you'll see is the config.vm.provision line. This is where the magic happens! Here is where you can customize your VM to install various pieces of software, run setup scripts or, really, do whatever you want. You can see some of the most basic examples look like

  config.vm.provision "shell" do |s|
    s.inline = "echo hello"

This will simply spawn a shell when the VM is provisioned and run the command echo hello on the VM. The implications are very exciting! From here, you can use a provision block to invoke package managers, run external scripts or even invoke other build management tools like ansible or chef.


Now that you have your VM customized, you'll want to be able to access it from the outside world. This is where comes into play. For example, if I want to make port 80 on my VM accessible, I can simply put the following inside of my Vagrant.configure block:

1 "forwarded_port", guest: 80, host: 8080

This will forward port 80 on my guest (VM) to port 8080 on my host (machine running vagrant). You can port forward to your heart's content, allowing various services access to the outside world.

There are some other networking options, like setting up private and public networks, check the documentation for more info.

Synced Folders

One of the other niceties of Vagrant is the ability to sync folders across machines. This makes it easy to transfer files back and forth without the need and hassle of using commands like scp and also means you can write your code on your host, using a graphical editor, perhaps, and run the code on the guest. To set this up for a VirtualBox Provider:

  config.vm.synced_folder "src/", "/srv/website"

This will sync files from src/ on the host to /srv/website on the VM. One of the niceties of Vagrant and VirtualBox is that you actually get this functionality for free. If you do an ls of /vagrant you should see all of the files from the host where you you ran vagrant up. There are further instructions on synced folders here.

The above should get you started on building your custom development environment! Feel free to read up on Vagrant here to get a better feel for the power of being able to build your own custom development environments.

comments powered by Disqus