How to Install and Troubleshoot KVM on Ubuntu: A Complete Guide with Comparison to VirtualBox

Usama AijazUsama Aijaz
6 min read

KVM (Kernel-based Virtual Machine) is a powerful and efficient virtualization solution for Linux, allowing you to run virtual machines (VMs) on a Linux host. It is integrated into the Linux kernel and provides high performance due to its close connection with the kernel. When compared with VirtualBox, KVM offers more scalability and better performance but is typically more complex to set up. In this article, we’ll walk through the installation process for KVM on Ubuntu, troubleshoot module loading, and compare it with VirtualBox.


Type 1 vs Type 2 Hypervisor

A hypervisor is a piece of software that allows you to create and manage virtual machines. There are two types of hypervisors:

  1. Type 1 Hypervisor: Also known as a "bare-metal" hypervisor, it runs directly on the hardware, without an underlying operating system. Examples include KVM and VMware ESXi. These are typically more efficient and provide better performance because they do not rely on a host OS.

  2. Type 2 Hypervisor: Also known as a "hosted" hypervisor, it runs on top of a host operating system. Examples include VirtualBox, VMware Workstation, and Parallels. These hypervisors are easier to use but tend to have higher overhead due to relying on the host OS.

KVM is considered a Type 1 hypervisor because it integrates directly with the Linux kernel, providing excellent performance and scalability. VirtualBox, on the other hand, is a Type 2 hypervisor and is simpler to use but may have more overhead and lower performance, especially when compared to KVM in a production environment.


How to Install KVM on Ubuntu

Prerequisites:

  • A 64-bit processor with hardware virtualization support (Intel VT-x or AMD-V)

  • A system running Ubuntu 20.04 or later

  • Administrative privileges (sudo access)

Step 1: Install KVM Packages

First, install KVM, libvirt, and the necessary management tools:

sudo apt update
sudo apt install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virt-manager

This will install:

  • qemu-kvm: the KVM software.

  • libvirt: a management tool for virtual machines.

  • virt-manager: a GUI tool to manage virtual machines.

Step 2: Verify KVM Installation

To check if KVM is installed and supported on your system, run:

kvm --version

If you see version information, KVM is installed.

Step 3: Check Virtualization Support

Ensure your CPU supports hardware virtualization:

egrep -c '(vmx|svm)' /proc/cpuinfo

If the output is greater than 0, your CPU supports hardware virtualization. If it returns 0, you'll need to enable virtualization in your BIOS/UEFI settings (more details below).

Step 4: Add Your User to the 'libvirt' Group

To manage virtual machines, your user needs to be part of the libvirt group:

sudo adduser $(whoami) libvirt

Log out and log back in for the changes to take effect.


Auto-loading KVM Module at Boot

For Intel Processors:

Intel processors use the kvm_intel kernel module. To load this module at boot:

  1. Edit the /etc/modules file:

     sudo nano /etc/modules
    
  2. Add the following line at the end of the file:

     kvm_intel
    
  3. Save and exit (CTRL + X, then Y).

  4. Update the initramfs (optional):

     sudo update-initramfs -u
    
  5. Reboot your system:

     sudo reboot
    
  6. After reboot, confirm that kvm_intel is loaded:

     lsmod | grep kvm
    

For AMD Processors:

For AMD processors, the procedure is similar but uses the kvm_amd module.

  1. Edit the /etc/modules file:

     sudo nano /etc/modules
    
  2. Add this line:

     kvm_amd
    
  3. Save and exit.

  4. Update the initramfs (optional):

     sudo update-initramfs -u
    
  5. Reboot your system:

     sudo reboot
    
  6. Verify that kvm_amd is loaded:

     lsmod | grep kvm
    

Troubleshooting KVM Module Loading

Troubleshooting Intel Platform (kvm_intel)

If the kvm_intel module isn't loading correctly, try these steps:

  1. Check if the module is blacklisted:

    Open the blacklist configuration file:

     sudo nano /etc/modprobe.d/blacklist.conf
    

    Ensure that kvm or kvm_intel is not blacklisted. If any lines contain these keywords, comment them out or remove them.

  2. Check kernel logs for errors:

    Look for any relevant kernel messages:

     dmesg | grep kvm
    

    This can provide insights into why the module isn’t loading.

  3. Force load with systemd:

    Create a systemd service to load the kvm_intel module at boot.

     sudo nano /etc/systemd/system/load-kvm-intel.service
    

    Add the following:

     [Unit]
     Description=Load KVM Intel module
     After=multi-user.target
    
     [Service]
     Type=oneshot
     ExecStart=/sbin/modprobe kvm_intel
     RemainAfterExit=true
    
     [Install]
     WantedBy=multi-user.target
    

    Reload systemd and enable the service:

     sudo systemctl daemon-reload
     sudo systemctl enable load-kvm-intel.service
     sudo systemctl start load-kvm-intel.service
    
  4. Check Secure Boot:

    Secure Boot can block the loading of unsigned kernel modules. To check the status:

     mokutil --sb-state
    

    If Secure Boot is enabled, you may need to disable it in the BIOS or sign the kernel module.


Troubleshooting AMD Platform (kvm_amd)

The troubleshooting steps for AMD processors are similar to Intel, but you’ll be working with the kvm_amd module:

  1. Check if the module is blacklisted:

    Open the blacklist configuration file:

     sudo nano /etc/modprobe.d/blacklist.conf
    

    Ensure that kvm_amd is not blacklisted. Remove or comment out any lines referencing it.

  2. Check kernel logs for errors:

    Run the following command to check kernel logs:

     dmesg | grep kvm
    

    Look for errors or warnings related to the kvm_amd module.

  3. Force load with systemd:

    To ensure the kvm_amd module is loaded at boot, create a systemd service:

     sudo nano /etc/systemd/system/load-kvm-amd.service
    

    Add the following content:

     [Unit]
     Description=Load KVM AMD module
     After=multi-user.target
    
     [Service]
     Type=oneshot
     ExecStart=/sbin/modprobe kvm_amd
     RemainAfterExit=true
    
     [Install]
     WantedBy=multi-user.target
    

    Reload systemd and enable the service:

     sudo systemctl daemon-reload
     sudo systemctl enable load-kvm-amd.service
     sudo systemctl start load-kvm-amd.service
    
  4. Check Secure Boot:

    Similar to Intel, check the status of Secure Boot:

     mokutil --sb-state
    

    If it’s enabled, disable Secure Boot in the BIOS or sign the kernel module.


Enabling Virtualization in BIOS/UEFI

If the egrep -c '(vmx|svm)' /proc/cpuinfo command returns 0, your CPU likely doesn’t have virtualization enabled in the BIOS/UEFI. To enable it:

  1. Reboot your computer and enter the BIOS/UEFI settings. The key to press varies by manufacturer (usually F2, F10, or DEL).

  2. Look for an option related to Intel VT-x (for Intel processors) or AMD-V (for AMD processors).

  3. Enable the option and save your changes.

  4. Reboot your machine and check again using egrep -c '(vmx|svm)' /proc/cpuinfo.


KVM vs. VirtualBox

FeatureKVMVirtualBox
PlatformNative to LinuxCross-platform (Linux, Windows, macOS)
PerformanceGenerally better (hardware-backed)Slightly lower (uses virtualization extensions but more overhead)
ManagementManaged via virt-manager, libvirtManaged via VirtualBox GUI, CLI tools
0
Subscribe to my newsletter

Read articles from Usama Aijaz directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Usama Aijaz
Usama Aijaz