Vagrant with VirtualBox is nice, but what about QEMU?
I’ve been using Ubuntu 12.04 LTS as operating system for work and home use exclusively ever since it was released on these days I mostly develop in C++ or in PHP to build Symfony 2 applications.
Now if you happen to use PhpStorm you may have noticed there was a feature introduced to use Vagrant with your projects.
The basic premise of Vagrant is that you should and now can easily can test whatever software you build. With Vagrant you can either chose from existing system templates or craft your own, to test your projects on a fresh, clean system environment. And the best thing is: you can configure the environment using Puppet or Chef, and whatever you break on it, you can always bring up a clean system within seconds.
But as with all interesting features this comes with a price. You have the choice to use either VirtualBox or libvirt based hypervisor to create and run virtual machines.
Wait, where is the issue here? I do not like to use VirtualBox, since it feels alien to run in under Linux. Linux IMHO is kvm/libvirt, and there is a good reason to use it: it works damn fine.
Thus if things would be perfect I would love to run a recent Vagrant version, extended with two plugins, namely
- vagrant-mutate – allowing me to use and convert VirtualBox Vagrant boxes to kvm/libvirt format,
- vagrant-libvirt – allowing me to use libvirt based hypervisor.
Ubuntu 12.04 – being almost two years old – does lack recent packages, such as an up to date version of qemu which is sitting at version 1.0.
Now that does not play too damn well into wanting to use Vagrant, since Vagrant relies on host images in the VirtualBox format, which qemu supported starting with version 1.2.
Lucky for me – and you – Hiroshi Miura provides a PPA with the current QEMU version packaged for Ubuntu 12.04 LTS at https://launchpad.net/~miurahr/+archive/vagrant.
We will add this PPA and use QEMU and a few other packages from here:
If you have a previous installation of QEMU and related packages, I would recommend to purge them from your system, and then add the PPA and install the new packages.
Now that we have a recent QEMU version – 1.4 as of writing this article – we can grab Vagrant. It should be noted that I explicitly use Vagrant 1.3.5 here, since there is a bug in the Vagrant plugin for libvirt which prevents using it with the Vagrant 1.4 (see this issue).
With Vagrant installed a quick
vagrant -v should print out
We’re not done yet, let us add the mutate and libvirt plugins.
libvirt-dev is installed as we need it to install the vagrant-libvirt plugin.
Once this is done we are almost ready to go. Since VirtualBox is the default hypervisor for Vagrant we have to tell Vagrant about our love for libvirt.
This can be done individually when starting a Vagrant deployment using
vagrant up --provider libvirt which IMHO is tedious if you work on multiple
projects and often have to up and destroy machines.
Thus you can also edit your
.bashrc file, and export the default Vagrant provider:
With this done, relog and you can simply use
vagrant up to start machines using
Is that all?
In a perfect world this would be everything but in reality there is a tiny addendum.
Vagrantfile created by
vagrant init usually comes with a section to configure
VirtualBox, and there is a similar section required for libvirt.
The vagrant libvirt plugins has a few more options.
Note that I skipped the libvirt.host setting to that libvirt directly calls
With this done, we can use Vagrant for our projects. Enjoy the pretty development environments, and make good use of them.
All code snippets in this post are made available to use under the MIT Licence.