How to Install KVM on Ubuntu 24.04: Step-By-Step
Within the Linux kernel resides a type 1 hypervisor known as KVM (Kernel-based Virtual Machine) utilized to enable virtualization by leveraging hardware resources to establish and oversee virtual machines (VMs). KVM stands out as a favored option, for developers seeking isolated environments, on a device.
This tutorial aims to assist you in setting up KVM on Ubuntu 24.04 for virtual machine management.
#What is KVM?
The Kernel-based Virtual Machine (KVM) serves as a virtualization solution that is embedded within the Linux kernel to transform Linux into a Type 1 hypervisor directly interacting with hardware systems without the need for an operating system layer like Type 2 hypervisors require. Integrating processor extensions like Intel VT x or AMD V into its operation allows KVM to virtualize hardware components and create environments for hosting operating systems and their associated applications and services. It is beneficial for developers and system administrators looking to efficiently manage and operate multiple operating systems within a single computing environment.
#What is KVM used for?
KVM allows you to run various operating systems as virtual machines. It’s widely used for server virtualization, development environments, and testing setups where isolated instances are necessary. Because it leverages Linux’s capabilities, KVM offers native performance levels, making it a preferred choice for both local and cloud-based virtualization.
#Prerequisites
Make sure you have an Ubuntu 24.04 operating system, with the following requirements to proceed with this tutorial;
- 64 bit processor with hardware virtualization support, like Intel VT x or AMD V.
- 4 GB of RAM
- Storage: At least 20 GB of free disk space
Deploy and scale your projects with Cherry Servers' cost-effective dedicated or virtual servers. Enjoy seamless scaling, pay-as-you-go pricing, and 24/7 expert support—all within a hassle-free cloud environment.
#How to install KVM on Ubuntu 24.04
Installing KVM is relatively straightforward and takes a few commands to be ready to use on Ubuntu, so let's dive right in. Follow the steps below to install KVM on the Ubuntu 24.04 system.
#Step 1: Verify hardware virtualization support
KVM relies on hardware virtualization extensions (Intel VT or AMD-V) to provide efficient performance. This support is enabled in the CPU and system firmware for KVM functionality to be active. The following steps below show how you can do this.
#1. Check virtualization support
Use the lscpu
command to confirm if your CPU supports virtualization. You should see either “vmx” (Intel) or “svm” (AMD) flag in the output.
lscpu | grep -E 'vmx|svm'
If the output includes “vmx” or “svm”, your CPU supports virtualization. If not, KVM cannot run on your system.
#2. Enable virtualization in BIOS/UEFI
If virtualization isn’t enabled, reboot and access BIOS/UEFI settings (usually by pressing keys like F2
, F10
, or Delete
during startup). Locate virtualization options under CPU configuration and enable Intel VT or AMD-V.
#Step 2: Install KVM and required packages
Once hardware support is verified, install KVM and additional packages, such as libvirt
for VM management and virt-manager
for a graphical interface.
sudo apt update
sudo apt install -y qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virt-manager
-
qemu-kvm
: Installation of the KVM module and tools. -
libvirt-daemon-system
andlibvirt-clients
: Handle virtualization management services. -
bridge-utils
: Helps configure networking for VMs. -
virt-manager
: a GUI to manage virtual machines. This is not a compulsory addition for your virtual machine to work, but you might want to add this if needed.
#Step 3: Verify the KVM installation
Confirm that KVM is installed correctly and the modules are loaded.
#1. List VMs
Check the list of all the active and inactive virtual environments:
```bash
sudo virsh list --all ```
#2. Check KVM status
Use the following commands to ensure KVM is active:
sudo systemctl status libvirtd
If libvirtd
is running, KVM is properly set up.
#2. Verify module loading
Confirm the presence of the KVM modules:
lsmod | grep kvm
The image above shows that my system has 'kvm' and 'kvm_intel' listed as loaded modules which means that KVM is set up and ready to be used.
#Step 4: Add user to KVM and Libvirt groups
To manage virtual machines without root privileges, add your user to the kvm
and libvirt
groups.
sudo usermod -aG kvm,libvirt $USER
newgrp libvirt
This grants your user permission to run VMs without elevated privileges. Log out and log back in to activate group membership changes.
#Step 5: Configure networking for virtual machines
Networking setup allows VMs to connect to the network or the internet. By default, KVM provides Network Address Translation (NAT), which is suitable for most basic setups.
#1. Understanding network modes
KVM supports several network modes—NAT, bridged, and isolated.
- NAT: Allows VMs internet access through the host but isolates them from external networks.
- Bridged: Connects VMs directly to the host’s network, making them accessible to external devices.
#2. Verify NAT configuration
The default NAT network configuration for KVM can be verified by checking the default
network in libvirt
:
sudo virsh net-list --all
If it shows as inactive, start it:
sudo virsh net-start default
sudo virsh net-autostart default
This ensures NAT networking for your virtual machines.
#Step 6: Launch Virt-Manager and create a virtual machine
Virt-Manager offers a straightforward GUI to create and manage virtual machines. In this step, you will learn how to run an Ubuntu 18.04 using the virt-Manager tool. I already have the ISO file locally, so you might need to download that first or use what you have already
#1. Open Virt-Manager
Launch it via the terminal or application menu:
sudo virt-manager
Note: The virt-manager
command won’t work if you are using a virtual machine because it requires access to a graphic display.
#2. Create a new virtual machine
- Click "File" > "New Virtual Machine" to start the VM creation wizard.
- Click “Browse…” to locate ISO media volume
- Click “Browse Local” and choose a local ISO file.
- Open the file you have selected
- Click “Foward” button to go to the next step.
- Allocate appropriate resources. Generally, 1-2 GB RAM and 1 CPU core are adequate for simple setups.
- Allocate virtual disk space, ideally more than 20 GB for a general-purpose OS.
- Click on finish to start the creation process
A window to continue the installation of the operating system will be provided after some time.
After the virtual machine is created, you should see it on your list of virtual machines.
sudo virsh list --all
#Step 7: Managing KVM virtual machines
For command-line management, KVM provides several utilities to control VM states.
#1. Start a VM
sudo virsh start <vm-name>
#2. Stop a VM
sudo virsh shutdown <vm-name>
#3. Pause and Resume
You can suspend a VM’s state and resume it later.
sudo virsh suspend <vm-name>
sudo virsh resume <vm-name>
These commands allow for easy virtual machine management directly from the terminal.
#Conclusion
In this guide, you’ve configured KVM on Ubuntu 24.04, set up network and user permissions, and created a virtual machine with Virt-Manager. With KVM fully installed and operational, you can now create and manage VMs on your system, leveraging Ubuntu’s robust virtualization capabilities. To build on the knowledge you have gotten here, consider experimenting with bridged networking or automated VM provisioning.
For additional guidance and troubleshooting tips, check out the official KVM documentation and Libvirt’s user guide, which provide comprehensive resources on advanced configurations, performance tuning, and automation options.
Cloud VPS - Cheaper Each Month
Start with $9.99 and pay $0.5 less until your price reaches $6 / month.