How to Install KVM on Ubuntu 20.04

December 3, 2020

Introduction

A type 2 hypervisor enables users to run isolated instances of other operating systems inside a host system. As a Linux based OS, Ubuntu supports a wide range of virtualization solutions.

Aside from popular third-party apps, such as VirtualBox and VMWare, the Linux kernel has its own virtualization module called KVM (Kernel-based Virtual Machine).

In this tutorial you will learn how to install and set up KVM on Ubuntu 20.04.

How to Install KVM in Ubuntu 20.04

Prerequisites

  • A system running Ubuntu 20.04
  • An account with sudo privileges
  • Access to the command line/terminal

Check Virtualization Support on Ubuntu 20.04

1. Before you begin with installing KVM, check if your CPU supports hardware virtualization via egrep command:

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

Check the number in the output:

Checking virtualization support using egrep command


If the command returns a value of 0, your processor is not capable of running KVM. On the other hand, any other number means you can proceed with the installation.

2. Now, check if your system can use KVM acceleration by typing:

sudo kvm-ok

The output should look like this:

Checking if the system is set up to run kvm virtualization

If kvm-ok returns an error stating KVM acceleration cannot be used, try solving the problem by installing cpu-checker.

3. To install cpu-checker, run the following command:

sudo apt install cpu-checker

4. When the installation completes, restart the terminal.

You are now ready to start installing KVM.


Note: When it is performed for servers, hardware virtualization is referred to as server virtualization.


Install KVM on Ubuntu 20.04

To enable KVM virtualization on Ubuntu 20.04:

  • Install related packages using apt
  • Authorize users to run VMs
  • Verify that the installation was successful

Step 1: Install KVM Packages

1. First, update the repositories:

sudo apt update

2. Then, install essential KVM packages with the following command:

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

This will start the installation of four KVM packages:

Installing the packages necessary to run KVM in Ubuntu 20.04


3. When prompted, type Y, press ENTER, and wait for the installation to finish.

Step 2: Authorize Users

1. Only members of the libvirt and kvm user groups can run virtual machines. Add a user to the libvirt group by typing:

sudo adduser ‘username’ libvirt

Replace username with the actual username.

Adding a user to the libvirt usergroup

2. Now do the same for the kvm group:

sudo adduser ‘[username]’ kvm
Adding user to the kvm usergroup

Note: If you need to remove a user from the libvirt or kvm group, just replace adduser with deluser in the command above.


Step 3: Verify the Installation

1. Confirm the installation was successful by using the virsh command:

virsh list --all

You can expect an output as seen below:

Confirming the installation was successful using virsh command

2. Or use the systemctl command to check the status of libvirtd:

sudo systemctl status libvirtd

If everything is functioning properly, the output returns an active (running) status.

Checking the status of the virtualization daemon with systemctl

3. Press Q to quit the status screen.

4. If the virtualization daemon is not active, activate it with the following command:

sudo systemctl enable --now libvirtd

Creating a Virtual Machine on Ubuntu 20.04

1. Before you choose one of the two methods listed below, install virt-manager, a tool for creating and managing VMs:

sudo apt install virt-manager
Installing virt-manager with apt

2. Type Y and press ENTER. Wait for the installation to finish.

Make sure you download an ISO containing the OS you wish to install on a VM and proceed to pick an installation method.

Method 1: Virt Manager GUI

1. Start virt-manager with:

sudo virt-manager

2. In the first window, click the computer icon in the upper-left corner.

Starting VM setup in virt manager on Ubuntu 20.04

3. In the dialogue box that opens, select the option to install the VM using an ISO image. Then click Forward.

Selecting the option to install from an ISO file in virt manager on Ubuntu 20.04

4. In the next dialogue, click Browse Local and navigate to the path where you stored the ISO you wish to install.

Choosing storage volume in virt manager on Ubuntu 20.04

5. The ISO you chose in the previous window populates the field in Step 2. Proceed to Step 3 by clicking Forward.

Confirming the ISO selection in virt manager on Ubuntu 20.04

6. Enter the amount of RAM and the number of CPUs you wish to allocate to the VM and proceed to the next step.

Allocating memory and CPUs in virt manager on Ubuntu 20.04

7. Allocate hard disk space to the VM. Click Forward to go to the last step.

Creating a disk image in virt manager on Ubuntu 20.04

8. Specify the name for your VM and click Finish to complete the setup.

Naming the VM in virt manager on Ubuntu 20.04

9. The VM starts automatically, prompting you to start installing the OS that’s on the ISO file.

Virtual machine OS installation screen in virt manager on Ubuntu 20.04

Method 2: Using Command Line

Use the virt-install command to create a VM via Linux terminal. The syntax is:

virt-install --option1=value --option2=value ...

In the following example, virt-install is used to install Fedora 33 Workstation.


Tip: For tidier appearance of commands with many options, type a back-slash after each option. That way, when you press Enter, the command will not execute, and the cursor will go to the next line.


Options behind the command serve to define the parameters of the installation.

Here is what each of them means:

OptionDescription
--nameThe name you give to the VM
--descriptionA short description of the VM
--ramThe amount of RAM you wish to allocate to the VM
--vcpusThe number of virtual CPUs you wish to allocate to the VM
--diskThe location of the VM on your disk (if you specify a qcow2 disk file that does not exist, it will be automatically created)
--cdromThe location of the ISO file you downloaded
--graphicsSpecifies the display type

Conclusion

After reading this article, you should know how to install KVM on Ubuntu 20.04. Additionally, the article describes two methods of setting up virtual machines, using the virt-manager GUI and the virt-install command.

Was this article helpful?
YesNo
Marko Aleksic
Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.
Next you should read
How to Install KVM on CentOS 8
November 5, 2020

A CentOS 8 machine can be turned into a hypervisor by installing KVM...
Read more
How to Install VirtualBox on Ubuntu
March 13, 2024

VirtualBox is a powerful tool for running a virtual operating system on your computer. In...
Read more
How to Install VMware Workstation on Ubuntu
March 21, 2024

This tutorial shows you how to install VMware Workstation Pro on...
Read more
What is a Hypervisor? Types of Hypervisors 1 & 2
September 29, 2022

Server virtualization is one of the hottest topics in the IT world today. It has...
Read more