Building a KVM virtual environment on Ubuntu generally involves the following core steps: Hardware Check ➔ Component Installation ➔ User Rights Configuration ➔ Network Setup ➔ Creating VMs.
This guide applies to Ubuntu 20.04, 22.04, and 24.04 LTS (both Desktop and Server editions).
1. Confirm CPU Hardware Virtualization Support
First, check if your CPU supports hardware virtualization (Intel vmx or AMD svm):
egrep -c '(vmx|svm)' /proc/cpuinfo
- Output > 0: Your CPU supports virtualization.
- Output = 0: Enable
Intel VT-xorAMD-Vin your BIOS/UEFI settings (note that some cloud servers do not support nested virtualization).
Verify that your system architecture is 64-bit (required by KVM):
uname -m
# Should output x86_64 or aarch64
2. Install KVM and Related Tools
Update your package list and install the KVM suite:
sudo apt update
sudo apt install -y qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virt-manager virtinst
Core Components Explanation:
qemu-kvm: KVM core emulation module.libvirt-daemon-system: libvirt daemon providing a unified management interface.libvirt-clients: Command-line management tools likevirsh.bridge-utils: Tools for configuring network bridges (optional, can be omitted if advanced bridging isn’t needed).virt-manager: Graphical user interface for managing virtual machines (can be skipped on headless servers).virtinst: CLI tools for creating VMs, such asvirt-installandvirt-clone.
3. Verify Installation and Start Services
Check and start the libvirtd service, then verify that the KVM kernel modules are loaded:
# Check libvirtd status
sudo systemctl status libvirtd
# Start and enable on boot if not running
sudo systemctl enable --now libvirtd
# Verify KVM modules are loaded (should output kvm_intel or kvm_amd)
lsmod | grep kvm
4. Add Current User to User Groups (Root-free Management)
To manage virtual machines without prefixing every command with sudo, add your current user to the libvirt and kvm groups:
sudo usermod -aG libvirt $USER
sudo usermod -aG kvm $USER
After running the commands, log out and log back in, or run the following command to apply the group membership immediately:
newgrp libvirt
5. Network Configuration (Optional)
By default, KVM creates a NAT virtual network named default (192.168.122.0/24), allowing virtual machines to access external networks through the host.
# View default networks
virsh net-list --all
# Activate and set to autostart if inactive
virsh net-start default
virsh net-autostart default
Configuring a Bridge Network (br0)
If you need your virtual machines to be on the same local network as the host machine, you can configure a network bridge using Netplan.
Edit your network configuration file (e.g., /etc/netplan/01-netcfg.yaml, file names may vary):
network:
version: 2
ethernets:
enp3s0:
dhcp4: false
bridges:
br0:
interfaces: [enp3s0]
dhcp4: true
Apply the network configuration:
sudo netplan apply
Note: Bridging takes over your physical network interface. Ensure you have an alternative remote access channel (like IPMI or an auxiliary NIC) before applying to avoid locking yourself out. Select
br0when creating VMs later.
6. Create Your First Virtual Machine
Method A: Graphical Interface (virt-manager)
- Launch
virt-managerby typing it into your terminal (or opening it from your desktop application menu). - Go to File ➔ New Virtual Machine, then select your ISO image, RAM size, and disk allocation.
- In the network step, choose
defaultfor NAT, or selectbr0if you set up a bridge network beforehand.
Method B: Command Line Interface (virt-install)
Example for installing Ubuntu Server:
sudo virt-install \
--name ubuntu-test \
--ram 2048 \
--vcpus 2 \
--disk path=/var/lib/libvirt/images/ubuntu-test.qcow2,size=20,format=qcow2 \
--os-variant ubuntu22.04 \
--network network=default \
--graphics spice,listen=0.0.0.0 \
--console pty,target_type=serial \
--cdrom /path/to/ubuntu-22.04-live-server-amd64.iso
--disk: Usesqcow2format for dynamic storage allocation to save disk space.--network: Connects to the default NAT network.- For VNC/SPICE remote graphics, you can switch to
--graphics vnc,listen=0.0.0.0.
7. Common Management Commands (virsh)
| Operation | Command |
|---|---|
| List all virtual machines | virsh list --all |
| Start a virtual machine | virsh start <vm-name> |
| Graceful shutdown | virsh shutdown <vm-name> |
| Force shutdown (Power off) | virsh destroy <vm-name> |
| Delete VM and its disk | virsh undefine <vm-name> --remove-storage all |
| Access text console | virsh console <vm-name> |
8. Remote Management (Optional)
- SSH + Virsh Remote Connection:
virsh -c qemu+ssh://user@host/system list - Web Control Panel (Cockpit):Access it via your browser at
sudo apt install cockpit cockpit-machineshttps://<host-ip>:9090.
Troubleshooting
- Permission Denied Error:
Ensure your current user has been added to both the
libvirtandkvmgroups and that you have logged back into your session. - Virtual Machine Cannot Access the Internet:
Check if the
defaultnetwork is active, and verify that IP forwarding is enabled on the host (cat /proc/sys/net/ipv4/ip_forwardshould return1). kvm: permission deniederror: The current user might lack access to/dev/kvm. Runls -l /dev/kvmto verify the group is set tokvmand ensure your user belongs to it.