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:
- It’s hard to isolate environment for different projects.
- It’s hard to replicate the set-up for fellow developers, designers.
- Backup is either all or nothing. Replicate the disk or install from scratch.
- Sometimes you screw up installing something and can’t go back.
- Your development env is too different from production.
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 environments. — vagrantup.com
Vagrant allows us to use a lightweight virtual machines to sandbox and share our development environment.
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.
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:
- Download official Ubuntu images built by Vagrant team over here
- Download one image built by the Vagrant community on vagrantbox.es
- Build one from a distribution iso using Vewee
Choose the first if in doubt.
Now register your base box, example for Ubuntu Precise 64bits:
vagrant box add precise64 http://files.vagrantup.com/precise64.box
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?
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.
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:
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!
On the next post of this series, I’ll show how to use Chef to:
- Have those vm environments versioned. So everyone is up to date.
- Deploy our environment to Amazon EC2 (production site) using the same Chef recipes, so we have prod/dev parity.
And we’ll move on with Vagrant:
- Defining a cluster set-up that represents our infrastructure.
- Configuring ips, hostnames so things look pretty.