Image for post
Image for post

DevOps Box: Vagrant with KVM

Configure Vagrant with KVM (Ubuntu)

When developing for cloud native solutions, I occasionally need to setup clean isolated environments using virtualization for testing or modelling solutions. One of the tools for my development system used to manage virtual machines is the popular Vagrant tool from Hashicorp.

Installation Steps

Prerequisite Tools

These are instructions use GNU bash that comes standard on most distros. Additionally, these commands need to be installed to for this running the scripts in this tutorial.

sudo apt install curl xml2

Installing KVM and libvirt

You can get libvirt and KVM by installing these tools:

sudo apt-get install -y \
bridge-utils \
cpu-checker \
libguestfs-tools \
libvirt-clients \
libvirt-dev \
qemu-kvm \

Installing Vagrant

You can query the latest Vagrant version and then download and install the package with this small script:

Installing vagrant-libvirt Plugin

By default, Ubuntu 20.04.1 comments out deb-src required by build-dep. Thus we’ll need to uncomment these entries to enable this process.

sudo cp /etc/apt/sources.list /etc/apt/sources.list~
sudo sed -Ei 's/^# deb-src /deb-src /' /etc/apt/sources.list
sudo apt-get update
sudo apt-get build-dep vagrant ruby-libvirt
sudo apt-get install \
dnsmasq-base \
lebtables \
libvirt-dev \
libxml2-dev \
libxslt-dev \
qemu \
ruby-dev \
vagrant plugin install vagrant-libvirt

Testing Vagrant Box

First let’s download a Vagrant box that supports libvirt.

vagrant box add generic/ubuntu2004 --provider libvirt
cat <<-VAGRANTFILE > Vagrantfile
Vagrant.configure("2") do |config| = "generic/ubuntu2004"
vagrant up --provider libvirt
vagrant ssh

System Info

Let’s install some stuff to see specs of the virtual system. Some of this will vary because it is dependent on the host hardware, but other devices can be emulated, such as network hardware.

sudo apt install -y inxi neofetch
inxi --system --machine --cpu --network --disk --info
Image for post
Image for post
Image for post
Image for post

Vagrant Features

Here are some options to add local file sharing as well as virtual network, which is useful if you want to network several virtual machines together.

Setup Synced Folders

You can add support to grant access to the current directory, which can be mounted as /vagrant on the virtual machine guest.

vagrant reload
ssh vagrant@ \
-i .vagrant/machines/default/libvirt/private_key
vagrant destroy


Here are some articles I came across in my research or by complete accident.

Tool Homepages


Vagrant Libvirt Boxen

Here are places where you can get a Vagrant box image that has support for the libvirt provider:

Networking Articles

Concluding Thoughts

The great thing about this setup is that libvirt with KVM is incredibly fast with about 20 seconds to bring up a system in contrast to Virtualbox, which can be over a minute to bring up a single system. Though, this comes at a cost as there is greater complexity with KVM and libvirt, while Virtualbox is easier to use and enjoys greater support.

Written by

Linux NinjaPants Automation Engineering Mutant — exploring DevOps, Kubernetes, CNI, IAC

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store