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.
Prerequisites
- A system running Ubuntu.
- An account with root privileges.
- Access to the terminal.
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
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:
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:
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:
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.
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.
3. Select the option to install the VM using an ISO image and click Forward.
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.
6. Allocate sufficient hard disk space to the VM. Click Forward to go to the last step.
7. Specify the name for your VM and click Finish to complete the setup.
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:
Option | Description |
---|---|
--name | The virtual machine's name. |
--description | A short VM description. |
--ram | The amount of RAM you wish to allocate to the VM. |
--vcpus | The number of virtual CPUs you wish to allocate to the VM. |
--disk | The VM's location on your disk (if you specify a qcow2 disk file that does not exist, it will be automatically created). |
--cdrom | The location of the ISO file you downloaded. |
--graphics | Specifies 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.
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.