How to Install KVM Linux | Guide to Scalable Linux Servers

Dependable technician giving kvm tutorial structured towards virtualization in Linux kvm server

Throughought our data center operations at IOFLOOD, we’ve needed the best virtualization tools to maximize resources on our bare metal cloud servers. An optimized kvm setup is fundamental to achieve this goal. This article provides a comprehensive guide on a Linux KVM install, to assist our customers diving into virtualization.

In this Linux KVM tutorial, we will guide you on the proper methods to install the KVM command. We will show you kvm setup methods for both APT and YUM-based distributions, delve into compiling KVM from source, installing a specific version, and finally, how to use KVM virtualization and ensure that the command is installed correctly.

So, let’s dive in and begin the Linux KVM Tutorial!

TL;DR: How to Install KVM Server in Linux?

Most Linux distributions come with KVM pre-installed. If not, you can install all necessary packages with the package manager used on your system, sudo [apt] install qemu qemu-kvm libvirt-daemon libvirt-clients bridge-utils virt-manager. For example, on Ubuntu, you can run the following command:

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

This command will install KVM along with the necessary libraries and utilities. The libvirt-daemon-system package provides the system-level daemon that manages virtual machines, while libvirt-clients provides the client tools. bridge-utils is a set of utilities for configuring ethernet bridging, which is often used in KVM networking.

This is just the basic way to install KVM on Linux, but there’s much more to learn about installing and using KVM. Continue reading for more detailed information and advanced usage scenarios.

Why and How to Install KVM on Linux

Kernel-based Virtual Machine (KVM) is a full virtualization solution for Linux. It turns the Linux kernel into a hypervisor, allowing a host machine to run multiple, isolated virtual environments, or ‘guests’. KVM is used for running virtual machines on Linux, serving as an alternative to proprietary solutions like VMware or VirtualBox.

Install KVM with APT

If you’re using a Debian-based distribution like Ubuntu, you’ll use the APT package manager to install KVM. Here’s how you can do it:

sudo apt update
sudo apt install qemu qemu-kvm libvirt-daemon libvirt-clients bridge-utils virt-manager

The command sudo apt update updates your package manager’s repository information. The second command installs KVM along with other necessary packages. qemu-kvm is the main KVM package. libvirt-daemon and libvirt-clients are used to manage KVM at the system and client level, respectively. bridge-utils helps set up networking for the virtual machines, and virt-manager provides a graphical interface to manage your virtual machines.

Install KVM with YUM

For CentOS or other Red Hat-based distributions, you’ll use the YUM package manager. Here’s how to install KVM using YUM:

sudo yum update
sudo yum install qemu-kvm libvirt libvirt-python libguestfs-tools virt-install

The sudo yum update command updates your package manager’s repository information. The second command then installs KVM and the necessary packages. libvirt-python and libguestfs-tools are additional libraries that help manage KVM. virt-install is a command-line tool used to create new virtual machines.

Install KVM with Pacman

If you’re using an Arch-based distribution like Manjaro, you’ll use the Pacman package manager. Here’s how to install KVM using Pacman:

sudo pacman -Syu
sudo pacman -S qemu virt-manager virt-viewer dnsmasq vde2 bridge-utils openbsd-netcat

The command sudo pacman -Syu updates your package manager’s repository and upgrades all your system’s packages. The second command installs KVM along with other necessary packages. dnsmasq and vde2 are used for networking, openbsd-netcat is a networking utility, and virt-viewer provides a graphical interface to view your virtual machines.

KVM Setup from Source Code

Installing KVM from source code gives you the most control over the installation process. You can customize the installation to fit your specific needs and even contribute to the project if you’re so inclined.

git clone https://github.com/qemu/qemu.git

cd qemu

./configure

make

sudo make install

This sequence of commands clones the QEMU (the software that provides hardware emulation for KVM) repository, navigates into the downloaded directory, configures the build process, compiles the source code, and installs the compiled binaries.

Different Versions of KVM

Install KVM Versions From Source Code

To install a specific version of KVM from source, you need to checkout the specific version tag after cloning the repository. For example, to install version 2.12.0, you would use the following commands:

git clone https://github.com/qemu/qemu.git
cd qemu
git checkout v2.12.0
./configure
make
sudo make install

How to InstalL KVM Using Package Managers

APT

On Debian-based distributions, you can specify a package version to install with APT. However, the available versions are limited to those in the repositories your system is configured to use. Here’s how you can install a specific version:

sudo apt-get install qemu-kvm=2.12.0-1ubuntu18.04.6

YUM

On Red Hat-based distributions, you can use the yum command to install a specific version of a package. Like with APT, the available versions are limited to those in your configured repositories. Here’s how you can install a specific version:

sudo yum install qemu-kvm-2.12.0-1.el7

Version Comparison

Different versions of KVM come with different features, bug fixes, and compatibility changes. Here’s a brief comparison of some recent versions:

VersionKey Changes
2.12.0Added support for ARMv8.4 architecture, improved device emulation
2.11.0Added support for Intel Processor Trace, improved Windows guest support
2.10.0Added support for AMD Secure Memory Encryption, improved graphics emulation

Using and Verifying New KVM Setup

Using KVM Virtualization

To create a new virtual machine with KVM, you can use the virt-install command. Here’s an example:

virt-install --name ubuntu_vm --ram 4096 --disk path=/var/lib/libvirt/images/ubuntu_vm.img,size=20 --vcpus 2 --os-type linux --os-variant ubuntu18.04 --network bridge=virbr0 --graphics none --console pty,target_type=serial --location 'http://us.archive.ubuntu.com/ubuntu/dists/bionic/main/installer-amd64/' --extra-args 'console=ttyS0,115200n8 serial'

This command creates a new Ubuntu 18.04 virtual machine with 4GB of RAM, 2 CPU cores, and a 20GB disk image.

Verifying Installation

To verify that KVM is installed correctly, you can use the virsh command to list your virtual machines:

virsh list --all

If KVM is installed correctly, this command will list all your virtual machines, or return an empty list if you don’t have any.

Alternatives to KVM Virtualization

While KVM is a robust and popular tool for virtualization in Linux, it’s not the only game in town. Let’s explore some alternative methods for virtualization, namely Xen and VirtualBox.

Diving into Xen

Xen is a bare metal hypervisor providing services that allow multiple computer operating systems to execute on the same computer hardware concurrently. Let’s see how to install Xen on Ubuntu:

sudo apt-get install xen-hypervisor

After installation, you need to add Xen to the GRUB menu:

sudo sed -i 's/GRUB_DEFAULT=0/GRUB_DEFAULT="Xen 4.1-amd64"/' /etc/default/grub
sudo update-grub

After rebooting, you can check the Xen kernel version:

uname -r

# Output:
# 4.1.0-0.bpo.2-amd64

Xen provides a high level of performance and scalability but requires more technical expertise to set up and manage compared to KVM.

Venturing into VirtualBox

VirtualBox is an open-source virtualization software package for x86 virtualization, developed by Oracle. It provides a graphical user interface and is easier to set up for beginners. Let’s install VirtualBox on Ubuntu:

sudo apt-get install virtualbox

You can verify the installation by running VirtualBox from the command line:

virtualbox

This command will open the VirtualBox GUI, where you can create and manage your virtual machines.

VirtualBox is user-friendly and suitable for desktop users who want to run a different operating system in a window. However, it lacks the performance and scalability of KVM and Xen.

Xen vs VirtualBox vs KVM: A Comparison

FeatureKVMXenVirtualBox
PerformanceHighHighModerate
ScalabilityHighHighLow
Ease of UseModerateLowHigh
Use CaseServers, CloudsServers, CloudsDesktops

In conclusion, while KVM is a solid choice for most Linux users, Xen and VirtualBox offer viable alternatives depending on your specific needs and expertise level.

Troubleshooting Common KVM Issues

While KVM is a powerful tool, you might encounter some issues during its installation or usage. Let’s discuss some common problems and their solutions.

Verifying KVM Virtualization Support

KVM requires that your processor supports hardware virtualization. If KVM fails to install or start, it might be because this feature isn’t enabled in your BIOS/UEFI settings. You can check if your processor supports hardware virtualization with the following command:

egrep -c '(vmx|svm)' /proc/cpuinfo

# Output:
# 4

If the output is 0, your processor doesn’t support hardware virtualization, or the feature isn’t enabled in your BIOS/UEFI settings. If the output is greater than 0, your processor supports hardware virtualization.

Solving Issues in a KVM Setup

If KVM fails to install, it might be because your system’s package lists are outdated. You can update them with the following commands:

sudo apt update  # For Debian-based distributions
sudo yum update  # For Red Hat-based distributions
sudo pacman -Syu  # For Arch-based distributions

After updating your package lists, you can try installing KVM again.

Addressing Network Issues

If your virtual machines can’t connect to the network, it might be because the network bridge isn’t configured correctly. You can list your network bridges with the following command:

brctl show

# Output:
# bridge name     bridge id               STP enabled     interfaces
# virbr0          8000.525400c7e4f3       yes             virbr0-nic
#                                                         vnet0

If your bridge isn’t listed or doesn’t have any interfaces, you’ll need to configure it. The process varies depending on your network setup and distribution, so consult your distribution’s documentation for detailed instructions.

Remember, troubleshooting is a key skill in any IT field. Don’t be discouraged by these issues; they’re opportunities to learn and become a more proficient Linux user.

Understanding KVM Virtualization

Virtualization is a technology that allows you to create multiple simulated environments or dedicated resources from a single, physical hardware system. It’s a way of maximizing physical resources to get the most out of an investment in hardware.

Virtualization: The What and the Why

In essence, virtualization lets you split one physical machine into multiple ‘virtual’ machines, each with its own operating system and applications. This technology has revolutionized the IT industry by allowing more efficient utilization of computer hardware.

# Let's list the VMs on our system
virsh list --all

# Output:
#  Id   Name   State
# ----------------------
#  -    VM1   shut off
#  -    VM2   shut off

In the above example, virsh list --all command lists all the virtual machines on the system. Here, VM1 and VM2 are separate virtual machines, each capable of running its own operating system and applications, yet both hosted on the same physical machine.

Kernel-based Virtual Machine (KVM)

KVM is a full virtualization solution for Linux on x86 hardware containing virtualization extensions (Intel VT or AMD-V). It consists of a loadable kernel module, kvm.ko, that provides the core virtualization infrastructure and a processor-specific module, kvm-intel.ko or kvm-amd.ko.

KVM also requires a modified QEMU although work is underway to get the required changes upstream.

Using KVM, one can run multiple virtual machines running unmodified Linux or Windows images. Each virtual machine has private virtualized hardware: a network card, disk, graphics adapter, etc.

# Let's check the version of KVM installed on our system
kvm --version

# Output:
# QEMU emulator version 2.11.1(Debian 1:2.11+dfsg-1ubuntu7.36)

In the above example, kvm --version command displays the version of KVM installed on the system. This can be helpful when troubleshooting or when certain features are only available in specific versions.

Understanding these fundamentals is key to leveraging the power of KVM and virtualization in general. With this knowledge, you can make informed decisions about your virtualization needs.

Practical Uses of KVM Virtualization

Cloud Computing and KVM Virtualization

Virtualization is a cornerstone of cloud computing. It allows for the creation of ‘virtual’ resources, such as servers, storage, and networks. These resources can be provisioned as needed, providing the flexibility and scalability that define cloud computing.

KVM plays a significant role in this aspect. It allows cloud service providers to create and manage virtual machines on their Linux servers, maximizing their hardware utilization and providing customers with customizable computing resources.

# Creating a VM in a cloud environment using KVM
virt-install --name cloud_vm --ram 8192 --disk path=/var/lib/libvirt/images/cloud_vm.img,size=50 --vcpus 4 --os-type linux --os-variant ubuntu18.04 --network bridge=virbr0 --graphics none --console pty,target_type=serial --location 'http://us.archive.ubuntu.com/ubuntu/dists/bionic/main/installer-amd64/' --extra-args 'console=ttyS0,115200n8 serial'

# Output:
# 'cloud_vm' created

In the above example, we’re creating a virtual machine in a hypothetical cloud environment. This VM has 8GB of RAM, 4 CPU cores, and a 50GB disk image. It’s running Ubuntu 18.04 and is connected to the network via a bridge.

Virtualization in Server Management

In server management, virtualization allows for better resource allocation and isolation. It enables running multiple applications or services on the same server, each within its own virtual machine. This approach prevents applications from interfering with each other while maximizing hardware utilization.

KVM is a popular choice for server virtualization due to its performance and scalability. It’s used in many enterprise environments, powering everything from small businesses’ servers to data centers.

Exploring Containerization

While virtualization involves running multiple operating systems on a single hardware system, containerization involves running multiple applications on a single operating system with isolation. This approach has gained popularity due to its lightweight nature and the rise of microservices architecture.

While KVM is not a containerization tool, understanding it can provide a solid foundation for learning about containerization. Tools like Docker and Kubernetes have become industry standards in this field.

Further Resources for KVM Virtualization

  1. Proxmox Virtual Environment: Detailed guides and tutorials on using KVM and other virtualization technologies in Red Hat environments.

  2. Linux KVM: The official KVM website, featuring a wealth of resources, including a user guide, FAQ, and links to community resources.

  3. Red Hat Virtualization Documentation: Proxmox VE is a complete open-source platform for all-inclusive enterprise virtualization that tightly integrates KVM hypervisor and LXC containers, software-defined storage and networking functionality on a single platform.

Recap: KVM Tutorial for Linux

In this comprehensive guide, we’ve delved into the process of installing and using KVM (Kernel-based Virtual Machine) on Linux. We’ve explored how this robust tool allows you to create and manage virtual machines, turning your Linux system into a personal cloud.

We started with the basics, explaining how to install KVM on different Linux distributions and how to use it to create a virtual machine. We then ventured into more advanced territory, discussing how to install KVM from source code, how to install different versions, and how to use KVM effectively.

Along the way, we tackled common challenges you might encounter when using KVM, such as verifying hardware virtualization support, solving installation failures, and addressing network issues. We provided you with practical solutions and examples to help you overcome these hurdles.

We also looked at alternative approaches to virtualization in Linux, comparing KVM with other tools like Xen and VirtualBox. Here’s a quick comparison of these methods:

MethodPerformanceScalabilityEase of Use
KVMHighHighModerate
XenHighHighLow
VirtualBoxModerateLowHigh

Whether you’re a beginner venturing into Linux virtualization or an experienced sysadmin looking to level up your skills, we hope this guide has given you a deeper understanding of KVM and its capabilities.

With its balance of performance, scalability, and moderate ease of use, KVM is a powerful tool for Linux virtualization. Now, you’re well equipped to harness its power. Happy virtualizing!