KVM on Ubuntu Server 20.04

I bought a new server, specifically to run virtual machines (VMs).

KVM described, directly from RedHat:

Kernel-based Virtual Machine (KVM) is an open source virtualization technology built into Linux®. Specifically, KVM lets you turn Linux into a hypervisor that allows a host machine to run multiple, isolated virtual environments called guests or virtual machines (VMs).

Various sites have a pretty wide variety of packages to install to use the KVM functionality. I found a lot of outdated information, or unnecessary packages.

After a bit of testing (and re-installing the host OS several times…), I found that these instructions work for Ubuntu Server 20.04.

Steps

  1. Make sure that your server supports virtualization:

    egrep -c '(vmx|svm)' /proc/cpuinfo
      32
    

    If the output of the above command is greater than 0, your server supports virtualization.

  2. Make sure that your server supports KVM virtualization, which can be different than simply supporting basic virtualization:

    sudo apt-get install -y cpu-checker
    
    sudo kvm-ok
    INFO: /dev/kvm exists
    KVM acceleration can be used
    
  3. Everything looks ok, so we can install/use KVM:

    sudo apt-get install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils -y
    

    qemu-kvm - package is the main KVM package bridge-utils - package helps you create a bridge connection to allow other users to access a virtual machine other than the host system Libvirt - packages are a C toolkit to interact with the virtualization capabilities of recent versions of Linux (and other OSes)

    If you plan on running Virt-Manager (recommended):

    sudo apt-get install virt-manager -y
    
  4. Check to make sure the virtualization daemon, libvritd-daemon, is running:

    sudo systemctl status libvirtd
    ● libvirtd.service - Virtualization daemon
         Loaded: loaded (/lib/systemd/system/libvirtd.service; enabled; vendor preset: enabled)
         Active: active (running) since Fri 2021-05-08 16:16:25 UTC; 22s ago
    TriggeredBy: ● libvirtd-ro.socket
                 ● libvirtd-admin.socket
                 ● libvirtd.socket
           Docs: man:libvirtd(8)
                 https://libvirt.org
       Main PID: 7502 (libvirtd)
          Tasks: 21 (limit: 32768)
         Memory: 11.5M
         CGroup: /system.slice/libvirtd.service
                 ├─7502 /usr/sbin/libvirtd
                 ├─7644 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/lib/libvirt/libvirt_leaseshelper
                 └─7645 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/lib/libvirt/libvirt_leaseshelper
    
    May 08 16:16:25 ubuntu-kvm systemd[1]: Started Virtualization daemon.
    May 08 16:16:25 ubuntu-kvm dnsmasq[7644]: started, version 2.84rc2 cachesize 150
    May 08 16:16:25 ubuntu-kvm dnsmasq[7644]: compile time options: IPv6 GNU-getopt DBus no-UBus i18n IDN2 DHCP DHCPv6 no-Lua TFTP conntrack ipset auth cryptoh>
    May 08 16:16:25 ubuntu-kvm dnsmasq-dhcp[7644]: DHCP, IP range 192.168.122.2 -- 192.168.122.254, lease time 1h
    May 08 16:16:25 ubuntu-kvm dnsmasq-dhcp[7644]: DHCP, sockets bound exclusively to interface virbr0
    May 08 16:16:25 ubuntu-kvm dnsmasq[7644]: reading /etc/resolv.conf
    May 08 16:16:25 ubuntu-kvm dnsmasq[7644]: using nameserver 127.0.0.53#53
    May 08 16:16:25 ubuntu-kvm dnsmasq[7644]: read /etc/hosts - 7 addresses
    May 08 16:16:25 ubuntu-kvm dnsmasq[7644]: read /var/lib/libvirt/dnsmasq/default.addnhosts - 0 addresses
    May 08 16:16:25 ubuntu-kvm dnsmasq-dhcp[7644]: read /var/lib/libvirt/dnsmasq/default.hostsfile
    
  5. Check if the KVM modules are loaded:

    lsmod | grep -i kvm
    kvm_intel             286720  0
    kvm                   835584  1 kvm_intel
    
  6. By default, only the root user can use KVM, so we need to add your user to the libvirt and kvm groups:

    sudo adduser `id -un` libvirt
    
    sudo adduser `id -un` kvm
    

    Log out, then back in, so that your user becomes an effective member of the libvirtd and kvm groups.

  7. After logging back in, check group membership for libvirt and kvm:

    groups
    tom adm cdrom sudo dip plugdev kvm lxd libvirt
    
  8. Verify the install:

    virsh list --all
     Id   Name   State
    --------------------
    

Once installed, you can either use the command line to install a VM, or use the Virt-Manager GUI.

References:

Ubuntu KVM documenation https://www.tecmint.com/install-kvm-on-ubuntu/

Redhat - What is KVM? https://www.redhat.com/en/topics/virtualization/what-is-KVM