In this guide you’ll learn to install KVM hypervisor on Ubuntu 20.04. KVM, short for Kernel-based Virtual Machine is a free and open source virtualization platform for Linux and owned by Red Hat. KVM turns your Linux system into a hypervisor and allows you to run multiple guest operating systems (virtual machines) which do not interfere with the normal Linux operations. To be able to run KVM virtual machines, you need to ensure that your Ubuntu 20.04 supports CPU virtualization extensions like Intel-VT and AMD-V

With KVM, every virtual machine is implemented like a regular Linux process but with isolated resources such as network card, memory and CPU. It is part of Linux and is faster than its counterparts. It is referred to as Kernel-based virtual machine because once installed a kernel module (kvm-intel.ko / kvm-amd.ko) is loaded into the kernel.

Install KVM Hypervisor on Ubuntu 20.04

The below steps guide us on how to install KVM on Ubuntu 20.04

Step 1: Confirm Hardware Virtualization is supported

As stated above, your Ubuntu 20.04 should support hardware virtualization for you to be able to to install a hypervisor like KVM and run virtual machines. Run the below command on the terminal

$ grep -E -c "vmx|svm" /proc/cpuinfo
8

If the output is higher than zero, hardware virtualization is supported otherwise it is not and you, therefore, need to enter system bios and enable VT technology.

You may also wish to check whether your system is capable of running KVM virtual machines. Install ‘cpu-checker’ to enable you to verify.

sudo apt update
sudo apt install -y cpu-checker

Once installed, verify with the below command:

$ kvm-ok
INFO: /dev/kvm exists
KVM acceleration can be used

Step 2: Install KVM , Virt-manager and other dependencies

Once you have verified that your machine can handle KVM hypervisor and run virtual machine, proceed to install KVM, virt-manager and bridge-utils with the below command:

sudo apt update
sudo apt install -y qemu qemu-kvm libvirt-daemon bridge-utils virt-manager virtinst

When installation is complete, verify that KVM module has been loaded into kernel with the below command:

$ lsmod | grep -i kvm
kvm_intel             286720  0
kvm                   712704  1 kvm_intel

Step 3 : Start libvirtd service on Ubuntu

Libvirtd service should be automatically started when installed. Confirm status as below:

$ sudo systemctl status libvirtd.service
● libvirtd.service - Virtualization daemon
     Loaded: loaded (/lib/systemd/system/libvirtd.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2021-02-08 13:24:15 EAT; 5min ago
TriggeredBy: ● libvirtd-ro.socket
             ● libvirtd.socket
             ● libvirtd-admin.socket
       Docs: man:libvirtd(8)
             https://libvirt.org
   Main PID: 58453 (libvirtd)
      Tasks: 19 (limit: 32768)
     Memory: 16.4M

Step 4: Create a Bridge network for KVM virtual machines (Optional)

The network bridge will enable your VMs to be accessible from the internet if you need so. There is already a virtual bridge, virbr0, created automatically when KVM is installed and is used for testing purposes. To create a virtual bridge, edit netplan or network interfaces configurations depending on which your system is using. For my case, I am editing netplan configuration

sudo vim /etc/netplan/01-network-manager-all.yaml

Below in my initial configuration:

network:
  ethernets:
    enp0s3:
     addresses: [1192.168.50.2/24 ]
      gateway4:  192.168.50.1
      nameservers:
        addresses: [4.2.2.2, 8.8.8.8]
  version: 2

To create a bridge, remove the settings on the primary interface and put the under the bridge interface configured on the primary interface as below:

network:
  ethernets:
    enp0s3:
      dhcp4: no
      dhcp6: no
  version: 2
  bridges:
    br0:
      interfaces: [enp0s3]
      addresses: [192.168.50.2/24]
      gateway4: 192.168.50.1
      nameservers:
        addresses: [4.2.2.2, 8.8.8.8]

Once done, activate the changes using below command and you should see your br0 bridge created.

sudo netplan apply

Step 5: Add your user to user to libvirt and kvm groups

Use the below commands to add your user to Libvirt and KVM groups

sudo usermod -aG libvirt $USER
sudo usermod -aG kvm $USER

Step 6: Reboot your Ubuntu 20.04

After all configurations, I would recommend to reboot your Ubuntu system to avoid the error of ‘Not Connected’ when you open virt-manager

sudo reboot

Step 7: Create KVM virtual machines

We can create a Virtual Machine in KVM using virt-manager user interface or from command line using virt-install. Let’s launch virt-manager and start creating our virtual machines. On the terminal, run the command as below:

$ virt-manager

The above command should open virt-manager graphical interface. If it doesn’t you may be required to install xserver environment as below:

sudo apt update
sudo apt-get install xorg -y
sudo reboot

Alternatively, if your Ubuntu 20.04 has a graphical interface, virt-manager will have been added to the your desktop and you can launch by double clicking on the desktop shortcut. Virt-manager should open as below:

KVM Hypervisor

Click on the VM icon to create a new virtual machine and you should get the below page. Choose ‘local install media’ and click ‘forward’.

KVM Hypervisor

Browse to your image location and click forward

Allocate Memory and CPUs then click ‘forward

Allocate storage space and click ‘forward

In the next page, verify the setting and click ‘Finish

Start installing you new OS

Create KVM Virtual Machines from Command line

To install a VM on KVM from command line, I am going to run a command as below, where in this case I am installing Oracle Linux 8

sudo virt-install \
--name=Oracle8-vm \
--vcpus=2 \
--memory=1650 \
--cdrom=/var/lib/libvirt/images/OracleLinux-R8-U1-x86_64-dvd.iso \
--disk size=20 \
--os-variant=ol8.0

The virsh Utility

Virsh is a utility used to interact with the virtual machines. For example, you can list all the configured guests by running the command below:

$ virsh list --all
 Id   Name         State
----------------------------
 4    Oracle8-vm   running
 5    ol8.0        running

You can also use edit guest parameters by running the command below and specifying VM name:

$ virsh edit ol8.0
Select an editor.  To change later, run 'select-editor'.
  1. /bin/nano        <---- easiest
  2. /usr/bin/vim.basic
  3. /usr/bin/vim.tiny
  4. /bin/ed

Choose 1-4 [1]: 

The output is an xml file of the VM configuration which you can edit as you like and save the file.

That’s all I had on how to install KVM hypervisor on Ubuntu 20.04. We have also looked at how to create virtual machines on KVM using both Virt manager and KVM command line. I hope the guide has be useful. Please don’t forget to click on the below links for more great articles:

LEAVE A REPLY

Please enter your comment!
Please enter your name here