This is the first in a series of articles I intend to write on Vagrant and why its f*cking awesome!
Vagrant is probably the most exciting thing to happen to local development environments since I first installed Xampp on my Windows machine and saw I had a working Apache environment all set up and ready to process pages just like the actual internet. No more FTP’ing files to a remote server before I can see if the two lines of code I changed actually worked!
Mimicking the production environment
So, if we have a working Apache setup, why bother spending time learning how to set up Vagrant? Well there are a few reasons, the most important of which is that if you are on Windows or Mac, you are not developing your website / web-app on anything like the environment in which it will be placed for production. Most hosting that you can buy will be within a Unix environment, which means, at its simplest, files and folders are case sensitive and that the directory separator is different.
Then there are issues around the software you are using. On your local Xampp install, you might be running PHP 5.4, but on the web host you only have access to PHP 5.3. That doesn’t sound like a massive difference, its only 0.1 versions higher, but in PHP-time, thats a couple of years of development, meaning that you wouldn’t have access to Short Array Syntax, Traits or the 20% speed increase in 5.4.
Then there are smaller issues, like not being able install extra PHP libraries such as ImageMagick on anything but a *nix machine without all kinds of ball ache.
Personally, I think Vagrant most useful feature is project isolation. Vagrant uses a single, isolated environment for each of your projects, which means you can do anything you like, install crazy packages, completely different set ups and configurations, knowing that if worst comes to worst, your Vagrant Box can be recreated and all your other projects as well as your actual development machine are safe.
Working as a team
Anyone who has worked as a member of a team developing a website or Web app might have run into a very common problem; everyone has a slightly different local setup. The differences could be anything I’ve already mentioned, but if one person pushes changes, which work perfectly on their machine, it’s not guaranteed to work elsewhere.
Because Vagrant is designed to be reusable, by including the Vagrant configuration file in your Git repository or just emailing it, each member of the team is guaranteed to have the exact same setup, which will, hopefully, closely mimic the production environment. What works for one, works for all.
So how does Vagrant work?
Vagrant sits on top of Virtual Box, software which allows you to install and run multiple operating systems on your machine, each stored and run from its own Box file. Vagrant uses the technology in place within virtual box to create and provision each development environment letting it do all the hard lifting. When the environment is created and running, Vagrant forwards ports and requests from your browser to the operating system virtual box created. So instead of your local Apache installation processing your website and returning it as HTML, the request is instead sent to the VirtualBox Guest OS. So if your VB Guest OS is Ubuntu 13.10, then thats the environment which deals with he request, with all Linux’s idiosyncrasies and security baked right in.