Linux VPS

Create a KVM virtual machine with virt-install

Use virt-install to create a KVM virtual machine from the command line: choose CPU, RAM and disk, attach an ISO or cloud image, connect a bridge and finish the install over VNC or serial console.

IntermediateKVM hosts2 min read

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 br0 for public networking, or libvirt's default NAT 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 ubuntu

On 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 \
  --noautoconsole

Connect 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:

  1. Download the image, e.g. Ubuntu's noble-server-cloudimg-amd64.img, and copy it to /var/lib/libvirt/images/app01.qcow2.
  2. Resize it: sudo qemu-img resize /var/lib/libvirt/images/app01.qcow2 30G.
  3. 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 --noautoconsole

The --cloud-init option needs virt-install 3.0 or newer.

Manage the VM afterwards

TaskCommand
List VMsvirsh list --all
Start / shut downvirsh start web01 / virsh shutdown web01
Start on host bootvirsh autostart web01
Serial consolevirsh console web01
Delete VM and disksvirsh undefine web01 --remove-all-storage

Troubleshooting

  • Permission denied on the ISO: keep images under /var/lib/libvirt/images so the qemu user can read them.
  • Unknown OS variant: update osinfo-db or use a close generic value like linux2022.
  • No network in guest: confirm the bridge exists with ip link show br0 and 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.

Next steps

Was this guide helpful?

Still stuck?

Ask MdmHost AI for a quick answer, or open a ticket and our team will take a look. We are here 24/7.