How to Install KVM on Ubuntu Linux

Published:3 August 2022 - 9 min. read

Nicholas Xuan Nguyen Image

Nicholas Xuan Nguyen

Read more tutorials by Nicholas Xuan Nguyen!

You manage virtual machines (VMs) on your network as a system administrator. There are many ways to create and manage VMs, and KVM is a popular solution for Ubuntu Linux.

This guide will teach you how to configure and install KVM on Ubuntu Linux. You will also learn the basic commands for managing VMs with KVM. By the end of this guide, you’ll know how to create and manage VMs using KVM on your Ubuntu server.

Ready? Read on to get started!

Prerequisites

This tutorial will be a hands-on demonstration. If you’d like to follow along, be sure you have an Ubuntu server with the following specifications.

  • A minimum of 4 GB of RAM and 2 virtual CPUs (vCPUs). The more CPU and RAM, the better.
  • An ISO image for the operating system you want to install on your VM. This guide will be using a Fedora Workstation ISO file, but feel free to have any Linux operating systems ISO.

If you do not have a server that meets these specifications, you can follow along with a free trial from a cloud provider like AWS, Google Cloud, or Azure.

Checking Your Hardware

Before you begin, you must check if your CPU supports virtualization. KVM requires CPU virtualization extensions, such as Intel VT-x or AMD-V.

This tutorial assumes that you’ve already logged in to the Ubuntu host via an SSH client or the desktop environment (if available) and have a terminal window open.

Note: This entire tutorial will be running the terminal commands logged in as the root user. If you don’t log in as root, you may prepend sudo for each command as needed.

Using the lscpu Command

The lscpu command is a built-in command that displays the CPU architecture information. To check if your CPU supports virtualization, run the below command to determine whether your machine supports virtualization.

lscpu | grep Virtualization:

If your host computer supports virtualization, you’ll get a result similar to the screenshot below. A blank result means that there’s no virtualization capability.

Checking virtualization support with lscpu
Checking virtualization support with lscpu

Using the cpu-checker Tool

If you need further confirmation about the hardware virtualization support status, another way to check is with the cpu-checker package. This package contains tools for evaluating the CPU support for various features, including virtualization.

1. Update your package repositories by running the below command.

apt update -y

2. Execute the below command to install the cpu-checker package.

apt install -y cpu-checker

3. Finally, check if your CPU supports virtualization by running this command.

kvm-ok

If you get the following result, your CPU supports virtualization with KVM.

Checking CPU virtualization support - OK
Checking CPU virtualization support – OK

If your CPU is not capable of virtualization, you’ll get the following result instead. In this case, your current hardware is sufficient, and you cannot install KVM.

Checking CPU virtualization support - NOT OK
Checking CPU virtualization support – NOT OK

Installing KVM on Ubuntu

Now that you have verified that your CPU supports virtualization, you are ready to install KVM on your Ubuntu server. This guide uses Ubuntu 20.04, but the steps should be similar for other versions of Ubuntu.

  1. To install KVM on Ubuntu, run the below apt command. The list below breaks down the packages you’re installing.
  • qemu-kvm – This package provides accelerated KVM support. This package is a userspace component for QEMU, which is responsible for accelerating KVM guests.
  • libvirt-daemon-system – This package provides the libvirt daemon and management tools. The libvirt daemon is responsible for creating, managing, and destroying virtual machines on your server.
  • libvirt-clients – This package provides the libvirt command-line interface for managing virtual machines.
  • bridge-utils – This package provides the userland utilities for configuring virtual network bridges. On a hosted virtualization solution like KVM, a network bridge connects the guest VM network to the host network.
  • virt-manager – This package provides a graphical user interface (GUI) for managing virtual machines.
apt install qemu-kvm libvirt-clients libvirt-daemon-system bridge-utils -y

2. If your Ubuntu host has a desktop environment, install the virt-manager package, which provides a graphical user interface (GUI) for managing virtual machines.

apt install virt-manager -y

3. To enable the libvirt daemon to start automatically at boot, run the below command.

systemctl enable libvirtd

4. To check the status of the libvirt daemon, run the below command:

systemctl status libvirtd

The service status should be enabled and active (running).

Checking the libvirtd daemon status
Checking the libvirtd daemon status

If libvirtd is not running, run the below command to start the daemon.

systemctl start libvirtd

5. Finally, run the below command to check if KVM is loaded in the kernel. The output of this command will show the kvm modules loaded in the kernel. At this point, you’ve successfully installed KVM.

lsmod | grep -i kvm
Checking if KVM is loaded in the kernel
Checking if KVM is loaded in the kernel

Creating Your First Virtual Machine in KVM

You’re ready to create your first virtual machine now that you’ve installed KVM. In this section, you will learn how to create a VM using the Virtual Machine Manager (GUI) and the command line.

Using the virt-manager (GUI)

Follow the below steps to create a VM using the Virtual Machine Manager. This section applies only if your Ubuntu host has a desktop environment. If not, skip this section.

1. Run the below command to launch the Virtual Machine Manager.

virt-manager
Launching the Virtual Machine Manager
Launching the Virtual Machine Manager

2. To create a new VM, click the computer icon on the top left.

Clicking the new virtual machine button
Clicking the new virtual machine button

3. On the New VM window, select Local install media(ISO image or CDROM) and click on Forward.

Selecting the operating system installation method
Selecting the operating system installation method

4. Click Browse —> Browse Local to choose the ISO image of the operating system you want to install. This example will set the ISO file at /root/Fedora-Workstation-Live-x86_64-36-1.5.iso.

Choosing the ISO installation file
Choosing the ISO installation file

5. Click on Forward after choosing the ISO.

Click Forward after choosing the ISO file
Click Forward after choosing the ISO file

6. In the next window, adjust your VM’s CPU count and memory allocation. Ensure that the values you choose are sufficient. This example leaves the default values unchanged. Click Forward to proceed.

Choosing the CPU and Memory settings
Choosing the CPU and Memory settings

7. Next, set the disk image size for this VM. Increase or decrease the disk size as needed. For this demonstration, the disk size of 10 GB is acceptable for the VM. Click Forward to move to the next step.

Allocating the disk space for your VM
Allocating the disk space for your VM

The default location for storing VM disk images is /var/lib/libvirt/images.

8. Next, type in the VM name or leave the default. In this example, the VM name will be fedora01. Leave everything else unchanged and click on Finish to create the VM.

Setting the VM name
Setting the VM name

9. Once KVM has created the VM, the VM console will show up, and you can now install the operating system as usual.

Virtual machine console
Virtual machine console

10. At this point, you now have one VM, as you can see in the Virtual Machine Manager. After finishing the operating system install, shut down the VM by clicking the power button icon.

Shutting down the VM after installing the guest operating system
Shutting down the VM after installing the guest operating system

Using the virt-install (CLI)

If you prefer working with the command line, virt-install will be your tool when creating new VMs. Run the below command to create a VM with the following details.

For the complete details, refer to the virt-install man page or run virt-install –help.

  • --name Fedora02 – Set the VM name as Fedora02.
  • --vcpu 1 – Allocate 1 vCPU to the VM.
  • --memory 1024 – Allocate 1024MB of RAM to the VM.
  • --cdrom /root/Fedora-Workstation-Live-x86_64-36-1.5.iso – Specifies the ISO file location containing the operating system installer.
  • --disk size=10 – Specifies the VM disk size in GB (10).
  • --graphics listen=0.0.0.0 – Configures the display server to listen on all interfaces.
virt-install --name Fedora02 --vcpu 1 --memory 1024 --cdrom /root/Fedora-Workstation-Live-x86_64-36-1.5.iso --disk size=10 --check disk_size=off

If you run the command from the terminal in the Ubuntu host with a desktop environment, the virt-install automatically launches the view-viewer window displaying the VM console.

Running virt-install on Ubuntu with a desktop environment
Running virt-install on Ubuntu with a desktop environment

If you executed the virt-install command from a remote SSH terminal or a host without a desktop environment, you’d get the below result.

Running virt-install on Ubuntu without a desktop environment
Running virt-install on Ubuntu without a desktop environment

In this case, you must access the KVM guest console from another machine capable of running graphical applications using the virt-viewer client.

Note: You must install the virt-viewer client for your operating systems, such as Windows and Linux. Follow this link for the download and install instructions.

Managing VMs in the Command Line

Now that you have created some VM, the hardest part is over. Next is learning how to manage your VMs from the command line. The management part may vary slightly depending on whether you are using the virt-manager GUI tool or virsh CLI.

In this section, you will learn how to perform some of the most common VM management tasks from virsh CLI.

Listing VMs

You first need to know how to list the existing VMs on your system. To do so, run the below command.

virsh list --all

As you can see below, there are currently two VMs. The list also shows each VM’s state.

Listing Existing VMs
Listing Existing VMs

Shutting Down VMs

The next thing you would want to do is to shut down a VM when you no longer need them running.

To gracefully shut down a VM, run the virsh shutdown command followed by the VM name. For example, run the below command to shut down the Fedora02 VM.

virsh shutdown Fedora02
Gracefully shut down a VM
Gracefully shut down a VM

The guest operating system running inside the VM can be unresponsive, and you can not even restart it from the virsh CLI. In such cases, run the virsh destroy command to stop the VM forcefully.

virsh destroy Fedora02
Forcefully stop the VM
Forcefully stop the VM

Starting a VM

When you need to start the VM again, run the virsh start command followed by the VM name. The below example will start up Fedora02.

virsh start Fedora02
You can start the VM again
You can start the VM again

Deleting a VM

When you no longer need a VM, you can delete it to free up resources on your system.

1. First, stop the VM instance. You cannot delete a VM that is running.

virsh destroy Fedora02

2. Get the VM’s associated storage. This example gets the storage list for Fedora02.

virsh domblklist Fedora02

Copy the Source value of the storage to delete.

Getting the VM storage list
Getting the VM storage list

3. Now, run the below command to delete the VM and its disk image. The virsh undefine deletes the VM, and the –storage option accepts the comma-separated list of storage to remove.

virsh undefine Fedora02 --storage /var/lib/libvirt/images/Fedora02-1.qcow2
Deleting a VM and its storage
Deleting a VM and its storage

Conclusion

In this article, you learned how to install KVM on Ubuntu Linux and create and manage virtual machines using the virsh CLI tool. You also learned how to use virt-manager to create virtual machines graphically.

KVM is a powerful and free virtualization solution that can run multiple virtual machines on a single host. KVM has many advanced features that make it suitable for running production workloads.

You can run thousands of KVM Guests on Amazon bare metal servers to save money on your AWS bill. You can also use KVM for disaster recovery by replicating your physical servers to Amazon EC2 instances.

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!