Vagrant & Chef for development environments. Part 1

Do I really have a problem?

I’m pretty sure you’re cozy with your current dev environment. But let’s take a look at what might be a problem:

If you don’t relate to any of this problems you can stop here.

How do we solve all this?

Create and configure lightweight, reproducible, and portable development

Vagrant allows us to use a lightweight virtual machines to sandbox and share our development environment.

First things first. Download Vagrant from the official website. As of writing you should be getting version 1.2.7. Beware that Vagrant used to be distributed through Rubygems, but not anymore.

Vagrant is the magic that scripts our virtual boxes, but it needs a vm provisioner. So also download and install Virtual Box (free). VMware is also compatible, if you’re wondering.

Base boxes

In order to start working with Vagrant, we need a base box, a Linux virtual machine image to serve as our starting point. Hint: you’re just going to need the URL.

You can either:

Choose the first if in doubt.

Now register your base box, example for Ubuntu Precise 64bits:

vagrant box add precise64

You can list the installed boxes with:

vagrant box list
precise64      (virtualbox)

Create your playground space:

mkdir my-server && cd my-server

Now, let the magic begin! Tell Vagrant to set-up:

vagrant init precise64

This will create a Vagrantfile, which allows you to share folders, forward ports and other things. For now, lets leave it as it is. But take a look at it later, you’ll find examples of useful configurations.

Let’s see Vagrant in action, shall we?

vagrant up

This might take a while for the first time, because it will copy the base box if it haven’t already and because it will clone that base box for this particular environment.

Everything should be ok for now, let’s take a look at our env.

vagrant ssh

This will ssh you into the newly created box.

The vm will keep running until you issue one of this commands:

vagrant suspend | halt | destroy

You can always resume or start it back again:

vagrant up

Sharing your box

After you installed everything you wanted, you can package the virtual machine state into a virtual machine image, so others can use too:

vagrant package # make sure you halted the vm

This will create a .box file of that virtual machine, so you can share with your work mates. All they have to do is follow the previous steps using your box file!

Next steps

On the next post of this series, I’ll show how to use Chef to:

And we’ll move on with Vagrant:

Cheers :)

Bruno Buccolo

Follow me on Twitter! @buccolo

© 2011– Bruno Buccolo | Made in São Paulo ☂