Skip to content

Instantly share code, notes, and snippets.

@plembo
Last active March 21, 2025 09:17
Show Gist options
  • Save plembo/9557105a1a468662292e39f5329703dc to your computer and use it in GitHub Desktop.
Save plembo/9557105a1a468662292e39f5329703dc to your computer and use it in GitHub Desktop.
Install Ubuntu VM using virt-install

Install a Ubuntu VM using virt-install for KVM (libvirtd)

Ubuntu is a particularly difficult distro to work with virt-install for a lot of reasons, not the least of which is that it departs from its Debian base in many key respects: starting with disk images. What's more, those departures are mostly undocumented. Debian and CentOS are a lot easier. Given IBM's recent actions with regard to CentOS, I think Debian is the best choice going forward.

Main source for how to do this was:

How to install KVM on Ubuntu 20.04 LTS Headless Server

For an interactive "graphical" console install

Two files, ubuntu.env for the environment variables, and inst-ubuntu.sh to run the commands.

ubuntu.env:

$ export VM_NAME="testsrv01"
$ export VM_ISO="/data/install/ubuntu/ubuntu-20.04-live-server-amd64.iso"
$ export VM_NET="default"
$ export VM_OS="ubuntu20.04"
$ export VM_IMG="/data/libvirt/images/${VM_NAME}.qcow2"
$ export VM_CORES=1
$ export VM_DISKSIZE=10
$ export VM_RAMSIZE=2048

inst-ubuntu.sh

#!/bin/bash
sudo virt-install \
--name ${VM_NAME} \
--memory ${VM_RAMSIZE} \
--vcpus ${VM_CORES} \
--os-variant=${VM_OS} \
--virt-type=kvm \
--cdrom=${VM_ISO} \
--network network=${VM_NET},model=virtio \
--graphics vnc \
--disk path=${VM_IMG},size=${VM_DISKSIZE},bus=virtio,format=qcow2

Non-graphical installs

So far, I've been unsuccessful in my efforts to do a non-graphical install. Unlike Debian or CentOS, even Ubuntu Server seems to have a problem giving me a serial console.

Notes on post-install customization

The following packages should always get installed:

  • openssh-server
  • vim
  • spice-vdagent

After installing vim, as root run update-alternatives --config editor. This will allow you to select vim.basic as the default system editor. Unless you can actually stand nano.

The spice-vdagent is automatically installed with Ubuntu 18.04 LTS and 20.04 LTS, but I include it on this list... just in case. This is the service that makes it unnecessary to hit Ctl-Alt every time you want to move the mouse away from the VMM console.

Miscellaneous

I needed to know the Short ID to use in VM_OS for "Ubuntu 20.04":

$ sudo apt update osinfo-db
$ osinfo-query os | grep "Ubuntu 20.04"
ubuntu20.04 | Ubuntu 20.04 | 20.04 | http://ubuntu.com/ubuntu/20.04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment