dotlinux blog

How to Install KVM on Ubuntu 20.04: A Step-by-Step Guide

KVM (Kernel-based Virtual Machine) is a Linux kernel module that enables hardware-assisted virtualization. It works with Intel VT-x or AMD-V processors to run VMs at near-native speed. Key components of a KVM setup include:

  • QEMU: An emulator that KVM uses to simulate hardware (e.g., disks, network cards).
  • libvirt: An API and management tool for virtualization platforms (KVM, Xen, etc.).
  • virt-manager: A GUI for managing VMs via libvirt.
  • virsh: A command-line tool for interacting with libvirt.

KVM is ideal for:

  • Running multiple OSes on a single server.
  • Testing software in isolated environments.
  • Creating development/staging environments.
  • Building cloud infrastructures (e.g., OpenStack uses KVM).
2026-04

Virtualization has become an essential tool for developers, sysadmins, and hobbyists alike—allowing you to run multiple operating systems (OSes) on a single physical machine. KVM (Kernel-based Virtual Machine) is a open-source hypervisor for Linux that turns your Ubuntu 20.04 system into a powerful virtualization host. Unlike Type-2 hypervisors (e.g., VirtualBox), KVM is a Type-1 (bare-metal) hypervisor, meaning it runs directly on the host’s hardware for near-native performance.

In this detailed guide, we’ll walk you through installing KVM on Ubuntu 20.04, configuring network bridging (for seamless VM networking), creating your first VM, and managing VMs with both GUI and command-line tools.

Table of Contents#

  1. Introduction to KVM
  2. Prerequisites
  3. Step 1: Check Hardware Virtualization Support
  4. Step 2: Install KVM and Related Packages
  5. Step 3: Configure User Permissions
  6. Step 4: Verify KVM Installation
  7. Step 5: Set Up a Network Bridge (Optional but Recommended)
  8. Step 6: Create Your First KVM Virtual Machine
  9. Step 7: Manage KVM VMs with virsh
  10. Troubleshooting Common Issues
  11. Conclusion
  12. References

2. Prerequisites#

Before you start, ensure:

  1. You’re running Ubuntu 20.04 LTS (Desktop or Server).
  2. Your CPU supports hardware virtualization (Intel VT-x or AMD-V).
  3. You have sudo privileges on the host.
  4. Enough RAM/storage for VMs (e.g., 4 GB RAM + 20 GB storage per VM).

3. Step 1: Check Hardware Virtualization Support#

First, confirm your CPU supports virtualization. Open a terminal and run:

egrep -c '(vmx|svm)' /proc/cpuinfo
  • Output ≥ 1: Virtualization is supported (vmx = Intel, svm = AMD).
  • Output 0: Virtualization is disabled in BIOS/UEFI (see below).

Enable Virtualization in BIOS/UEFI#

If the output is 0, you need to enable virtualization in your motherboard’s BIOS/UEFI:

  1. Restart your computer.
  2. Press the key to enter BIOS (usually Del, F2, or F10—check your motherboard manual).
  3. Navigate to the Advanced or CPU Configuration section.
  4. Enable Intel Virtualization Technology (VT-x) or AMD-V.
  5. Save changes and exit (usually F10).

Update your package list and install KVM and dependencies:

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

Let’s break down the packages:

  • qemu-kvm: Emulator for KVM.
  • libvirt-daemon-system: Systemd service for libvirt.
  • libvirt-clients: Command-line tools for libvirt (e.g., virsh).
  • bridge-utils: Tools for creating network bridges.
  • virt-manager: GUI for managing VMs.

Start and enable the libvirtd service (runs automatically on boot):

sudo systemctl start libvirtd
sudo systemctl enable libvirtd

5. Step 3: Configure User Permissions#

By default, only root and users in the libvirt/kvm groups can access KVM. Add your user to these groups:

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

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

6. Step 4: Verify KVM Installation#

To confirm KVM is set up correctly:

  1. Check if libvirtd is running:

    sudo systemctl status libvirtd

    You should see active (running).

  2. List available virtualization pools (storage) with virsh:

    virsh pool-list --all

    Output (default pool):

    Name      State    Autostart
    ------------------------------
    default   active   yes
    
  3. Open virt-manager (GUI):

    • On Desktop: Search for “Virtual Machine Manager” in the applications menu.
    • On Server: Run virt-manager in the terminal (requires X11 forwarding if using SSH).

If virt-manager opens without errors, KVM is ready!

By default, KVM uses NAT networking (VMs share the host’s IP). For VMs to appear as separate devices on your local network (e.g., for a server), use a bridge.

Ubuntu 20.04 uses netplan for network configuration. Follow these steps:

Step 5.1: Identify Your Physical Interface#

Run:

ip a

Look for your wired interface (e.g., enp0s3, eth0).

Step 5.2: Edit Netplan Configuration#

Back up the original config:

sudo cp /etc/netplan/00-installer-config.yaml /etc/netplan/00-installer-config.yaml.bak

Open the config file with a text editor (e.g., nano):

sudo nano /etc/netplan/00-installer-config.yaml

Replace the content with this (adjust enp0s3 to your interface):

network:
  version: 2
  ethernets:
    enp0s3:  # Physical interface name
      dhcp4: no  # Disable DHCP on physical interface
  bridges:
    br0:  # Bridge name (can be anything)
      interfaces: [enp0s3]  # Attach physical interface to bridge
      dhcp4: yes  # Enable DHCP for the bridge (use static IP if preferred)
      parameters:
        stp: true  # Spanning Tree Protocol (prevents network loops)
        forward-delay: 4  # Delay before bridge starts forwarding traffic

Step 5.3: Apply the Netplan Config#

Test the config (rolls back if there’s an error):

sudo netplan try

If no errors, apply permanently:

sudo netplan apply

Step 5.4: Verify the Bridge#

Run ip a again—you should see the br0 bridge with an IP address (e.g., 192.168.1.100).

8. Step 6: Create Your First KVM Virtual Machine#

You can create VMs using virt-manager (GUI) or virt-install (command line). We’ll cover both.

8.1 Using virt-manager (GUI)#

  1. Open virt-manager and click the “Create a new virtual machine” button (➕).
  2. Select “Local install media (ISO image or CDROM)” and click Next.
  3. Browse to your ISO file (e.g., ubuntu-20.04-desktop-amd64.iso). virt-manager will auto-detect the OS—if not, select it manually. Click Next.
  4. Allocate Resources:
    • Memory: 2048 MB (minimum for Ubuntu Desktop).
    • CPUs: 2 (minimum). Click Next.
  5. Create Disk Image:
    • Set the disk size (e.g., 20 GB). Click Next.
  6. Finalize VM Settings:
    • Name your VM (e.g., ubuntu-vm).
    • Check “Customize configuration before install” (optional but recommended for network/ hardware tweaks).
    • Click Finish.
  7. Customize VM (Optional):
    • Go to the “Network” section and select your bridge (br0) from the “Source network” dropdown.
    • Adjust other settings (e.g., storage, CPU) if needed.
    • Click “Begin Installation”.
  8. Follow the OS installer prompts (e.g., select language, timezone, username/password).

Once installed, your VM will boot automatically!

8.2 Using virt-install (Command Line)#

For server users or automation, use virt-install. Here’s a command to create an Ubuntu 20.04 VM:

virt-install \
  --name ubuntu-vm \
  --os-variant ubuntu20.04 \
  --memory 2048 \
  --vcpus 2 \
  --cdrom ~/Downloads/ubuntu-20.04-desktop-amd64.iso \
  --disk size=20 \
  --network bridge=br0 \
  --graphics spice \
  --noautoconsole

Let’s explain the options:

  • --name: Name of the VM.
  • --os-variant: Tells KVM the OS for optimal settings (run osinfo-query os to see all variants).
  • --memory: RAM in MB.
  • --vcpus: Number of CPU cores.
  • --cdrom: Path to the ISO file.
  • --disk size: Disk size in GB (creates a QCOW2 image in /var/lib/libvirt/images/).
  • --network bridge: Use the br0 bridge (replace with your bridge name).
  • --graphics spice: Enables SPICE (a high-performance remote display protocol).
  • --noautoconsole: Don’t attach to the VM’s console (use virt-manager or virsh to access it).

To access the VM’s console, run:

virt-manager --connect qemu:///system --show-domain-console ubuntu-vm

9. Step 7: Manage KVM VMs with virsh#

virsh is a powerful command-line tool for managing VMs. Here are common commands:

TaskCommand
List running VMsvirsh list
List all VMs (running/stopped)virsh list --all
Start a VMvirsh start ubuntu-vm
Shutdown a VM (graceful)virsh shutdown ubuntu-vm
Force-stop a VM (risky!)virsh destroy ubuntu-vm
Delete a VM (removes definition, keeps disk)virsh undefine ubuntu-vm
View VM infovirsh dominfo ubuntu-vm
Edit VM config (XML)virsh edit ubuntu-vm

10. Troubleshooting Common Issues#

Here are fixes for common KVM problems:

Issue 1: “Could not access KVM kernel module”#

  • Cause: Virtualization is disabled in BIOS or user isn’t in the kvm group.
  • Fix: Enable virtualization in BIOS (Step 3) or add your user to the kvm group (Step 5).

Issue 2: “Permission denied when connecting to libvirt”#

  • Cause: User isn’t in the libvirt group.
  • Fix: Run sudo usermod -aG libvirt $USER and log out/in.

Issue 3: “Network bridge not working”#

  • Cause: Netplan config has syntax errors or the physical interface is down.
  • Fix:
    1. Check netplan syntax with sudo netplan try.
    2. Ensure the physical interface is up: sudo ip link set enp0s3 up.
    3. Verify the bridge with ip a show br0.

Issue 4: virt-manager won’t open#

  • Cause: libvirtd service is stopped or missing dependencies.
  • Fix:
    1. Start libvirtd: sudo systemctl start libvirtd.
    2. Install missing dependencies: sudo apt install --reinstall virt-manager.

11. Conclusion#

You’ve successfully installed KVM on Ubuntu 20.04 and created your first VM! KVM is a flexible, high-performance virtualization solution—perfect for both personal and enterprise use.

Next steps to explore:

  • Snapshots: Backup VMs with virsh snapshot-create.
  • Cloning: Duplicate VMs with virt-clone.
  • Remote Management: Use virt-manager to manage VMs on a remote server.
  • Cloud Images: Deploy pre-built VM images (e.g., Ubuntu Cloud Images) for faster provisioning.

12. References#

  1. Ubuntu KVM Installation Guide
  2. libvirt Documentation
  3. virt-manager User Manual
  4. Netplan Configuration Guide
  5. virsh Command Reference

Let me know if you have any questions—happy virtualizing! 🚀