How to Install QEMU and Near-Native VMs on Linux: Ultimate Guide

How to Install QEMU and Near-Native VMs on Linux: Ultimate Guide

how-to-install-qemu-on-linux

Hi all, welcome to my blog, MusaBase! In this comprehensive guide, I'll walk you through installing QEMU and creating KVM that deliver near-native performance on Linux. Traditionally, many users have relied on separate installation to run different operating system, which requires restarting your machine every time you switch. With virtualization, you can run multiple environment simultaneously with performance that rivals native hardware. In this guide I'm using Arch Linux with KDE Plasma as its desktop environment, but you can follow this guide on any linux distribution with only minor adjustments to package names or your package manager.







What is QEMU?

QEMU, short for Quick Emulator is a free, open-source emulator and virtualizer that can run entire operating system or individual programs.It leverages dynamic binary translation to support multiple CPU architectures. Developers use it for cross-platform testing, debugging, and isolated deployments. When paired with KVM , it delivers near-native performance. Its flexibility, protability, and cost-effectiveness make it a top choice over proprietary solutions.




Prerequisites

To run QEMU effectivley on Linux, you need a recent 32- or 64-bit Linux distribution. Many full-featured distros, such as Ubuntu, Linux Mint, Fedora and openSUSE typically come with standard build tools (like GCC, make, and pkg-config). However, if you're using a minimal or non-development-focused system (or a distro like Arch Linux without the base-devel group pre-installed), you may need to manually install these packages along with necessary libraries like glib2.0, libfdt, pixman and zlib.
Specifications:

  • Processor: Intel or AMD x86-64-bit compatible with virtualization support.
  • Ram: 8GB or more for higher performance.
  • Hard-Drive: Based on your needs. Basically you will be allocating space according to the OS or program you will insatll and run.
  • Operating System: Any recent Linux distribution 32-bit or 64-bit.

Hardware Acceleration
  • Virtualization Extensions:
    • A CPU with virtualization extensions is essential.
    • Intel:Requires VT-x with EPT.
    • AMD: Requires AMD-V with RVI.
    • These both (EPT & RVI) technologies helps in offloading guest memory address translation to hardware, significantly reducing the overhead of virtualizing memory
  • IOMMU Support for Advanced Features (e.g., PCI Passthrough):
    • Intel: VT-d is needed.
    • AMD: AMD-V is required.
    • IMMOU rempas and isolates memory accesses from hardware devices, ensuring that when a PCI card (e.g., GPUs, network cards, sound cards and storage controllers), or similar device is passed through to a virtual machine, it can only access its designated memory space, boosting performance and security.
  • Software vs Hardware Acceleration: If you don't have enough resources or much capable hardware then QEMU also offers only software acceleration.
    • Software Acceleration: QEMU's TCG emulates CPUs entirely on software, which is significantly slower.
    • Hardware Acceleration:Technologies like KVM on Linux allows guest code to run almost natively, delivering much higher performance.

Most modern CPUs even back from 2011-2012 era include built-in virtualization extensions. Many ARM64 processors from 2012 onward also support these features.


If you are not sure whether your CPU supprots virtualization, follows these steps:

  • Reboot your pc.
  • During startup, press the BIOS/UEFI key (commonly F2, F10, F12, Del, Esc, depending on your motherboard).
  • Look for sections labeled Advanced, CPU Configuration, Processor Settings, System Configuration, or Security.
  • Locate Virtualization Options:
    • For Intel CPUs: Look for Intel Virtualization Technology or VT-x.
    • For AMD CPUs: Look for AMD Virtualization (AMD-V) or SVM Mode.
    • Some systems might also show Intel VT-d (for directed I/O virtualization).
  • Enable Virtualization.
  • Save Settings and Exit.
  • Reboot.

Now, in your linux terminal, run::

LC_ALL=C lscpu | grep Virtualization

If your CPU doesn't support virtualization, you can still create and use VMs but then they will rely solely on Software Emulation.




Step 1: Installing QEMU and Core Virtualization Packages

Lets begin the installation of the QEMU package along its full suite of virtualization tools. Although installing QEMU alone will work, adding a few extra packages can significantly enhance your virtualization experience.

Installing Packages

For the best daily VM experience, I recommand installing the following packages. Use the appropriate command for your Linux distribution:


Arch-based distros:

sudo pacman -S qemu-full qemu-img libvirt virt-install virt-manager dnsmasq libosinfo edk2-ovmf

Ubuntu/Debian-based distros:

sudo apt install qemu-system-x86 qemu-utils libvirt-daemon-system libvirt-clients virt-manager virtinst dnsmasq-base libosinfo ovmf

Red Hat-Based distros:

sudo dnf install qemu-kvm qemu-img libvirt virt-install virt-manager dnsmasq libosinfo ovmf

Note: Replace the pacman, apt, dnf package manager command with the one used on your Linux distribution.

Package Name Reference Table

Since package names can vary between distributions, refer to the table below for a quick overview:

Arch-based Ubuntu/Debian-based Red Hat-based
qemu-full qemu-system-x86,
qemu-utils
qemu-kvm,
qemu-img
qemu-img qemu-utils qemu-img
libvirt libvirt-daemon-system,
libvirt-clients
libvirt
virt-install virtinst virt-install
virt-manager virt-manager virt-manager
dnsmasq dnsmasq-base dnsmasq
libosinfo libosinfo-bin libosinfo
edk2-ovmf ovmf ovmf

Note: Avoid running VMs as root user to maintain security and system stability.




Step 2: Enabling and Configurinng Core Virtualization Services

Now we need to enable essential services and adjust configuration file for the packages we installed in Step 1. Follow these steps:

2.1. Enable Virtualization Services:

  • Open your terminal and run the following command to start and enable the libvirtd service immediately:
sudo systemctl enable --now libvirtd

Verify your Init System

  • To verify which Init System your Linux system is using run:
ps --no-headers -o comm 1

For SysVinit:

  • Start the libvirtd service with:
sudo /etc/init.d/libvirtd start
#or
sudo service libvirtd start
  • Enabling the service so it automatically start on boot:
    • This varies significantly between distributions.
    • On Debian-based systems that uses SysVinit:
sudo update-rc.d libvirtd defaults
#or
sudo update-rc.d libvirtd enable
    • On Red Hat-based systems:
sudo chkconfig libvirtd on

For OpenRC:

  • Starting the service:
sudo rc-service libvirtd start
  • Enabling the service so it automatically start on boot:
sudo rc-update add libvirtd default
Important Notes:
  • The SysVinit "enable" step is the most vairable. Distributions using SysVinit have implemented their own vairations of how runlevels and service startup are managed.
  • These commands assume the libvirtd service has been properly installed. If the service files are missing, these commands will fail.

2.2. Add User to the Libvirt Group:

By default, libvirt requires root privileges to manage VMs. Adding your user to the libvirt group allows you to manage virtual machines without sudo, which is both safer and more convenient.

  • To add User to libvert group, run:

Arch-based distros & Red Hat-Based distros:

sudo usermod -aG libvirt $USER
adding-user-to-libvirt-group-for-qemu-to-always-run-as-root

Ubuntu/Debian-based distros:

  • If sudo usermod -aG libvirt $USER doesn't work:
sudo adduser $USER libvirt

Log out and back in to apply group changes.

2.3. Load KVM Kernel Modules:

These modules are required for KVM to work because they enable the CPU’s hardware virtualization features. If these modules aren’t loaded, your system can’t use KVM, and virtualization won’t work properly.

  • For Intel CPUs, type:
sudo modprobe kvm-intel
loading-kvm-kernel-modules-for-intel-cpus

  • For AMD CPUs, type:
sudo modprobe kvm_amd
loading-kvm-kernel-modules-for-amd-cpus

2.4. Configure Libvirt (Optional):

  • Open the libvirt configuration file with your preferred text editor (e.g., kate or nano):
kate /etc/libvirt/libvirtd.conf
OR
nano /etc/libvirt/libvirtd.conf
  • Use the editor's search function ctrl + f to locate and uncomment/modify these lines by removing the leading (#):
unix_sock_group = "libvirt"
unix_sock_rw_perms = "0770"

2.5. Restart the Libvirt Service:

  • Apply any configuration changes by restarting the libvirtd service:
sudo systemctl restart libvirtd

If you're using a different init system, adjust the command accordingly. For example:

  • SysVinit:
sudo service libvritd restart
#OR
sudo /etc/init.d/libvirtd restart
  • OpenRC:
sudo rc-service libvirtd restart




Step 3: Launch QEMU & Virt-Manager

Launch Virt-Manager:

virt-manager

We have now successfully configured a working virtual machine environment. By following the above steps, we ensure that KVM is properly enabled, users have the right permissions, and virtual machines can be created and managed efficiently.

Understanding QEMU Basics

Before you start using QEMU in depth, here are a few important points to understand first:

QEMU Core:

  • QEMU itself is primarily a command-line tool. This means that its fundamental operations relies on typing commands into a terminal. So in it's basic form, these is no Graphical User Interface.
  • However, QEMU does have UI subsystem, that allows for different user interfaces to be creatd.
  • Refer to QEMU documentation for more structured details and advanced configurations.

Graphical User Interfaces (GUI) and Launchers:

  • Several GUI are available to simplify QEMU usage. The most prominent example is virt-manager.
  • virt-manager provides a user-friendly interface for managing virtual machines, and it heavily relies on QEMU and libvirt.
  • Therefore, while QEMU's core is commadn-line based, it's frequently used with GUIs that make it more accessible.



Step 4: Installing Windows in QEMU’s Virtual Machine

Now, lets test if our setup acutally works by installing an Operating System in our Virtual Machine. This process involves:

  • Creating a Virtual hard drive for the OS installation.
  • Attaching anOS Image.
  • Allocating Ram and CPU cores.
  • Passing USB-Devices(mouse, keyboard, audio devices).
  • Enabling hardware acceleration for better performance.

I'll demonstrate using Windows 11, but you can use any OS.ISO file. You can also try recovery images (usually in .bin or .img formats) with a few tweaks as well.

We can use Virt manager by 2 different approach to create Virtual machines.

  • GUI Approach
  • CLI Approach

4.1. GUI Approach

4.1.1: Select Architecture and Installation Method

  • Open Virt-Manager: Launch the application from your desktop environment or by running virt-manager in a terminal.
  • Create a New VM: Click the Create a New virtual machine icon 🖥️ in the toolbar.
  • Choose Installation Source:
    • Import Existing Disk Image: Select this if you already have an OS image (ISO or IMG) prepared.
    • Local Install Media: Choose this to install a fresh operating system from an ISO or other bootable image.
  • Pick Architecture: At the Select Architecture options, select the CPU architecture for your VM, such as x86_64, aarch64, arm, or s390x. For this guide, i am choosing Local Install Media with x86_64, then click Forward.

4.1.2: Create a New Storage Pool

A storage pool in libvirt is a way to organize and manage your VM disks and ISOs:

  • In step 2 screen click on Browse to open the Pools windows.
  • Click the ➕ Add icon define a new pool.
  • On the Add a New Storage Pool you can name your pool.
  • Name your pool, choose the Directory pool type, and set its path (default is /var/lib/libvirt/images/pool). To keep things tidy, create a custom directory, like (/mnt/sdc1/VirtualOS/windows11pro).
  • Click Finish to create and activate the pool.

4.1.3: Select Operating System Image

  • Back in the VM creating wizard, select your newly created storage pool.
  • If your ISO isn't already in the pool, place it there now, then click Browse and select it.
  • Virt-Manager will auto-detect the OS, if not, enter the OS name manually and click Forward.

I have already pasted my windows11.iso file inside this directory so i will select it.


4.1.4: Allocate RAM and CPUs

  • On the next screen, allocate memory and CPU cores according to your needs (e.g., 8 GB RAM, 4 CPUs. then click Forward.
virt-manager-allocating-ram-and-cpus

4.1.5: Create a Virtual Storage Drive

  • In the storage configuration step, choose Select or create custom storage and click Manage.
  • Select your storage pool, click ➕ Create Volume, and specify:
    • Name: e.g., win11_drive.
    • Format: qcow2 (supports compression and snapshots) or raw (higher performance).
    • Capacity: e.g., 64 GB for Windows 11.
  • Click Finish, then Choose Volume and ,Forward.
virt-manager-creating-virtual-storage-drive

selecting-custom-crated-storage-pool-and-adding-virtual-storage-drive

4.1.6: Finalizing the VM Setup

  • On final screen, name your VM (e.g., "Windows 11 VM") and confirm the Netwokr Type (default: Virtual network 'default').
  • (Optional) Tick the Customize configuration before install to tweak hardware settings. For a basic setup, leave defaults.
  • Click Finish to launch your VM with the specified settings.

4.2. CLI Approach

4.2.1: Getting things ready

Before booting QEMU with the Windows 11 ISO, we need 2 files:

I have the ISO file already so i will create a virtual hard-drive.

But before that i would recommend creating a separate empty folder for each OS in which you will store operating system image file, virtual hard disk file and any other files needed for OS to run in QEMU. For instance, i have an empty folder named VirtualWindows11. I will cd into this folder from my terminal like this:

cd /mnt/sdc1/VirtualWindows11

Now, in this folder, i will create a virtual disk of 64GB of storage for Windows 11.

4.2.2: Creating Virtual Drive with QEMU

  • Next, create a 64GB virtual disk for Windows 11 with the following command:
qemu-img create -f qcow2 virtualdrive.qcow2 64G
qemu-img-command-to-create-virtual-drive-img

4.2.3: Verifying Required Files

Ensure you have the following additional OVMF Firmware files (needed for UEFI support):

  • OVMF.fd
  • OVMF_VARS.fd

I have already saved my windows11.iso file inside my VirtualWindows11 folder with two additional .fd format files, these files are needed to run modern operating systems that require UEFI support, such as recent versions of Windows and Linux.

Please see the image below for clarification:

files-structure-for-qemu-to-run-uefi-operating-system

These 2 additional files comes with the ovmf package and usually located in directories like:

  • usr/share/ovmf/x64/
  • usr/share/ovmf/
  • usr/share/qemu-firmware/

If you can't locate them, run:

sudo find / -name OVMF_VARS.fd
#OR
sudo find / -name OVMF_CODE.fd

Note: On some distributions (for example, Arch Linux) the filenames might be slightly different (e.g., OVMF.4m.fd and OVMF_VARS.4m.fd). In such cases, remove the ".4m" for simplicity.


4.2.4: Install Windows 11 in QEMU

Now that we have everything ready, let's install Windows 11 on QEMU.

  • Open terminal and type the following command:
qemu-system-x86_64 \
    -cpu host -enable-kvm -smp 2 -m 8G \
    -drive if=pflash,format=raw,unit=0,file=OVMF.fd,readonly=on \
    -drive if=pflash,format=raw,unit=1,file=OVMF_VARS.fd \
    -drive file=virtualdrive.qcow2,format=qcow2 \
    -drive file=Win11Pro.iso,media=cdrom,readonly=on \
    -device virtio-vga-gl -display "gtk,gl=on,show-cursor=on" \
    -netdev user,id=net0 -device virtio-net-pci,netdev=net0 \
    -usb -device usb-tablet -device usb-kbd \
    -device ich9-intel-hda -device hda-duplex
    
boot-qemu-with-win11-os-image

4.2.5: Optional: Fixing Internet Connection in Windows 11 VM

If you follow along and install Windows 11 with the optional flags, you may experience internet connection issues inside the VM. This is because Windows needs VirtIO Network Drivers to properly recognize and use the virtual network adapter. You can easily fix this by installing the VirtIO drivers within your virtual machine. These drivers are available from the Fedora Project's website and are usually distributed as an ISO image.


Or Copy and paste the link below. It will take you to fedorapeople.org website page and from there you can get the ISO image for drivers.

https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/archive-virtio/

Make sure you download the latest version of the ISO file (the filename may be something like virtio-win-0.1.266.iso).

Once downloaded, copy the virtio-win-0.1.266.iso file into your VirtualWindows11 Folder or Directory and run QEMU again with this new VirtIO.Win.iso image.

  • We just need to add an extra -drive flag with previous qemu-system command:
-drive file=virtio-win-0.1.266.iso,media=cdrom,readonly=on
  • The full command will now look like this:
qemu-system-x86_64 \
    -cpu host -enable-kvm -smp 2 -m 8G \
    -drive if=pflash,format=raw,unit=0,file=OVMF.fd,readonly=on \
    -drive if=pflash,format=raw,unit=1,file=OVMF_VARS.fd \
    -drive file=virtualdrive.qcow2,format=qcow2 \
    -drive file=virtio-win-0.1.266.iso,media=cdrom,readonly=on \
    -device virtio-vga-gl -display "gtk,gl=on,show-cursor=on" \
    -netdev user,id=net0 -device virtio-net-pci,netdev=net0 \
    -usb -device usb-tablet -device usb-kbd \
    -device ich9-intel-hda -device hda-duplex
    
running-qemu-with-virtio-drivers

Install Virtio-Win-Drivers in QEMU

Follow these steps:

  • Open My Computer.
  • There you should have two Cd-Drives one will be containing Windows11Installer.ISO and the second one containing VirtIO-Win.
  • Open the CD-Drive which contains virtio-win image, it will named something like CD-Drive virtio-win etc.
  • Open virtio-win-gt-x64.exe file.
  • Select Network and press Next to install.
  • It will take short time and you would have network drivers installed for virtio-network.
  • Restart the VM or Windows inside VM so Windows can apply the changes.

And there you have it, everything is configured and running. The ViriIO-Win utiliy offers various drivers for different scenarios when running Windows in a virtual machine. It can even pass throuigh GPU features (which i'll cover in another article). Now you have a near-native running virtual machine in Linux. Feel free to experiment with different QEMU flags or options to see how your machine or the operating system inside reacts.




QEMU Terminal Commands and Options

  • For a full list, check the QEMU man page (man qemu-system-x86_64) command, or visit QEMU Documentation. Options may vary slightly between QEMU versions.

Below are some common, everyday QEMU terminal options:

  • Basic System Emulation:

qemu-system-x86_64 # Emualtes a 64-bit x86(AMD64/Intel64) system.
qemu-system-arm # Emulates a 32-bit ARM system.
qemu-system-aarch64 # Emulates a 64-bit ARM system.
  • System Configuration:

-m "size": Allocate RAM (e.g., -m 4G for 4GB).
-smp "n": Set CPU cores (e.g., -smp 4 for 4 cores).
-cpu "model": Specify CPU model (e.g., -cpu host for passthrough, -cpu qemu64)
-machine "type": Select emulated machine (e.g., -machine q35, -machine virt,accel=kvm).
-accel "type": Enable acceleration (e.g., -accel kvm, -accel tcg).
-name "Windows": Name the VM (for monitoring).
  • Storage Options:

-drive file=disk.qcow2,format=qcow2,if=virtio
file="path/to/file": Disk Image path.
format="type": Image format (e.g., qcow2, raw).
if="interface": Interface type (e.g., virtio, ide, scsi).
media="type": disk or cdrom.
-accel "type": Enable acceleration (e.g., -accel kvm, -accel tcg).
-enable-kvm: Enable KVM acceleration (legacyalternative to -accel kvm).
-name "Windows": Name the VM (for monitoring).
-cdrom "file": Attach a CD/DVD image (e.g., -cdrom ubuntu.iso).
-boot "order": Set boot order (e.g., -boot order=c,menu=on).
  • Display/Graphics:

-display "type": Select display backend (e.g., gtk, sdl, none, cocoa).
-vga "model": Set VGA card (e.g., virio, std, qxl).
-nographic: Disable graphical outpu (use terminal).
-spice "options": Enable SPICE remote desktop (e.g., -spice port=5900).
-device "usb-tablet": Improve moue integration (aviods cursor drift).
  • Networking:

-netdev "type",id="name": Define a network backend.
Types: user (NAT), tap (bridged), socket.
Example: -netdev user,id=net0 -device virtio-net-pci,netdev=net0.
-nic "options": Simplified network setup (e.g., -nic user, model=virtio).
-net user -net nic: Legace networking (NAT with virtual NIC).
  • USB/Peripherals:

-usb: Enable USB bus.
-device usb-host,vendorid=0x1234,productid=0x5678: Passthrough a USB device.
-device usb-mouse: Emulate a USB mouse.
  • Audio:

-audiodev "backend",id="name": Configure audio backend (e.g., pa, alsa).
-soundhw "model": Emulate audio hardware (e.g., hda, ac97).
  • Advanced Options:

-kernal "vmlinuz": Directly boot a linux kernal.
-initrd "file": Specify an initramfs.
-append "cmline": Kernel command-line arguments.
-monitor "type": Expose QEMU monitor (e.g., -monitor stdio).
-snapshot: Run in temporary mode (no disk changes).
-loadvm "snapshot": Restore a saved VM state.

Example Commands

1. Basic Boot:
qemu-system-x86_64 -smp 2 -m 8G -drive file=OS.qcow2,format=qcow2 -enable-kvm

2. Boot from ISO:
qemu-system-x86_64 -m 8G -cdrom steamos.iso -drive file=disk.qcow2,format=qcow2

3. Networked VM:
qemu-system-x86_64 -nic user,model=virtio -drive file=disk.qcow2,format=qcow2



Afterwards

Well, if you've followed the steps above, you now have a full functional QEMU/KVM virtual machine enviornmnet on your Linux system. Unlike the old method of having seprate installations for each operating system, this virtual machine setup allows you to run multiple enviornmnets concurrently. This means you can easily test and use different systems without the need to reboot.At this point, your setup is not only capable of running various operating systems (such as Windows 11) with near-native performance, but it also provides a solid foundation for futher customization and advanced virtualizaion techniques. You can also pass your GPU to QEMU if you don't want to dual boot. For GPU Passthrough please visit my (How to Pass a Single GPU on a Virtual Machine: QEMU/KVM) article.

Next Steps:

  • Experiment with Advanced Options:
    • Test different QEMU flags and configurations to understand how each impacts performance and functionality. This knowledge will help you tailor your virtual machines to specific use cases, whether for development, testing, or production.
  • Monitor and Optimize:
    • Use performance monitoring tools and logging options (e.g., -d exec,cpu and -D logfile) to identify bottlenecks. Fine-tuning resources like CPU allocation, memory, and disk I/O can significantly improve efficiency.
  • Plan for Future Scalability:
    • As your virtualization needs grow, consider integrating additional tools like virt-manager, libvirt and advanced networking setups to build a scalable, production-grade environment.

Don't stop here test with it, make tweaks and push QEMU to its limit.




🎉 Congratulations, your QEMU setup is complete!

If this guide helped you, subscribe for more step-by-step tutorials.
If you face any issue following any step, feel free to comment down below or contact me at my contact page.
1O1 out, I'll see you in the next one!

Load comments