Have you ever slaved over a website? Sweating every line or code and pixel to get it perfect, rushing for that deadline only to deploy it to your server and having nothing happen? Vagrant can help us stop that happening to us again.
We’ve all been there. You’ve built something magnificent, but you built it using closures in PHP 5.4 and the live server only has 5.2 installed. Crash. And. Burn.
This is a very common thing in the world of web development and especially WordPress development, which can be looked down upon in some circles as being a little unprofessional. What usually happens is the developer takes their local machine, installs MAMP or WAMP on it once and leaves it alone, because it works. They don’t get updated, they don’t get patched, they don’t get anything. Eventually – the developer writes something that works fine on their computer but just point blank refuses to run on production or in staging.
This is all avoidable with a little professionalism and some common sense.
Vagrant is a Command Line interface for Virtual Box. It allows us to create and manage new computers that exist only within our computer. It does this through Virtual Box’s infrastructure and a few configuration files of its own. It gets particularly interesting when we add tools like Puppet, Chef and Ansible to install software onto our servers for us.
This combination of virtual machines, provisioning and configuration means that we can create exact copies of servers running on our local machines, so mismatched versions of PHP, MySQL or anything else are gone and forgotten. Never experience an unexpected version clash again. With a provisioner, we can also quickly create and throw away machines with little or no effort. Imagine building a WordPress theme or plugin and being able to test it easily on different versions on PHP or MySQL just by changing a version number in a configuration file.
Getting Started with Vagrant
Let’s first download VirtualBox and Vagrant. Install them both.
In your terminal change directory into the project, you want to work on and initialise vagrant in it.
vagrant init hashicorp/precise32
This will create a vagrantfile in your directory and configure it to the use the hashicorp/precise32 box.
Boxes are pre-made images of machines. These speed up Vagrant as it just loads the image into VirtualBox, rather than installing the machine manually, which would take forever. There are hundreds of boxes out there with various configurations.
Running vagrant up now will start VirtualBox, load the machine, and run your provisioner. Vagrant will download and cache anything it needs to do this as it goes. Your first vagrant up can take a while!
vagrant up
Congratulations, you have now got a new virtual computer running on your computer through VirtualBox and Vagrant.
Working With Vagrant
Now your machine is running, you might want to do something with it.
Access
We’ll start by proving it’s really there. You can SSH into your new computer with
vagrant ssh
You can now do anything on this machine that you like or could do normally through the terminal. If you break something then just shut down the box and recreate it, no harm done to your computer.
Shut it down
vagrant halt
You can stop the virtual machine running should you no longer need it. You can start it up again with Vagrant up and it will be just as you left it.
Get rid of it completely
vagrant destroy
Completely destroy the Virtual machine and all traces of it from your host computer.
Put files on it
Your new VM isn’t much good unless you can put files on it. By default, vagrant sync your project directory to the /vagrant directory on the virtual machine but this can be configured to say a web servers htdocs folder or anywhere else. Open your Vagrantfile and take a look in there. Your Vagrantfile contains all the configuration options for your Vagrant instance.
Vagrant And WordPress
Vagrant just provides a machine to run things on. If you have any experience setting up servers for projects you’re going to know exactly what do to here. Install Apache, PHP and MySQL as your normally would on any machine and then add WordPress to your project as you normally would. By default, it will be synced to the /vagrant folder on your virtual machine, so configure Apache/Nginx to look for files there and you’re good to go. Install WordPress as normal.
It’s that simple. WordPress is now running on a Ubuntu Linux server, that is running on your machine and can be configured any way you want it without having to rebuild an entire machine. As we said before – this is great for emulating production environments for local development.
Vagrant is a powerful tool for creating and managing Virtual Servers that simulate production environments for local development. We’ve only scratched the surface of what it can do in a serious development environment. We’d love to hear what you think of Vagrant and how you use it in your development process, particularly if you use it with WordPress, Comment below and let’s start a discussion.