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-x or AMD-V in 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

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 like virsh.
  • 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 as virt-install and virt-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 br0 when creating VMs later.


6. Create Your First Virtual Machine

Method A: Graphical Interface (virt-manager)

  1. Launch virt-manager by typing it into your terminal (or opening it from your desktop application menu).
  2. Go to File ➔ New Virtual Machine, then select your ISO image, RAM size, and disk allocation.
  3. In the network step, choose default for NAT, or select br0 if 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: Uses qcow2 format 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)

OperationCommand
List all virtual machinesvirsh list --all
Start a virtual machinevirsh start <vm-name>
Graceful shutdownvirsh shutdown <vm-name>
Force shutdown (Power off)virsh destroy <vm-name>
Delete VM and its diskvirsh undefine <vm-name> --remove-storage all
Access text consolevirsh console <vm-name>

8. Remote Management (Optional)

  • SSH + Virsh Remote Connection:
    virsh -c qemu+ssh://user@host/system list
    
  • Web Control Panel (Cockpit):
    sudo apt install cockpit cockpit-machines
    
    Access it via your browser at https://<host-ip>:9090.

Troubleshooting

  1. Permission Denied Error: Ensure your current user has been added to both the libvirt and kvm groups and that you have logged back into your session.
  2. Virtual Machine Cannot Access the Internet: Check if the default network is active, and verify that IP forwarding is enabled on the host (cat /proc/sys/net/ipv4/ip_forward should return 1).
  3. kvm: permission denied error: The current user might lack access to /dev/kvm. Run ls -l /dev/kvm to verify the group is set to kvm and ensure your user belongs to it.