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 modeling solutions. One of the tools for my development system that is used to manage virtual machines is the popular Vagrant tool from Hashicorp.

Vagrant by default supports Virtualbox, but what if I want to use the native Linux virtualization solution with KVM? Well, you can with the vagrant-libvirt plug-in.

This article will cover how to install and setup these components on Ubuntu 20.04.1 LTS (Focal Fossa).

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

Afterward, we can install required packages for vagrant-libvirt.

sudo apt-get build-dep vagrant ruby-libvirt
sudo apt-get install \
dnsmasq-base \
lebtables \
libvirt-dev \
libxml2-dev \
libxslt-dev \
qemu \
ruby-dev \

Now we can install the plug-in:

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

Create a small configuration file to use use this new Vagrant box:

cat <<-VAGRANTFILE > Vagrantfile
Vagrant.configure("2") do |config| = "generic/ubuntu2004"

And now bring up the system (< 20 seconds):

vagrant up --provider libvirt

You can no log onto the virtual machine guest with:

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

For inxi, we can get this information:

And for neofetch we get this

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.

Update the Vagrantfile configuration to have the following:

When you are ready you can run the command below. This will install NFS client on the guest system, and update /etc/exports on the host system (so you will be prompted for password). For this to work, you should have nfs-kernel-server installed and running to support NFS (see Setting Up NFS HowTo).

vagrant reload

Afterward, you can access the mounted directory /vagrant on the virtual guest system to write to the current working directory on the host.

Setup Private Networks

With Vagrant and libvirt provider, you can also add private networks that are fully routable from localhost (

You can now access directly. Try this for ssh:

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


When finished with the virtual guest, you can destroy the virtual machine and associated configuration (/etc/exports entries and virtual IP addresses) with a single command:

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.

I especially wanted to cover these tools in this small tutorial as documentation from web searches around using the latest Vagrant with vagrant-libvirt plug-in often yield inconsistent and often outdated information.

I hope this was useful exposure to Hashicorp Vagrant tool with the libvirt provider for automation in your local development setup.




Linux NinjaPants Automation Engineering Mutant — exploring DevOps, o11y, k8s, progressive deployment (ci/cd), cloud native infra, infra as code

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

TryHackMe : Nmap Room

Azure CDN & Azure FrontDoor

Unified Staking is Ready

Finally, InsureDAO testnet is out on Rinkeby testnet today, 6th August at 4:00 PM (UTC) !!

Becoming a Data Engineer: A Definitive Guide

A speedway to Continuous Integration and Continuous Deployment for Java, Node or .NET — Yo Team step by step

What to Expect from Java 10 — One Developer’s View

Antenna toolbox: Radiation pattern for linear array yields

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
Joaquín Menchaca (智裕)

Joaquín Menchaca (智裕)

Linux NinjaPants Automation Engineering Mutant — exploring DevOps, o11y, k8s, progressive deployment (ci/cd), cloud native infra, infra as code

More from Medium

Recipe OSS: How to configure HTTP headers for objects? (Alibaba Cloud)

Installing a LAMP Stack on Azure VM

BATS (Bash Automated Testing System)

Container Certification using GitHub Actions