Vagrant: libvirt plugin

vagrant Nov 24, 2025

I've been using Vagrant since quite a few years ago. It is that easy to create new virtual machines and configuring them the way I need. And yes, I admit that once I was stuck to Virtual Box for some years, even if I knew that KVM is much more performant than Virtual Box. However, I’ve been using the Vagrant libvirt plugin for over a year and I rid off Virtual Box.

The main reason to use Virtual box instead of libvirt is that Virtual box is really portable. It will work on Linux but it will also work in Windows or Mac OS. This is my best reason to use this default provider for Vagrant. However, I don't work with windows, nor with MacOS so let's check go with libvirt and KVM.

Installation pre-requisites

Of course, we'll need to install vagrant and all the needed virtualization stuff in our Linux system. It will be easy for you to find a good installation guide for qemu-kvm, libvirt and vagrant for your operating system.

In Ubuntu we could simple run (... and yes, I know it could be more fine grained):

sudo apt install -y qemu-system libvirt-daemon virt-manager

sudo systemctl enable libvirtd
sudo systemctl start libvirtd

libvirt + qemu installation Ubuntu

And the Vagrant installation goes this way

# 
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor | sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg > /dev/null

echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list

sudo apt update && sudo apt install vagrant

Vagrant Installation Ubuntu

In arch-linux we'll need to install vagrant using yay or paru or something simular after installing libvirt and qemu

sudo pacman -S qemu libvirt virt-manager ebtables dnsmasq bridge-utils

sudo systemctl enable libvirtd
sudo systemctl start libvirtd

# Install Vagrant
yay -S vagrant

Vagrant with libvirt and qemu installation in Arch Linux

In both systems it is interesting to add your user to the libvirt and kvm groups:

sudo usermod -aG libvirt $(whoami)
sudo usermod -aG kvm $(whoami)

And log out and in again.

Install the vagrant-libvirt plugin

Using Ubuntu 24.04

If you are using Ubuntu, installing the Vagrant plugin in Ubuntu is really easy, you simple run:

# Well, we'll need to compile some things.
sudo apt install -y build-essential libvirt-dev

# And install the plugin itself.
vagrant plugin install vagrant-libvirt

Using Arch Linux

However, I am using Arch Linux and, after running that command, our plugin sadly won't work. The installation process was a bit more complicated. I need to manually install the gem:

git clone https://github.com/vagrant-libvirt/vagrant-libvirt.git

cd vagrant-libvirt
vim lib/vagrant-libvirt/driver.rb

## In a text editor (nano or vim), we remove the lines:
# > ip_command = %q( awk "/$mac/ {print \$1}" /proc/net/arp )
# > ...
# > libvirt_ip_command: ip_command,

## We build the Gem:
gem build

## And install it in Vagrant:
VAGRANT_DISABLE_STRICT_DEPENDENCY_ENFORCEMENT=1 vagrant plugin install ./vagrant-libvirt-*.gem

If those 2 lines are not removed, well get this error:

So, the changes in the driver.rbfile are as follow:

        # Get config options for Libvirt provider.
        config = @machine.provider_config
        uri = config.uri

-       # Setup command for retrieving IP address for newly created machine
-       # with some MAC address. Get it from dnsmasq leases table
-       ip_command = %q( awk "/$mac/ {print \$1}" /proc/net/arp )
-
        conn_attr = {
          provider: 'libvirt',
          libvirt_uri: uri,
-         libvirt_ip_command: ip_command,
        }
        conn_attr[:libvirt_username] = config.username if config.username
        conn_attr[:libvirt_password] = config.password if config.password

Using it: Creating a first VM.

In qemu-kvm there are 3 important things (to me) which are 1. setting up a virtio-network driver, 2. setting a virtio-disk driver and 3. host-passthrough the CPU (passing the host CPU model features, model, stepping, exactly to the guest). The first 2 ones are that way by default by vagrant-libvirt, and we'll only have to care for the last one.

$ cd vagrant/01-basic-ub2404-vm

$ # We need to be in the directory where the Vagranfile is:
$ ls
Vagrantfile

$

This first Vagrantfile goes like this:

Vagrant.configure("2") do |config|
  # The Vagrant-box to use (operating system image) - an ubuntu24.0 here.
  config.vm.box = "cloud-image/ubuntu-24.04"

  # Configurations for the Virtual server:
  config.vm.provider "libvirt" do |vb|

    # Disk 50 Gb
    vb.machine_virtual_size = 50

    # Memory 8Gb RAM
    vb.memory = "8192"

    # 4 cores/cpus
    vb.cpus = 4 

    # Nested virtualization - yes (virtualization inside virtualization)
    vb.nested = true

    # We need to care this: host-passtrhough -- Copy our CPU Configurations
    vb.cpu_mode = "host-passthrough"
  end
end

Once defined, we can run vagrant up to start the new server and when the server is running, we can connect via ssh to the server with vagrant ssh.

We can stop the server with vagrant halt (we could start again the same server using again vagrant up) or we can destroy the server forever using vagrant destroy.

0:00
/1:46

Simple and quick demo.

Tags