How to Install and Troubleshoot KVM on Ubuntu: A Complete Guide with Comparison to VirtualBox
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:
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.
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:
Edit the
/etc/modules
file:sudo nano /etc/modules
Add the following line at the end of the file:
kvm_intel
Save and exit (CTRL + X, then Y).
Update the initramfs (optional):
sudo update-initramfs -u
Reboot your system:
sudo reboot
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.
Edit the
/etc/modules
file:sudo nano /etc/modules
Add this line:
kvm_amd
Save and exit.
Update the initramfs (optional):
sudo update-initramfs -u
Reboot your system:
sudo reboot
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:
Check if the module is blacklisted:
Open the blacklist configuration file:
sudo nano /etc/modprobe.d/blacklist.conf
Ensure that
kvm
orkvm_intel
is not blacklisted. If any lines contain these keywords, comment them out or remove them.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.
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
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:
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.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.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
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:
Reboot your computer and enter the BIOS/UEFI settings. The key to press varies by manufacturer (usually F2, F10, or DEL).
Look for an option related to Intel VT-x (for Intel processors) or AMD-V (for AMD processors).
Enable the option and save your changes.
Reboot your machine and check again using
egrep -c '(vmx|svm)' /proc/cpuinfo
.
KVM vs. VirtualBox
Feature | KVM | VirtualBox |
Platform | Native to Linux | Cross-platform (Linux, Windows, macOS) |
Performance | Generally better (hardware-backed) | Slightly lower (uses virtualization extensions but more overhead) |
Management | Managed via virt-manager , libvirt | Managed via VirtualBox GUI, CLI tools |
Subscribe to my newsletter
Read articles from Usama Aijaz directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by