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#
- Introduction to KVM
- Prerequisites
- Step 1: Check Hardware Virtualization Support
- Step 2: Install KVM and Related Packages
- Step 3: Configure User Permissions
- Step 4: Verify KVM Installation
- Step 5: Set Up a Network Bridge (Optional but Recommended)
- Step 6: Create Your First KVM Virtual Machine
- Step 7: Manage KVM VMs with
virsh - Troubleshooting Common Issues
- Conclusion
- References
2. Prerequisites#
Before you start, ensure:
- You’re running Ubuntu 20.04 LTS (Desktop or Server).
- Your CPU supports hardware virtualization (Intel VT-x or AMD-V).
- You have sudo privileges on the host.
- 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:
- Restart your computer.
- Press the key to enter BIOS (usually Del, F2, or F10—check your motherboard manual).
- Navigate to the Advanced or CPU Configuration section.
- Enable Intel Virtualization Technology (VT-x) or AMD-V.
- Save changes and exit (usually F10).
4. Step 2: Install KVM and Related Packages#
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-managerLet’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 libvirtd5. 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 $USERImportant: 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:
-
Check if
libvirtdis running:sudo systemctl status libvirtdYou should see active (running).
-
List available virtualization pools (storage) with
virsh:virsh pool-list --allOutput (default pool):
Name State Autostart ------------------------------ default active yes -
Open
virt-manager(GUI):- On Desktop: Search for “Virtual Machine Manager” in the applications menu.
- On Server: Run
virt-managerin the terminal (requires X11 forwarding if using SSH).
If virt-manager opens without errors, KVM is ready!
7. Step 5: Set Up a Network Bridge (Optional but Recommended)#
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 aLook 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.bakOpen the config file with a text editor (e.g., nano):
sudo nano /etc/netplan/00-installer-config.yamlReplace 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 trafficStep 5.3: Apply the Netplan Config#
Test the config (rolls back if there’s an error):
sudo netplan tryIf no errors, apply permanently:
sudo netplan applyStep 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)#
- Open
virt-managerand click the “Create a new virtual machine” button (➕). - Select “Local install media (ISO image or CDROM)” and click Next.
- Browse to your ISO file (e.g.,
ubuntu-20.04-desktop-amd64.iso).virt-managerwill auto-detect the OS—if not, select it manually. Click Next. - Allocate Resources:
- Memory: 2048 MB (minimum for Ubuntu Desktop).
- CPUs: 2 (minimum). Click Next.
- Create Disk Image:
- Set the disk size (e.g., 20 GB). Click Next.
- Finalize VM Settings:
- Name your VM (e.g.,
ubuntu-vm). - Check “Customize configuration before install” (optional but recommended for network/ hardware tweaks).
- Click Finish.
- Name your VM (e.g.,
- 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”.
- Go to the “Network” section and select your bridge (
- 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 \
--noautoconsoleLet’s explain the options:
--name: Name of the VM.--os-variant: Tells KVM the OS for optimal settings (runosinfo-query osto 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 thebr0bridge (replace with your bridge name).--graphics spice: Enables SPICE (a high-performance remote display protocol).--noautoconsole: Don’t attach to the VM’s console (usevirt-managerorvirshto access it).
To access the VM’s console, run:
virt-manager --connect qemu:///system --show-domain-console ubuntu-vm9. Step 7: Manage KVM VMs with virsh#
virsh is a powerful command-line tool for managing VMs. Here are common commands:
| Task | Command |
|---|---|
| List running VMs | virsh list |
| List all VMs (running/stopped) | virsh list --all |
| Start a VM | virsh 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 info | virsh 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
kvmgroup. - Fix: Enable virtualization in BIOS (Step 3) or add your user to the
kvmgroup (Step 5).
Issue 2: “Permission denied when connecting to libvirt”#
- Cause: User isn’t in the
libvirtgroup. - Fix: Run
sudo usermod -aG libvirt $USERand log out/in.
Issue 3: “Network bridge not working”#
- Cause: Netplan config has syntax errors or the physical interface is down.
- Fix:
- Check netplan syntax with
sudo netplan try. - Ensure the physical interface is up:
sudo ip link set enp0s3 up. - Verify the bridge with
ip a show br0.
- Check netplan syntax with
Issue 4: virt-manager won’t open#
- Cause:
libvirtdservice is stopped or missing dependencies. - Fix:
- Start
libvirtd:sudo systemctl start libvirtd. - Install missing dependencies:
sudo apt install --reinstall virt-manager.
- Start
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-managerto manage VMs on a remote server. - Cloud Images: Deploy pre-built VM images (e.g., Ubuntu Cloud Images) for faster provisioning.
12. References#
- Ubuntu KVM Installation Guide
- libvirt Documentation
- virt-manager User Manual
- Netplan Configuration Guide
- virsh Command Reference
Let me know if you have any questions—happy virtualizing! 🚀