Help Docs Server Administration Linux Server Administration How to Install KVM on Ubuntu — Step-by-Step Guide

How to Install KVM on Ubuntu — Step-by-Step Guide

Install KVM on Ubuntu 22.04. A step-by-step tutorial on KVM installation, network bridging, and using virt-install to create your first virtual machine.

Introduction

Virtualization lets you run multiple operating systems on a single physical server, which saves costs and boosts efficiency. On Ubuntu, the Kernel-based Virtual Machine (KVM) provides a stable, open-source virtualization platform that turns your server into a flexible environment for hosting virtual machines (VMs).

Prerequisites

Before you start, make sure your system meets these requirements:

  • Operating system: Ubuntu 22.04 LTS
  • CPU: Intel VT-x or AMD-V enabled (hardware virtualization support)
  • RAM: At least 4 GB (more is recommended for multiple VMs)
  • Disk space: Minimum 20 GB free for VM images

Instructions

  1. Check for CPU virtualization support. Run the following command to see if your CPU supports hardware virtualization. If the output is greater than 0, your CPU has the necessary support.
egrep -c '(vmx|svm)' /proc/cpuinfo
  1. Update your system. Ensure your system is up-to-date by running the following command:
sudo apt update && sudo apt upgrade -y
  1. Install KVM, Libvirt, and related utilities. Use this command to install the necessary packages. qemu-kvm is the main virtualization package, and libvirt-daemon-system manages the VMs. virt-manager provides a graphical interface.

sudo apt install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virt-manager virt-viewer virt-install
  1. Add your user to the libvirt group. This allows you to manage VMs without sudo. After running this, log out and log back in for the changes to take effect.

sudo usermod -aG libvirt $USER
  1. Verify the KVM module is loaded. Run this command to check if the KVM kernel module is active. A successful output will show kvm_intel or kvm_amd.

lsmod | grep kvm
  1. Create a bridge interface for your virtual machines. This allows your VMs to communicate with the external network. First, open the netplan configuration file. The file name may vary, but it’s typically located in /etc/netplan/.

ls /etc/netplan/
  1. Edit the netplan file. Open the identified file with your preferred text editor (e.g., nano).
sudo nano /etc/netplan/00-installer-config.yaml
  1. Replace the file content with a bridge configuration. The example below configures br0 to use DHCP. Note: The eno1 should be replaced with your actual physical network interface name (e.g., eth0, enp3s0). You can find this by running the ip a command.

YAML
network:
ethernets:

    eno1:

      dhcp4: false

  bridges:

    br0:

      dhcp4: true

      interfaces:

        - eno1

  version: 2

  

  1. Apply the netplan configuration. Apply the new network configuration. This will restart your networking services and create the br0 bridge.
sudo netplan apply
  1. Create your first virtual machine using the virt-install command.

    The example below creates a VM named ubuntu-server-vm with 2GB of RAM, 2 vCPUs, and a 20GB disk. It uses a local ISO file for installation. Note: Update the ISO path and VM name as needed.

sudo virt-install --name ubuntu-server-vm --ram 2048 --vcpus 2 --cpu host --hvm
  • –disk path=/var/lib/libvirt/images/ubuntu-server-vm.qcow2,size=20
  • –cdrom /path/to/ubuntu-server.iso –graphics vnc
  • –network bridge=br0
  • –name: Unique name for the VM.
  • –ram: Memory allocation in MB.
  • –vcpus: Number of virtual CPUs.
  • –disk: Disk image path and size.
  • –cdrom: Path to the bootable ISO file.
  • –graphics vnc: Enables VNC for graphical access.
  • –network bridge=br0: Connects the VM to the bridge network.

 Find your VNC display port by running the following command:

sudo virsh domdisplay ubuntu-server-vm

Frequently asked questions

  • Check your netplan configuration and ensure the bridge interface is correctly set up with the right physical interface.
  • Ensure your firewall (e.g., ufw) is not blocking traffic. You may need to allow traffic on the br0 interface.

  • Confirm the path to your ISO file is correct and the file is not corrupted.
  • The user running the command must have read access to the ISO file.

Verify that your firewall allows traffic on the VNC port (usually starting from 5900).


Managing your Virtual Machine After Installation

Want to learn more about managing your virtual machine? Check out our article on Managing your virtual machine with Virsh.

Graphical Management:

Use virt-manager to manage your VMs with a graphical interface.


sudo virt-manager

Command-Line Management:

  1. List all VMs
 sudo virsh list --all
  1. Start a VM
 sudo virsh start your_vm_name
  1. Shut down a VM
 sudo virsh shutdown your_vm_name

Was this article helpful?