How to Install QEMU and Near-Native VMs on Linux: Ultimate Guide
Installation Linux QEMU WalkthroughHi all, welcome to my blog, MusaBase! In this comprehensive guide, I'll walk you through installing QEMU and creating Kernel-based Virtual Machines (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, but you can follow this guide on any linux distribution with only minor adjustments to package names or your package manager.
Jump To:
- What is QEMU?
- Prerequisites
- Step 1. Installing QEMU and Core Virtualization Packages.
- Step 2, Enabling and Configuring Core Virtualization Services.
- Step 3. Launching a Virtual Machine using QEMU with Virt-Manager.
- Step 4. Booting QEMU with an OS Image
- QEMU Terminal Commands and Options.
- Afterwards.
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 try to run.
- Operating System: Any recent Linux distribution 32-bit or 64-bit.
If you need detailed information about the architectures and technologies required for creating a virtual machine with Hardware Acceleration
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
Now it's time to install 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
For SysVinit:
- Starting the libvirtd service:
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 the command:
Arch-based distros & Red Hat-Based distros:
sudo usermod -aG libvirt $USER
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
- For AMD CPUs, type:
sudo modprobe kvm_amd
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. Launching a Virtual Machine using QEMU with Virt-Manager
Launch Virt-Manager:
- Finally, test your setup by launching virt-manager with:
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.
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. Booting QEMU with an OS Image
Now, to 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.
We can use Virt manager by 2 different approach to create Virtual machines.
- GUI Approach
- CLI Approach
QEMU Terminal Commands and Options
Here 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
- For a full list, check the QEMU man page (man qemu-system-x86_64) or QEMU Documentation. Options may vary slightly between QEMU versions.
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.
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!