How to Install KVM on Ubuntu

A comprehensive guide to setting up Kernel-based Virtual Machine for hardware virtualization

📅 November 20, 2025 ⏱️ 35 min read 🎯 Intermediate

What You'll Learn

In this tutorial, you'll learn how to install and configure KVM (Kernel-based Virtual Machine) on Ubuntu, verify CPU virtualization support, install necessary packages, create your first virtual machine, and manage VMs using command-line tools and graphical interfaces.

Introduction to KVM

KVM (Kernel-based Virtual Machine) is a full virtualization solution for Linux on x86 hardware containing virtualization extensions (Intel VT or AMD-V). It consists of a loadable kernel module that provides the core virtualization infrastructure and a processor-specific module.

Using KVM, you can run multiple virtual machines running unmodified Linux or Windows images. Each virtual machine has private virtualized hardware: a network card, disk, graphics adapter, etc. KVM is open-source software and has been part of the Linux kernel since version 2.6.20, making it a mature and reliable virtualization platform.

Why Use KVM?

  • Native Performance: KVM is built into the Linux kernel, providing near-native performance
  • Open Source: Free to use with no licensing costs
  • Flexible: Supports various guest operating systems including Linux, Windows, and BSD
  • Scalable: Can handle everything from a single VM to large-scale deployments
  • Security: Leverages Linux security features like SELinux and secure boot

Prerequisites

Before proceeding with the KVM installation, ensure you have:

⚠️ Important Note

If you're installing KVM on a VPS, ensure that your hosting provider supports nested virtualization. Not all VPS providers allow KVM to be installed on their virtual servers. Check with your provider before proceeding.

Step 1: Check CPU Virtualization Support

The first step is to verify that your CPU supports hardware virtualization and that it's enabled in your BIOS. Run the following command to check:

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

If the output is 0, your processor doesn't support hardware virtualization, or it's disabled in the BIOS. If it's 1 or higher, your CPU supports virtualization.

For a more detailed check, use:

lscpu | grep Virtualization

You should see output similar to:

Virtualization:                  VT-x

Or for AMD processors:

Virtualization:                  AMD-V

If virtualization is not enabled, you'll need to reboot your system and enable Intel VT-x or AMD-V in your BIOS/UEFI settings. The exact steps vary by motherboard manufacturer, so consult your hardware documentation.

Step 2: Install KVM and Related Packages

Now that we've confirmed virtualization support, let's install KVM and its dependencies. First, update your package list:

sudo apt update

Install KVM and associated tools:

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

Let's break down what each package does:

💡 Server vs Desktop

If you're installing on a server without a GUI, you can skip virt-manager and manage VMs exclusively through command-line tools like virsh.

Step 3: Verify KVM Installation

After installation, verify that the KVM modules are loaded:

lsmod | grep kvm

You should see output similar to:

kvm_intel             282624  0
kvm                   663552  1 kvm_intel

Or for AMD processors:

kvm_amd               106496  0
kvm                   663552  1 kvm_amd

Check the status of the libvirtd service:

sudo systemctl status libvirtd

The service should be active and running. If it's not, start and enable it:

sudo systemctl start libvirtd
sudo systemctl enable libvirtd

Step 4: Add Your User to Required Groups

To manage VMs without sudo privileges, add your user to the libvirt and kvm groups:

sudo usermod -aG libvirt $USER
sudo usermod -aG kvm $USER

Log out and log back in for the group changes to take effect. You can verify your group membership with:

groups

You should see libvirt and kvm in the output.

⚠️ Security Consideration

Adding users to the libvirt group grants them significant control over the virtualization system. Only add trusted users to this group.

Step 5: Configure Network Bridge (Optional)

By default, KVM creates a NAT-based network for VMs. If you want your VMs to appear as independent hosts on your network with their own IP addresses, you'll need to configure a network bridge.

First, install the network bridge utilities if not already installed:

sudo apt install bridge-utils

Edit the network configuration. For Ubuntu 20.04+ using Netplan, edit /etc/netplan/01-netcfg.yaml:

sudo nano /etc/netplan/01-netcfg.yaml

Add or modify the configuration to create a bridge:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
      dhcp4: no
  bridges:
    br0:
      interfaces: [enp0s3]
      dhcp4: yes

💡 Note

Replace enp0s3 with your actual network interface name. You can find it using ip link command.

Apply the network configuration:

sudo netplan apply

Verify the bridge was created:

ip addr show br0

Step 6: Create Your First Virtual Machine

Now that KVM is installed and configured, let's create your first virtual machine. We'll use Ubuntu Server as an example.

Download an ISO Image

First, download an operating system ISO. Let's use Ubuntu Server:

cd /var/lib/libvirt/images/
sudo wget https://releases.ubuntu.com/22.04/ubuntu-22.04.3-live-server-amd64.iso

💡 Storage Location

The default storage pool for libvirt is /var/lib/libvirt/images/. You can use a different location if needed.

Create VM Using virt-install

Create a new VM with the following command:

sudo virt-install \
  --name=ubuntu-vm \
  --vcpus=2 \
  --memory=2048 \
  --cdrom=/var/lib/libvirt/images/ubuntu-22.04.3-live-server-amd64.iso \
  --disk size=20 \
  --os-variant=ubuntu22.04

Let's break down the parameters:

This command will create the VM and automatically launch the console. Follow the on-screen prompts to install the operating system.

✅ Pro Tip

To see all available OS variants, run: osinfo-query os

Step 7: Managing Virtual Machines with virsh

The virsh command-line tool is the primary way to manage VMs. Here are the most common commands:

List All VMs

virsh list --all

Start a VM

virsh start ubuntu-vm

Shutdown a VM

virsh shutdown ubuntu-vm

Force Stop a VM

virsh destroy ubuntu-vm

⚠️ Warning

The destroy command forcefully stops the VM without a graceful shutdown. Use it only when necessary, similar to pulling the power plug.

Auto-start VM on Boot

virsh autostart ubuntu-vm

Connect to VM Console

virsh console ubuntu-vm

To exit the console, press Ctrl + ]

View VM Information

virsh dominfo ubuntu-vm

Delete a VM

First, ensure the VM is stopped, then undefine it:

virsh shutdown ubuntu-vm
virsh undefine ubuntu-vm --remove-all-storage

Step 8: Using virt-manager (GUI)

If you installed virt-manager and have a desktop environment, you can manage VMs through a graphical interface.

Launch virt-manager from your applications menu or run:

virt-manager

The interface allows you to create, configure, and manage VMs visually. You can view console output, manage virtual hardware, create snapshots, and more.

💡 Remote Management

You can use virt-manager on your local machine to manage KVM on a remote server via SSH. Add a new connection in virt-manager using the QEMU/KVM connection type over SSH.

Step 9: Performance Optimization

To get the best performance from your KVM virtual machines, consider these optimizations:

Enable Nested Virtualization (Optional)

If you need to run VMs inside VMs, enable nested virtualization:

For Intel processors:

echo "options kvm-intel nested=1" | sudo tee /etc/modprobe.d/kvm-intel.conf

For AMD processors:

echo "options kvm-amd nested=1" | sudo tee /etc/modprobe.d/kvm-amd.conf

Reload the KVM module:

sudo modprobe -r kvm_intel
sudo modprobe kvm_intel

Use VirtIO Drivers

VirtIO drivers provide better I/O performance for network and storage devices. When creating VMs, specify the VirtIO driver for disks and network interfaces:

--disk bus=virtio
--network bridge=br0,model=virtio

Allocate CPU Properly

Don't over-commit CPUs. As a general rule, don't allocate more vCPUs to VMs than you have physical cores. Leave at least 1-2 cores for the host system.

Common Issues and Troubleshooting

Issue: "KVM module is not loaded"

Solution: Ensure virtualization is enabled in BIOS and load the KVM module:

sudo modprobe kvm
sudo modprobe kvm_intel  # or kvm_amd

Issue: "Permission denied" when running virsh commands

Solution: Make sure your user is in the libvirt group and you've logged out/in:

sudo usermod -aG libvirt $USER

Issue: Poor VM performance

Solutions:

Issue: Network connectivity problems in VMs

Solution: Check the default network is active:

virsh net-list --all
virsh net-start default
virsh net-autostart default

Security Best Practices

When running KVM in a production environment, follow these security guidelines:

Next Steps

Now that you have KVM installed and running, here are some things to explore:

Conclusion

KVM is a powerful virtualization platform that's free, open-source, and built into the Linux kernel. With this guide, you've learned how to install KVM on Ubuntu, verify CPU virtualization support, create and manage virtual machines using both command-line and graphical tools, and optimize performance.

Virtualization opens up many possibilities, from testing software in isolated environments to running production workloads efficiently. Take time to explore KVM's advanced features and integrate them into your workflow.

🎉 Congratulations!

You now have a fully functional KVM hypervisor on your Ubuntu system. You can create, manage, and run multiple virtual machines with different operating systems.

Related Tutorials

← Back to All Tutorials