Skip to content

Installing KVM on Ubuntu for production use

A grown-up KVM install on Ubuntu — packages, kernel modules, libvirtd, networking, and the checks an MSP runs before launching the first guest.

A bare-metal Ubuntu box, an Intel or AMD CPU with virtualization extensions enabled in firmware, and a directive to “make it a hypervisor.” This article is the procedure we follow before declaring a KVM host ready for guests: install the right packages, confirm the kernel modules loaded, get libvirtd healthy, pick a sane networking model, and document the host. The goal is not “kvm-ok says OK” — it is a host where the first guest boots, networks, persists across reboots, and is observable.

How to verify

Before touching apt, confirm the CPU exposes virtualization extensions and the BIOS/UEFI has them enabled. A box that lacks VT-x/AMD-V will install KVM but every guest will fall back to TCG emulation, which is two orders of magnitude slower.

egrep -c '(vmx|svm)' /proc/cpuinfo
lscpu | grep -i virtualization
sudo dmesg | grep -i -e 'kvm:' -e 'iommu' | head
ls /dev/kvm

The first count must be at least 1. If /dev/kvm is missing after install, the kernel module did not load — almost always a BIOS toggle. Reboot, enter firmware, enable Intel VT-x or AMD-V and (if you plan PCI passthrough) Intel VT-d or AMD-Vi.

What’s happening

KVM is a Linux kernel module (kvm.ko plus a CPU-specific module kvm_intel.ko or kvm_amd.ko) that turns the kernel into a Type-1-ish hypervisor by exposing /dev/kvm to userspace. QEMU is the userspace process that actually drives a guest — it allocates memory, attaches devices, and (when KVM is available) hands the CPU instructions to the kernel for hardware-accelerated execution. libvirt is the management layer on top: a daemon (libvirtd) plus a CLI (virsh) and an XML schema that abstracts away the QEMU command line.

On Ubuntu, you install three layers in one go: qemu-kvm (the userspace emulator with KVM acceleration), libvirt-daemon-system (the daemon and its systemd unit), and libvirt-clients (virsh and friends). The package set pulls in bridge-utils and dnsmasq-base so the default NAT network works out of the box.

The procedure

  1. Install the package set. No PPAs unless you have a documented reason — Ubuntu’s archive ships a recent enough stack for most workloads:

    sudo apt update
    sudo apt install -y qemu-kvm libvirt-daemon-system libvirt-clients \
      bridge-utils virtinst cloud-image-utils

    virtinst provides virt-install for scripted guest creation; cloud-image-utils provides cloud-localds for cloud-init seed ISOs.

  2. Confirm modules and devices. The package post-install loads the modules and creates /dev/kvm with group kvm. Verify:

    lsmod | grep kvm
    ls -l /dev/kvm
    sudo systemctl is-active libvirtd
    sudo systemctl is-enabled libvirtd
  3. Add the operator account to the libvirt group. Without it, every virsh call needs sudo and virt-manager cannot connect to qemu:///system:

    sudo usermod -aG libvirt,kvm "$USER"
    newgrp libvirt
    virsh -c qemu:///system list --all

    The last command should return an empty table with no error — that’s the libvirt daemon answering.

  4. Pick the networking model and stick with it. The default virbr0 NAT network is fine for a dev box but useless when a guest needs to be reached from outside the host. For production, configure a Linux bridge on the physical NIC (see libvirt domain XML schema for the corresponding <interface> block) using netplan:

    # /etc/netplan/01-bridge.yaml
    network:
      version: 2
      ethernets:
        eno1:
          dhcp4: false
      bridges:
        br0:
          interfaces: [eno1]
          dhcp4: true
          parameters:
            stp: false
            forward-delay: 0
    sudo netplan apply
    ip -br link show br0
  5. Declare the bridge to libvirt. libvirt does not auto-discover Linux bridges; you define them as a network or attach to them directly per-guest. The lightweight option:

    cat <<'EOF' | sudo tee /etc/libvirt/qemu/networks/br0.xml
    <network>
      <name>br0</name>
      <forward mode="bridge"/>
      <bridge name="br0"/>
    </network>
    EOF
    sudo virsh net-define /etc/libvirt/qemu/networks/br0.xml
    sudo virsh net-start br0
    sudo virsh net-autostart br0
  6. Set sane storage defaults. The default pool /var/lib/libvirt/images is fine, but on production hosts we move it to a dedicated filesystem (often LVM thin or a ZFS dataset) so a runaway guest cannot fill the root volume. List, define, and inspect pools with virsh pool-list --all, virsh pool-define-as, virsh pool-info.

  7. Smoke-test with one guest. Pull a cloud image and boot it (see virt-install CLI for the full flag set):

    virt-install --name smoke --ram 2048 --vcpus 2 \
      --disk size=10,pool=default \
      --location 'http://archive.ubuntu.com/ubuntu/dists/jammy/main/installer-amd64/' \
      --network network=br0 --graphics none --console pty,target_type=serial \
      --extra-args 'console=ttyS0,115200n8'

    When it returns, virsh list shows the guest, virsh console smoke attaches a serial console, and virsh shutdown smoke powers it off cleanly.

Common pitfalls

  • A CPU with vmx/svm flags but /dev/kvm absent means the BIOS toggle is off — reboot into firmware. No amount of modprobe will help.
  • Forgetting to add the user to the libvirt group leaves every command needing sudo; the user’s session must be re-established (logout/login or newgrp libvirt) for the change to take effect.
  • The default virbr0 NAT network and a host-side firewall (UFW, firewalld) collide — guests get DHCP but no outbound traffic; you must allow forwarding on the bridge or accept that guests are isolated.
  • Nested virtualization is off by default on Intel; if you need to run KVM inside a KVM guest (see nested virtualization KVM), kvm_intel.nested=1 must be set as a module parameter.
  • qemu-kvm and the host kernel must agree on CPU model: copying a guest XML from a Skylake host to an older Westmere host without changing the <cpu mode> element produces a guest that refuses to start with a cryptic feature error.

Stack Harbor stands up KVM fleets as the operating model for clients who want self-hosted VMs without the cost of a commercial hypervisor — host build under config management, networking documented, storage pools sized to a forecast, guest templates versioned, libvirt API exposed only to the bastion. If you have a fleet of one-off KVM hosts that grew organically and you want it run as one platform, that is the work covered under Managed Operations.