DevOps Box: Vagrant with KVM
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.
This article will cover how to install and setup these components on Ubuntu 20.04.1 LTS (Focal Fossa).
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
sudo apt-get install -y \
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
sudo apt-get build-dep vagrant ruby-libvirt
sudo apt-get install \
Now we can install the plug-in:
vagrant plugin install vagrant-libvirt
Testing Vagrant Box
First let’s download a Vagrant box that supports
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|
config.vm.box = "generic/ubuntu2004"
And now bring up the system (< 20 seconds):
vagrant up --provider libvirt
You can no log onto the virtual machine guest with:
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
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.
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).
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
You can now access
192.168.123.29 directly. Try this for ssh:
ssh firstname.lastname@example.org \
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:
Here are some articles I came across in my research or by complete accident.
Vagrant Libvirt Boxen
Here are places where you can get a Vagrant box image that has support for the
- Robox Generic (ArchLinux, CentOS, Debian, Devuan, DragonFly, Fedora, FreeBSD, Gentoo, Ubuntu, …): https://app.vagrantup.com/generic
- ArchLinux: https://app.vagrantup.com/archlinux
- Debian: https://app.vagrantup.com/debian
- Fedora: https://app.vagrantup.com/fedora
- Haiku: https://app.vagrantup.com/haiku-os
- Windows: https://app.vagrantup.com/jborean93
- F5: https://app.vagrantup.com/boeboe
- Cumulus Networks: https://app.vagrantup.com/CumulusCommunity
- VyOS: https://app.vagrantup.com/vyos
- Pulp Repository Manager: https://app.vagrantup.com/pulp
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.