How to Install KVM on Ubuntu and Create a Virtual Machine
To install KVM on Ubuntu, set up QEMU and the libvirt daemon, then put your login into the libvirt group:
sudo apt update
sudo apt install qemu-kvm libvirt-daemon-system virtinst
sudo adduser $USER libvirt
The KVM module already ships with the Linux kernel. QEMU executes the VMs, libvirt controls them, and virtinst brings the virt-install command for creating new ones. The following guide shows the whole process, starting with a hardware test and ending with a working VM in a headless server setup.
Step 1: check that the CPU supports KVM
KVM requires hardware virtualization: Intel VT-x or AMD-V, enabled in the BIOS/UEFI. Ubuntu tool kvm-ok checks both:
sudo apt install cpu-checker
kvm-ok
KVM acceleration can be used is what you should look for. Virtualization must be activated in your BIOS if it has not been enabled. In a virtual private server, KVM only runs if the host supports nested virtualization, and many companies do not offer it. That is the reason why KVM hosts are usually dedicated servers.
Step 2: install KVM on Ubuntu
sudo apt update
sudo apt install qemu-kvm libvirt-daemon-system virtinst
sudo systemctl status libvirtd
libvirt-daemon-system will configure and start the daemon of libvirt. Without it, you have an engine that lacks a service for virsh and other tools to talk to.
Step 3: group membership
sudo adduser $USER libvirt
groups
The sudo users become automatically members of the libvirt group; add any other users who work with virtual machines. The group check happens at the time of your login, hence log out and log back into the account. Once done, you will be free from using sudo for virsh.
Step 4: create a virtual machine with virt-install
Download the installation ISO into the libvirt images directory and set up the virtual machine. The following code shows how to create an Ubuntu 24.04 server with 2 vCPU, 4GB RAM, and 30GB hard drive:
sudo wget -P /var/lib/libvirt/images https://releases.ubuntu.com/24.04/ubuntu-24.04.X-live-server-amd64.iso
virt-install \
--connect qemu:///system \
--name web1 \
--memory 4096 \
--vcpus 2 \
--disk size=30 \
--os-variant ubuntu24.04 \
--cdrom /var/lib/libvirt/images/ubuntu-24.04.X-live-server-amd64.iso \
--network network=default \
--graphics vnc,listen=127.0.0.1 \
--noautoconsole
Please replace X with the corresponding version number found at the Ubuntu releases page. The command for osinfo-query os displays the valid values of --os-variant.
The VM's screen only accepts connections from localhost. Find its display with virsh vncdisplay web1 (:0 is equal to port 5900), then connect to it from your local machine through an SSH tunnel:
ssh -L 5900:127.0.0.1:5900 user@your-server
Point a VNC viewer at localhost:5900 and run the installer.
Step 5: manage VMs with virsh
| Task | Command |
|---|---|
| List all VMs | virsh list --all |
| Start / shut down | virsh start web1 / virsh shutdown web1 |
| Force off | virsh destroy web1 |
| Start with the host | virsh autostart web1 |
| Show the VM's IP (NAT network) | virsh domifaddr web1 |
| Delete a VM and its disk | virsh undefine web1 --remove-all-storage |
Even if it is misleadingly named, virsh destroy simply cuts the power; it doesn't delete anything.
Networking: NAT or bridge
By default, VMs use NAT on libvirt's default network behind the virbr0 bridge. VMs have access to the Internet and you can reach them from the host, however, nobody else can connect to a VM directly from outside.
Setting up public IPs per VM requires putting a host interface on a Linux bridge (with Netplan on the Ubuntu server) and using --network bridge=br0 to connect the VMs; each of them should receive an IP address from your provider. The thing is, do not do this using the SSH client, because one small mistake can make the host unavailable. Our Linux network commands cheat sheet helps verify the IPs and routes afterwards.
Sizing a KVM host
Plan cores and memory for all VMs at once, plus headroom for the host. Our EPYC 7763 servers have 64 cores, enough for 50+ VMs, with 1 IPv4 address included, extra IPs at $2.50 each and a /64 IPv6 subnet available. See virtualization servers for the full setup.
Was this article helpful?
Written by Julius · Product Manager