Vagrant use KVM as provider

Posted on 2016-11-4 in Virtualation

Vagrant is built-in support providers like VirtualBox, Hyper-V and Docker. But if you want to use custom provider like KVM on your local system, you have to do a few more steps.

I do below steps on Ubuntu 16.04 64bit.

Check hardware virtualization

First, you need to check whether your CPU supports hardware virtualization or not. In command line, run:

egrep -c ' lm ' /proc/cpuinfo

If print 0, it means your CPU is not 64-bit.
If 1 or higher, that means your CPU supports hardware virtualization.

Install prerequisites

You should have both qemu and libvirt installed if you plan to run VMs on your local system.
My laptop is Ubuntu 16.04, so run:

apt-get install qemu libvirt-bin ebtables dnsmasq
apt-get install libxslt-dev libxml2-dev libvirt-dev zlib1g-dev ruby-dev

If your system is CentOS 6, 7 or Fedora 21:

yum install qemu libvirt libvirt-devel ruby-devel gcc qemu-kvm

Add Users to Groups

You should add your account to the group libvirtd:

sudo adduser `id -un` libvirtd

After this, you need to relogin so that your user becomes an effective member of the libvirtd group. The members of this group can run virtual machines.

Install vagrant-libvirt

I suppose you already have vagrant installed. So if you want to use kvm provider in Vagrant, just install vagrant-libvirt using standard Vagrant plugin installation system.

vagrant plugin install vagrant-libvirt

Verify Installation

After all of the installation, you can create the Vagrantfile as usual, but when you trying to boot up that VM, you should tell Vagrant to use Libvirt, not the default VirtualBox:

vagrant up --provider=libvirt

Or you can set the Libvirt as the default provider:

export VAGRANT_DEFAULT_PROVIDER=libvirt

And you can check the status of your VMs using follow command as well:

➜  ~ virsh list --all
 Id    Name                           State
 ----------------------------------------------------
  5     testmonkey_default             running