How to use Vagrant for WordPress Development

What is Vagrant?

Essentially, Vagrant is a wrapper around virtualization software (like VirtualBox) used for virtual development environment configuration. Using Vagrant commands you can easily run, stop or reboot a development server with any OS distribution you need with your application running on it. Using Puppet you can modify the virtual server instance by adding various packages (dependencies) to it (like PHP, GIT, MySQL etc.). If you want to go deeper and learn a lot more about vagrant check out their site .

Development is production

Here’s a sentence we’ve all heard a million times: “But, It works on my computer”. In most cases it’s indeed true. By using Vagrant we can eliminate that problem. Vagrant and Puppet allow us to recreate a production environment locally which means that there won’t be any surprises once we deploy out site or application to the staging or to the production server. Since development environment packages are defined via configuration files we can add those files to the project version control as well. This means other team members can run the project on the exact same environment as you do.

Use case

So, let’s say you’re developing a WordPress site that will be hosted on WordPress VIP hosting. Since WordPress VIP hosting has a set of rules that define how your site should behave it would be cool to have a local development environment that acts in the same way. Luckily there’s such a thing and it’s called vip-quickstart, It’s based on Vagrant and Puppet.

In order to explain how Vagrant helps you in your development process we’ll see how to create a new VIP Quickstart project locally. First you’ll need to download and install VirtualBox (link to virtualbox), Vagrant (link to vagrant) and Git (link to git). Once installed we can now clone the vip-quickstart repository into our project directory (that will be created by the git clone command).

git clone git@github.com:Automattic/vip-quickstart.git wordpressvip

This command will now clone vip-quickstart repospory in our WordPressvip folder. Once cloned use terminal to navigate to the newly created project folder and run:

bin/vip-init

This will now initialize the new WordPress VIP development environment. Since this is probably the first time you’re booting up this development environment it will take some time for Vagrant to download and install all the required packages (including the OS). One more thing. Keep your eyes on the terminal since it will prompt you to enter your password a few times.

Once the provisioning (dependency download and installation process) has finished VIP Quickstart “wizard” will ask you to enter a hostname for your new development environment. You’ll also be provided with the default one. Click the ENTER key and Boom! That’s it! You now have a fully functional development environment that is accessible via the hostname you’ve entered a few seconds ago. Now you can start the site development just like you would normally. Since Vagrant creates a shared folder for your project your local files will be synced to the virtual machine automatically (upon file creation/saving). VIP Quickstart installs WordPress multisite with a few suggested plugins including VIP theme and plugin scanner.

Creating custom development environment

By using online solutions like puphpet you can easily create your own development environment(s) that match the production environment your site will be running on. All you need to do is fill in a few input fields that’ll be used for server configuration and click download. Now you can use downloaded files as a starting point for your new project development.

Conclusion

Having a local environment that is the same as the production environment is a nice thing to have. It provides you some form of security during the development process. Equally it also eliminates the need for applications like MAMP, WAMP, XAMPP etc. Once you remove your virtual box instance you are 100% sure that you’ve removed all of the software and services that were installed on it. So go ahead, knock yourselves out and use Vagrant in your next project. Believe me, you won’t regret it.

About Domagoj Gojak