virt-install is the quickest way to create a KVM virtual machine on a headless server. One command defines the VM in libvirt, creates its disk and starts the installer. This guide shows how to create a VM with virt-install using an ISO or a ready-made cloud image.
Before you start
- A host with KVM and libvirt installed, see install KVM on Ubuntu.
- A bridge such as
br0for public networking, or libvirt'sdefaultNAT network. - Enough free disk space in
/var/lib/libvirt/images.
Find the OS variant
Telling libvirt the guest OS lets it pick sensible defaults such as virtio devices:
virt-install --osinfo list | grep -i ubuntuOn older versions use osinfo-query os.
Create a VM with virt-install from an ISO
Download the installer ISO into the images directory, then run:
sudo virt-install \
--name web01 \
--memory 4096 \
--vcpus 2 \
--disk path=/var/lib/libvirt/images/web01.qcow2,size=40,format=qcow2,bus=virtio \
--cdrom /var/lib/libvirt/images/ubuntu-24.04-live-server-amd64.iso \
--os-variant ubuntu24.04 \
--network bridge=br0,model=virtio \
--graphics vnc,listen=127.0.0.1 \
--noautoconsoleConnect to the installer through an SSH tunnel: find the port with virsh vncdisplay web01, then forward it (see SSH port forwarding) and open a VNC viewer on localhost.
Create a VM from a cloud image (no installer)
Cloud images boot in seconds and are configured by cloud-init:
- Download the image, e.g. Ubuntu's
noble-server-cloudimg-amd64.img, and copy it to/var/lib/libvirt/images/app01.qcow2. - Resize it:
sudo qemu-img resize /var/lib/libvirt/images/app01.qcow2 30G. - Create it with your SSH key injected:
sudo virt-install --name app01 --memory 2048 --vcpus 2 \
--disk /var/lib/libvirt/images/app01.qcow2,bus=virtio \
--import --os-variant ubuntu24.04 \
--network bridge=br0,model=virtio \
--cloud-init ssh-key=/home/admin/.ssh/id_ed25519.pub \
--graphics none --noautoconsoleThe --cloud-init option needs virt-install 3.0 or newer.
Manage the VM afterwards
| Task | Command |
|---|---|
| List VMs | virsh list --all |
| Start / shut down | virsh start web01 / virsh shutdown web01 |
| Start on host boot | virsh autostart web01 |
| Serial console | virsh console web01 |
| Delete VM and disks | virsh undefine web01 --remove-all-storage |
Troubleshooting
- Permission denied on the ISO: keep images under
/var/lib/libvirt/imagesso the qemu user can read them. - Unknown OS variant: update
osinfo-dbor use a close generic value likelinux2022. - No network in guest: confirm the bridge exists with
ip link show br0and that the IP you assign is routed to the host.
Frequently asked questions
qcow2 or raw disk?
qcow2 supports thin provisioning and snapshots; raw is slightly faster. See qcow2 vs raw.
How do I change RAM later?
Shut the VM down and run virsh setmaxmem web01 8G --config and virsh setmem web01 8G --config.
Can I clone a VM?
Yes: virt-clone --original web01 --name web02 --auto-clone while the source is off.
