How to Install KVM on Ubuntu

August 8, 2024

Introduction

KVM (Kernel-based Virtual Machine) is an open-source virtualization technology built into the Lin ux kernel. It operates within the Linux kernel and uses its capabilities to manage hardware resources directly, acting like a Type 1 hypervisor.

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

How to Install KVM on Ubuntu - a tutorial.

Prerequisites

Install KVM on Ubuntu

To install KVM, you need to set up the necessary virtualization packages, ensure your system supports hardware virtualization, and authorize users to run KVM. This section outlines the necessary steps for KVM installation on the latest Ubuntu version (24.04 Noble Numbat).

Step 1: Update Ubuntu

Before installing KVM, update your system's package repository information. Refreshing the package repository ensures you install the latest available program version available for your system.

Run the command below:

sudo apt update

Provide the root password and wait for the apt package manager to complete the process.

Step 2: Check Virtualization Support on Ubuntu

The next step is to make sure your system supports virtualization. Follow the steps below:

1. Use the egrep command to check if your CPU supports hardware virtualization. Run the following command:

egrep -c '(vmx|svm)' /proc/cpuinfo
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. Next, check if your system can use KVM acceleration:

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, install cpu-checker to resolve the issue.

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

sudo apt install cpu-checker

4. When the installation completes, rerun the command to check KVM acceleration availability, and if everything is ok, you are ready to start installing KVM.

Step 3: Install KVM Packages

Install the essential KVM packages with the following command:

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

Wait until the system installs all the packages.

Step 4: Authorize Users

Only members of the libvirt and kvm user groups can run virtual machines. If you want specific users to run VMs, add them to those user groups. Follow the steps below:

1. Add the user you want to run the virtual machines to the libvirt group:

sudo adduser [username] libvirt

Replace [username] with the actual username.

2. Next, do the same for the kvm group:

sudo adduser [username] kvm

For example:

Adding user to the libvirt and kvm usergroups.

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

Step 5: Verify the Installation

Confirm that the KVM installation was successful with the virsh command. The virsh command is a command-line tool for managing virtual machines on Linux systems. Run the command below:

sudo virsh list --all

The command lists all active and inactive virtual machines on the system. You can expect an output similar to the one below if you have not yet created any VMs:

Confirming the installation was successful using the virsh command.

Alternatively, use the systemctl command to check the status of libvirtd, the daemon that provides the backend services for the libvirt virtualization management system:

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.

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

sudo systemctl enable --now libvirtd

Create Virtual Machine on Ubuntu

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

sudo apt install virt-manager -y

Wait for the installation to finish.

Download the ISO with the OS you wish to install on a VM and proceed to pick an installation method below.

Method 1: Virt Manager GUI

Virt-manager is a graphical user interface tool for managing virtual machines, allowing users to create, configure, and control VMs using libvirt. Follow the steps below:

1. Start virt-manager by running the command below:

sudo virt-manager

Note: If you are using a bare metal server to run VMs and you want to connect via SSH, specify the -Y option when establishing the connection. It enables trusted X11 forwarding, which allows you to run graphical applications on a remote server and display them on your local machine securely.
The syntax is:

ssh -Y username@hostname

2. In the Virtual Machine Manager window, click the computer icon in the upper-left corner to create a new VM.

Starting VM setup in virt manager on Ubuntu 24.

3. Select the option to install the VM using an ISO image and click Forward.

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

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

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

Configuring memory and CPUs amount in virt manager.

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

Creating a disk image in virt manager.

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

Naming the VM in virt manager on Ubuntu.

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

Method 2: Using Command Line

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

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

The options behind the command serve to define the parameters of the installation.

The available options are:

OptionDescription
--nameThe virtual machine's name.
--descriptionA short VM description.
--ramThe amount of RAM you wish to allocate to the VM.
--vcpusThe number of virtual CPUs you wish to allocate to the VM.
--diskThe VM's location 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.

Note: For a tidier appearance of commands with many options, type a back-slash (\) after each option. That way, when you press Enter, the command does not execute, and the cursor goes to the next line.

In the following example, we used virt-install to install Ubuntu 24.

Installing a Ubuntu 24 VM using the virt-install command.

Conclusion

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

Next, see how to install VirtualBox on Ubuntu or learn the difference between containers and virtual machines.

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