Setting up a web development environment has always been something of a pain. Sure, we web devs don’t have to deal with compilers and libraries and whatnot to get coding, but when it comes time to run your stuff, you need a server.
Assuming you don’t have endless resources to set up development servers remotely, and considering the pain of pushing code to a remote over and over while you write it, this has usually meant building your own server to test on. If you’re a linux user, you’ve probably gone through the steps of installing Apache and MySQL locally, a php interpreter, and so on, turning your desktop into a hybrid workstation-server. Maybe you’ve moved on from that and started using XAMPP or MAMPP or some other canned, all-in-one solution.
Then, once you’ve got everything dialed in how you want it, it’s time to start a new project and now you have to juggle vhost configurations. Then you need to work on some legacy code that needs PHP 5.2 to function, so you have to juggle multiple server setups. Then you decide to dabble in something like Rails or Django, so you install more dependencies and more libraries on top of every php extension and PEAR module you already installed for those PHP projects. Then you get a new computer, and uhhhh…
I went through variations on this theme for years, then I discovered Vagrant, and I’m never going back.
To understand why Vagrant is an awesome way of doing things, we need to look at a few concepts that Vagrant brings together.
First, we get away from the messy pile of libraries, interpreters, and configurations on our local machine by using virtual machines. Using VMs lets us avoid cluttering up our workstation by setting up different servers for different setups with all the dependencies we need. If you were to use, say, Virtualbox and Linux guests, you get free servers! Go ahead and have a PHP 5.2 server, a PHP 5.4 server, a Rails server, a Haskell-on-a-Horse server; whatever strikes your fancy, really. After all, free servers!
Cool, right? We can go farther. What if you’ve got two projects, one in Drupal and one based on Laravel, and you don’t want to worry about them sharing the same environment? No problem, let’s just make our VM setups reproducible. FREE SERVERS, RIGHT?! Turns out there’s several excellent tools available for provisioning multiple servers with the same configuration, like Puppet and Chef. Bringing our provisioning software to the party makes it fairly simple to make multiple identical copies of the same server environment.
Now for the good part. If you’re like me, you probably don’t want to fuss around with having to learn another piece of new software like Puppet or Chef to set up your local development environment. You probably don’t have endless hours to configure different virtual machines for different projects. Here’s where Vagrant comes in. Vagrant allows you to describe and manage these setups in a fairly compact fashion that can be distributed.
Turns out, lots of people have already set up application-specific Vagrant profiles and kindly decided to share them with the world, open-source style. Setting up a development server for WordPress or Drupal becomes as easy as cloning a repository and running
vagrant up. Not finding what you need on GitHub? No problem, just use a Vagrant generator tool like PuPHPet to build whatever you need for your CodeIgniter project running on HHVM with a few clicks.
But wait, it gets even better! You’ve probably worked on a team before (I hope), so there’s a good chance you’ve seen a fractal of the awful local development setup I started this article with where everyone’s got their own homebrew local development environment. Sally’s code works fine on her laptop, but crashes for Jim, and Bob can’t run code from either of them. Moving from dev to test always reveals platform-specific bugs too, somehow. There’s a good chance your sysadmins or devops team are already hip to something like Puppet. With Vagrant, we can borrow their configuration and Sally, Jim, and Bob’s local development platforms can be exactly the same as test and production.
At work, I’ve created a vagrant profile for Drupal that closely matches our hosting environment’s test and production environments. The VM image and Vagrant profile are hosted locally, so onboarding a new developer takes two steps: Clone the Drupal-dev git repo to your machine and start Vagrant. Poof, you’ve got a local server that looks like production, just like magic. That’s why I think you, too, should use Vagrant and never look back.