Easy local stack management with Vagrant
I’m using Vagrant to hand-off complete copies of my local development environment to other members of my team. This is a great way to lower setup time, isolate dependencies and eliminate inconsistencies. Frontend developers are able to work against a full local environment without wasting time on backend configuration. The following describes how Vagrant can make this possible without any additional provisioning tools. Those tools are powerful and offer even more efficiencies, but we’re leaving them out in the interest of simplicity.
A sample environment
On my MacBook Pro, I built a VM that runs the entire technology stack for a web application I’m currently working on called Jude. It’s a VirtualBox VM with things like Linux, Apache Web Server, MySQL, PHP, Memcache, APC, Drush, and Apache Solr installed and configured to work together. The codebase is checked out from a remote SVN code repository to a local directory on my Mac that’s also shared to the VM. I can use my regular Mac text editor (NetBeans) to edit code locally, and the changes are immediately available in the running VM. I can also use the command line (SSH), a database explorer (Sequel Pro), and a breakpoint debugger (NetBeans) to inspect the running web app.
Vagrant, Chef, and the Drupal Vagrant project made most of this configuration automatic, but manual configuration would have worked just as well. The point being it doesn’t matter how the initial VM gets created or what the technology stack is. It just matters that we set it up once and we want an easy way to copy it to another machine.
Sample workflow for spinning up a new VM copy
Step 1: Package the source VM
First we need to package up the initial VM from the source machine and make it available for download. The following command packs up the VirtualBox VM called vagrant_1374870184 and creates a file on the source machine called jude.box.
vagrant package --base vagrant_1374870184 --output jude.box --vagrantfile box.Vagrantfile
The box file then needs to be copied to the target machine or uploaded to a publicly accessible URL.
Step 2: Install the target VM
On the target machine we need to install VirtualBox and Vagrant, then open up a terminal window and run the following commands.
mkdir <project-directory>cd <project-directory>svn co https://path/to/repo/trunk/htdocs public/dev-site.vbox.local/www/echo -e '33.33.33.10\tdev-site.vbox.local' | sudo tee -a /etc/hostsvagrant init jude https://path/to/file/jude.box
The first three lines create the project directory and checkout the codebase from a remote SVN repository. The public
directory in the checkout location is the directory that will be mounted to the VM via NFS. The dev-site.vbox.local/www
directory represents the web root of an Apache vhost on our VM.
Line four adds the site’s domain alias to our local hosts file. 33.33.33.10
is the IP address we defined in the Vagrantfile on our source VM and dev-site.vbox.local
is the vhost we defined in the source VM’s Apache conf.
Line five uses the source box file we packaged in the first step to initialize the target VM configuration. We now have a file called Vagrantfile
in our project directory where we could override environment settings if we needed.
Step 3: Start the target VM
Now we’re ready to start up the new VM by running:
vagrant up
The first start up may take a few minutes, especially if the box file is remote and has not been downloaded yet. Start ups in the future will be much faster. Once this is complete we can access our new local copy of Jude at http://dev-site.vbox.local.
Further possibilities
At my company, we use a similar technology stack across many of our projects. Vagrant can be used to manage reusable VM components across all these projects. In addition to developer workstation installations, these could be used to spin up identical development, testing and production environments. Say goodbye to “works on my machine” bugs.
If you’re interested in using this approach on your next project and you’re using Drupal, be sure to check out Drupal Vagrant. It made the setup of my initial VM really simple. The only piece that needed to be manually configured was Apache Solr.