The following post is by Jason Witt, a regular around here on topics like WordPress development. This time Jason introduces us to a development prerequisite: the development environment itself. There are lots of ways to level up behind off-the-shelf app solutions, including scripting your own setups.
One question I hear again and again from new and experienced developers alike is “What local development setup do you use?” As a WordPress developer that needs, at a minimum, Apache, MySQL, and PHP, I would answer “You should give MAMP/XAMPP/WAMP a try, it’s easy to use and works great”. I still think that, but the more I learn and develop my skills as a developer, I no longer think MAMP/XAMPP/WAMP can support my needs.
I think most of us have uttered these words at least once: “Well, it works on my local server”. What can you do? When the live server isn’t the same as your local server, it’s not as though you can predict an issue on the live server. Vagrant can help solve that problem.

Let me introduce you to Vagrant, a tool that allows you to easily set up a virtual server environment on your local machine. In their own words:
Vagrant provides easy to configure, reproducible, and portable work environments built on top of industry-standard technology and controlled by a single consistent work flow to help maximize the productivity and flexibility of you and your team.
With Vagrant, you can set up your local machine with the exact same environment as your live server. From the OS, to the web server, to the database. Now you can build your web app or website with the utmost confidence that it will run just as well on the live server as it does on your local server.
Prerequisites
There are a few things you need to have before you can start working with a Vagrant box.
First, you need a virtual machine installed on your computer for Vagrant to run the virtual boxes. The two most popular virtual machines are VMware Workstation and VirtualBox. VMware is a commercial product and VirtualBox is a free, open-source product.
Next, you need git installed if you want to clone Vagrant Box repos from GitHub or Bitbucket. You can find many Vagrant boxes distributed on GitHub and Bitbucket, and having git to clone the repos is an easy way to get the Vagrant Boxes. I won’t go into installing git in this article, but you can learn how install git here. You can also find a bunch of great git related articles right here on CSS-Tricks.
You also need to have SSH installed to be able to SSH into your virtual box. If you’re running OSX or Linux, you probably already have OpenSSH installed. If you’re on Windows, you can follow the tutorial here to get SSH installed on your PC.
Finally, you need a command line tool. Yes, I know the command line can be scary, but trust me; you only need the most basic of command line skills to use Vagrant. If you’re on Windows, you can use the Command Prompt. On OSX you can use Terminal.
Installing Vagrant
All right, it’s time to get into the meat and bones. Head over to the Vagrant website to download and install the latest version of Vagrant for your operating system.
After you install Vagrant, you can check to make sure it was installed correctly by typing the following command:
vagrant -v
If Vagrant was properly installed, you should see something like this:
Vagrant 1.7.2
Now that you have Vagrant installed, let’s find and set up a Vagrant Box. There are three popular ways to download a Vagrant Box. Downloading a box directly, cloning a box from GitHub or Bitbucket, and adding a box from the Vagrant Cloud.
Getting the Boxes
For the purpose of this tutorial I’m going to use a pre-built Vagrant Box called Scotch Box. Scotch Box is a basic LAMP stack, that, once installed, will give you everything you need to start developing with those dependencies.
Scotch Box is set up for use on the VirtualBox virtual machine. If you’re running VMware, you can find a Vagrant Box to download at vagrantbox.es. There are also a ton of other great boxes for VirtualBox there, too.
If you want to download Scotch Box, head over to the GitHub repo at https://github.com/scotch-io/scotch-box, and download the repo. Once it’s downloaded, navigate to the directory where you want to install your Vagrant Box. Create a directory where you want the box located, then extract the contents of the zip file into that folder.
Cloning the repo just as easy. Navigate to the parent directory where you’re going to install the box using your CLI. Then, use this command to create the Vagrant Box directory and clone the box:
git clone https://github.com/scotch-io/scotch-box.git my-project
Make sure you change my-project to what you want to name your Vagrant Box’s directory.
The last way to get a Vagrant Box is to grab it from the Vagrant Cloud. In my opinion this is the easiest way to set up a Vagrant virtual machine.
First, add the box. You do this by using the add box
command, followed by the box’s name. The box’s name is usually formatted like: something/anotherthing
. The name for Scotch Box is scotch/box
. To add Scotch Box, use the following command:
vagrant box add scotch/box
This command downloads the Vagrant Box, making it available to use with Vagrant.
After the box is downloaded, create the directory you want the box to be located in and navigate into that directory in your CLI. Once there, initiate the box and add the Vagrant file to the directory with this command:
vagrant init scotch/box
Starting Up Vagrant
At this point, you have the Vagrant files downloaded and in the correct directory. We want to install all components available in the box, so navigate to your newly created Vagrant Box directory and start Vagrant for the first time with the command vagrant up
.
vagrant up
Now wait….
The first time you run a Vagrant Box, it needs to download and install the OS, the server, and all the additional components. The amount of time for this varies depending on the Vagrant Box you choose and your connection speed. Scotch Box is pretty lightweight and takes me about 10 minutes. I’ve also used a Vagrant Box called Varying Vagrant Vagrants, which is an excellent box for WordPress development, but it’s pretty big. It takes me the better part of 20-30 minutes on the initial vagrant up
.

Using Vagrant
Now that you have the Scotch Box Vagrant Box installed and running, what can you do with it? Well, you can do anything you can do on any other LAMP stack server. Scotch Box has a pre-configured web server that uses the IP address http://192.168.33.10/
, so if you point you browser to that address you’ll see this:

Everything being rendered by the web server can be found in the “public” directory. This is going to work just like any other web server you’ve ever used. In addition to the Apache web server, Scotch Box also installed these components that are ready to be used:
- Vim
- MySQL
- PHP 5.5
- Ruby
- Git
- Screen
- Composer
- Laravel Installer
- cURL
- GD and Imagick
- Mcrypt
- Memcache and Memcached
- NPM
- Grunt
- Bower
- Yeoman
- Gulp
If you want to use a virtual domain instead of the 192.168.33.10
IP address, you can edit your hosts file and add this line:
192.168.33.10 my-domain.dev
Vagrant Commands
Vagrant lets you control your server from the command line. You can find a full list of commands here, but let’s get you started with the basic commands.
vagrant
All Vagrant commands begin with vagrant
. If you run the vagrant
command by itself, it will list all available commands you can use.
vagrant up
The vagrant up
command is used for starting your virtual box. After you run this command your virtual box will be running and ready to use.
vagrant ssh
The vagrant ssh
command allows you to SSH into your virtual box and gives you access to a shell. This is helpful for accessing MySQL and performing commands such as mysqldump
to backup your databases.
vagrant suspend
To pause your machine you can use vagrant suspend
. This is useful if you want to stop your machine without having to completely shut it down.
vagrant halt
To shut down your machine you use vagrant halt
. This shuts it down and retains the machine’s current state.
vagrant destroy
To turn off your machine and completely start over fresh, use the command vagrant destroy
. After a vagrant destroy
command you’ll have to go through the initial vagrant up
again.
Conclusion
The world of web development is far more vast today than it was even 5 years ago. Having the ability to develop on a local server that mirrors your live server gives you a more stable development environment and the confidence to push your projects live without the worry of it breaking immediately. Tools such as Vagrant are the future of web development, and by using these tools you’ll grow as a web developer and help mature the landscape of the Internet.
What’s the easiest way to create & connect to a mySQL table for a local WordPress install in a Vagrant environment? I’ve just used phpMyAdmin through MAMP up to this point
The easiest way to connect to your database whether local or live would be using Sequel Pro – http://www.sequelpro.com/
Once you have the app up and running just create a SSH connection for your local vagrant host.
If you need more information on this visit: http://www.sketchtowordpress.com/
Contact a developer there via the contact page (they provide some free support for vagrant and wordpress). Awesome people who are more than happy to help you.
I am also using vagrant but its tooooooo slow. Is there any thing to update date in my PC configuration because i have 8BG RAM and i have allocated 6GB to vagrant using vagrantbox file.
Hi Atul,
Edit the Vagrantfile and add these lines with your needs inside Vagrant.configure…:
Also, allocating 6GB out of your 8GB may be too much, your whole system may be running out of RAM.
I personally manage my Vagrant Dev Box using puphpet (www.puphpet.com), I believe Chef is another popular alternative but I haven’t checked it out myself.
The nice thing about puphpet is that it allows you to easily configure custom boxes with sensible defaults. Picking a specific OS with a particular stack and easily configurable vhosts make it very important to my dev environment.
Unfortunately there are some very real drawbacks to using Vagrant.
I’ve had it happen more than once that it suddenly doesn’t find my box anymore and just tries to reinstall a new one (a reboot sometimes solves this). At times there are compatibility issues with the configuration file you’re using, the version of vagrant and puphpet’s version, which leaves you with no other option than to just start all over again with a new box (but hope you’re techsavvy enough to get your database back at that point).
It’s not as fast as it should be either, I work mainly with Drupal and have had a lot of issues with performance. If you google you’ll mostly find that slowness is related to the shared folders and that you should use NFS. But then other problems pop up, like being unable to give your apache user sufficient rights to write to a folder for example. I’m now using the default sharing option but have given my VM 4GB of ram and 4 or so CPU cores (I’m running this on a first gen 15″ retina macbook pro) which seems to have helped a lot, but it’s still not blazingly fast.
Most of these issues are likely related to my lack of knowledge of Linux (and perhaps Ruby). But I’m not a full stack engineer, I’m a front-end developer working with Drupal that needs an easy to set-up local dev environment.
I’m still staying with Vagrant for now, as I feel it’s one of the best options out there. But it’s not for everyone and might cause you more problems than it solves if you’re not careful.
Hi John. Check out our (Penn State Drupal User’s Group) Nittany Vagrant distribution. It is made specifically for Drupal folks looking to rapidly set up a local development environment. Let me know what you think!
https://github.com/psudug/nittany-vagrant
Forgive me if this is a stupid question. What’s the best way to go about if you have multiple dev sites? If I read that it takes 20-30 minutes on the initial
vagrant up
, then multiple boxes doesn’t seem the solution.You can put them in separate folders and use the base-meta tag in order to have your relative paths work.
Or you can configure your vhosts (either manually or by using a tool such as puphpet) where you define that certain folders are linked to certain web addresses. The IP of your vagrant box is always the same, so one the vhosts are configured you can edit your hosts file to include them e.g.:
192.168.0.156 http://www.siteone.dev siteone.dev
192.168.0.156 http://www.sitetwo.dev sitetwo.dev
I have no idea what you mean with this…
Looking at puphpet now, but cannot say that I understand much of that.
Anyways, thanks for taking the time to reply. I guess I need to read some more tuts on Vagrant.
The HTML base-tag (https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base)
If you work with subfolders and you don’t use vhosts you will access your site via http://ip.of.vagrantbox/subfolder which might cause issues with relative links. By specifying your basepath this will be avoided.
But I recommend just looking into the vhosts implementation.
Puphpet looks very overwhelming at first, but is mostly preconfigured. If you just download the default configuration you’ll get an ubuntu 14.04 box with nginx, php and mysql preconfigured, accessible via http://www.awesome.dev I think.
Just look at the awesome.dev vhost and copy the same configuration for other vhosts you’d like to create.
The hosts file is a file on your own system (so not in the vbox) that you need to configure to tell your system that a certain domain should resolve to a certain ip.
Hi John,
In regards to puphpet, when I already have Vagrant installed isn’t the ubuntu 14.04 box with nginx, php and mysql preconfigured completely redundant?
Vagrant is simply an environment for managing different ‘boxes’. Vagrant will download a box (the ubuntu 14.04 install in this case) and install the appropriate development stack based on the vagrantfile and the other files generated by puphpet (or manually written).
Vagrant provides an interface that allows you to easily communicate with these boxes and rapidly discard/reinstall them.
With puphpet for example: if I need an extra dependency for php or a new vhost, I simply drop my config.yaml back in puphpet, change the configuration and download the updated configuration. I then run ‘vagrant up –provision’ and my box is updated with these new settings.
Hello. There is Laravel Homestead, but it’s for nginx. I have created a vagrant box that uses Apache. Feel free to try it: https://github.com/ronilaukkarinen/jolliest-vagrant
Thanks Roni, I will check it out.
That’s just the first time you run vagrant up. Like with VVV it installs a bunch of add ons like Grunt with a ton of modules, and the WordPress Test suite among other add ons. One you run that first up it only takes a moment to start Vagrant up.
It doesn’t take 20-30 minutes. That is only for downloading the box. After that, it’s like 10 seconds to boot up.
Scotch Box is currently intended to host one project per VM. So you can run multiple so long that you
suspend
VM before starting a new one. This is out-of-the-box, technically you can install vhost to setup multiple sites if you wanted to though.Oh man, I wish this post was a week earlier. I just spent days trialing different skeleton vagrant stacks, none of which were very reliable. Scotch box looks great though, thanks.
I saw some tutorial author share their vagrant box so their reader can follow their tutorial. And my question is if i install 5 diff lamp vagrant box, does that mean in each project directory, each has their own php, mysql, composer, laravel installation? won’t that take lot of space?
My boxes are usually 1-2gigs. I don’t think it takes up too much HD space. You could also use vagrant destroy, and just start fresh every time if you’re concerned about space.
This is a good starter article, but I’d love to see a follow-up that deals with a) using your IDE/text editor with sites inside Vagrant and also b) deploying to a production server.
Well, you’d use your IDE/text editor like you would for anything else, nothing changes there. You can even install IDE like VIM on the box just like on your PC. There are a lot of boxes that have VIM as part of the setup.
You guys should check out this Vagrant WordPress setup that one of my developers created. It creates a fully functional WordPress install with PHPMyAdmin in less than 3-5 minutes of provisioning. My company uses it for all of our projects.
https://github.com/axocomm/vagrant-wordpress
Email me [email protected] if you have any trouble configuring it.
Awesome, thanks for sharing!
I’m using Vagrant for all my WordPress work. Can’t say that I’m an advanced Vagrant user, or even close to that.
For my needs “Varying Vagrant Vagrants” is a great tool that starts up new WordPress installs with just a few lines of commands in the terminal. Everything from downloading WP, setting up config/database, hostname (ie. mysite.dev or similiar) is taken care of.
I’m using https://github.com/chad-thompson/vagrantpress as a development box. It’s been very stable and fast for me.
Hey, I made Scotch Box! Great article.
For Vagrant, Scotch Box just works – plain and simple. Also look into Homestead for an NGINX stack that just works.
Otherwise, the true beauty of Vagrant is in provisioning. With Scotch Box, everything is packed into one big fat VM. This works well for basic PHP stuff and making it easy for everyone.
If you start doing advanced stuff though, you can get Vagrant to provision your server on
vagrant up
using tools like Chef, Puppet, or even a shell script. Imagine a server setup as a config file that Vagrant uses.Also, stay tuned everyone. Scotch Box 2.0 is coming soon. Better support for VHost, faster performance, optional HHVM, better file permissions, and more.