Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Raspberry Pi Virtual Machine: Setup Guide

· 7 min read · Home Tech

In this article
  1. Understanding Virtualization on ARM
  2. What You Need Before You Start
  3. Step-by-Step: Installing KVM and QEMU
  4. Choosing a Guest OS
  5. Performance Expectations and Limitations
  6. Common Problems and How to Fix Them
  7. Alternatives to Full Virtualization
  8. Frequently Asked Questions

Yes, you can run a virtual machine (VM) on a Raspberry Pi, and the most practical way is to use KVM (Kernel-based Virtual Machine) with QEMU on a 64-bit operating system. This lets you run other operating systems, like a different Linux distribution or even Windows on ARM, in an isolated environment on your Pi. The performance is surprisingly usable for lightweight tasks, though it depends heavily on your Pi model and the guest OS you choose.

Understanding Virtualization on ARM

Virtualization on a Raspberry Pi is different from on a typical x86 PC. Most desktop virtualization software like VirtualBox or VMware relies on hardware virtualization extensions that are common on Intel and AMD processors. The Raspberry Pi uses an ARM processor. Modern Pi models (Pi 3, Pi 4, Pi 5, and the Pi 400) have ARMv8-A CPUs that include hardware virtualization extensions (Virtualization Extensions, or VE). However, these are not the same as Intel VT-x/AMD-V, and not all software is written to use them.

KVM is a Linux kernel module that can use these ARM virtualization extensions. When combined with QEMU (a user-space emulator), you can create and run VMs with near-native performance for CPU-bound tasks, as long as the guest OS supports ARM architecture. This is the same technology used in cloud ARM servers, so it is a robust and well-tested path.

What You Need Before You Start

Before you begin, gather the following:

  • A 64-bit operating system: The Raspberry Pi OS (formerly Raspbian) has a 64-bit version (since 2022, the default is 64-bit). You can also use Ubuntu Server or other 64-bit Linux distributions. KVM will not work on a 32-bit OS.
  • A Raspberry Pi 3, 4, or 5: The Pi 3 has only 1GB of RAM, which is very limiting. A Pi 4 with 4GB or 8GB is the minimum recommended. The Pi 5 is the best performer.
  • Storage: VMs need disk space. A fast microSD card is okay for basic testing, but an SSD connected via USB 3.0 (on Pi 4/5) will give much better performance.
  • Patience: Installing and configuring KVM is not a one-click process. Expect to spend some time in the terminal.

Step-by-Step: Installing KVM and QEMU

Here is the general process for setting up a VM on a Raspberry Pi running a 64-bit OS. The commands are for Debian-based systems (like Raspberry Pi OS or Ubuntu).

  1. Update your system: Run sudo apt update and sudo apt upgrade to ensure all packages are current.
  2. Install the virtualization packages: Run sudo apt install qemu-system-arm qemu-utils libvirt-daemon-system libvirt-clients virtinst. This installs QEMU, the libvirt daemon (which manages VMs), and the tools to create them.
  3. Add your user to the libvirt group: Run sudo adduser $(whoami) libvirt and sudo adduser $(whoami) kvm. This allows your user to manage VMs without sudo for every command. Log out and back in for the group changes to take effect.
  4. Enable the virtualization module: Check if KVM is available by running kvm-ok (install with sudo apt install cpu-checker if needed). It should report that KVM acceleration can be used. If not, you may need to add arm_64bit=1 to your /boot/config.txt and reboot.
  5. Download a guest OS image: For example, download a Debian ARM64 cloud image or a Ubuntu ARM64 cloud image. These are designed for VMs and are smaller than desktop ISOs.
  6. Create a virtual disk: Use qemu-img create -f qcow2 myvm.qcow2 10G to create a 10GB disk image.
  7. Install the guest OS: Use virt-install with the downloaded image and the disk you created. You will need to specify the architecture (aarch64) and the amount of RAM and CPUs to allocate.

After the installation completes, you can boot the VM with virsh start myvm and connect to it via VNC or SSH.

Choosing a Guest OS

Not every operating system runs well on a Pi VM. Here are realistic options:

  • Linux (Debian, Ubuntu, Arch): The best support. ARM64 versions of these distributions work well with KVM. You can run a headless server or a lightweight desktop environment like XFCE or LXDE.
  • Windows on ARM: Microsoft sells Windows 11 for ARM, but it is not officially supported on Raspberry Pi. Some enthusiasts have managed to run it, but it requires a lot of tweaking, and performance is poor. It is not a beginner-friendly option.
  • Android: There are Android images for ARM, but they are not officially supported and often lack GPU acceleration. It is a fun experiment but not a daily driver.
  • BSD: FreeBSD and NetBSD have ARM64 support and can run in a VM, but they are less common for Pi users.

Performance Expectations and Limitations

Running a VM on a Pi is not like running one on a desktop PC. Here is what you should expect:

  • CPU: For CPU-bound tasks (like compiling code), the VM will run at near-native speed because KVM uses hardware virtualization. The Pi 4's quad-core Cortex-A72 is decent, but the Pi 5's Cortex-A76 is significantly faster.
  • RAM: The Pi 4 and 5 have up to 8GB, but the VM will share that with the host OS. If you allocate 4GB to the VM, the host only has 4GB left. Memory pressure can cause slowdowns.
  • GPU: There is no GPU passthrough. The guest OS will use a basic framebuffer (like VNC), so any graphical interface will be slow. 3D acceleration is not possible. For a desktop experience, use a lightweight desktop and a VNC connection.
  • I/O: The storage speed is the biggest bottleneck. A microSD card is slow, and running multiple VMs on it will cause lag. An SSD is strongly recommended.

In short, a Pi VM is great for testing software, running isolated services, or learning about virtualization. It is not a replacement for a desktop machine.

Common Problems and How to Fix Them

Here are the most frequent issues people run into:

  • KVM not available: If kvm-ok says KVM is not available, your kernel may not have the module loaded. Run sudo modprobe kvm and sudo modprobe kvm-arm (or similar). If that fails, ensure you are running a 64-bit kernel (uname -m should output aarch64).
  • VM is extremely slow: This usually means KVM is not being used. QEMU falls back to full emulation, which is much slower. Check with virsh vcpuinfo myvm to see if the vCPU is pinned to a physical CPU. Also, make sure you are not allocating more CPUs than the Pi has.
  • Network not working: By default, libvirt creates a NAT network. If the guest cannot get an IP, check the default network is active (virsh net-start default) and that the bridge is up.
  • Cannot connect to VNC: The VNC server may be bound to localhost. Use virsh vncdisplay myvm to get the port, and then use an SSH tunnel to connect securely.

Alternatives to Full Virtualization

If KVM seems too complex or performance is not enough, consider these alternatives:

  • Docker: For running individual applications (like a web server or database), Docker is much lighter than a VM. It uses the host kernel, so it is not a full OS, but it provides isolation. This is the best choice for most server use cases.
  • LXD: LXD is a container manager that can run full Linux systems (like Ubuntu) in containers. It is closer to a VM than Docker, but still uses the host kernel. It is faster than a VM and uses less RAM.
  • Dual-boot: If you need a different OS for a specific task, you could create a second SD card or partition and boot into it. This is not virtualization, but it is simple and gives full performance.
  • Chroot: For very light isolation, a chroot can run a different Linux distribution without rebooting. It is not secure for untrusted code, but it is useful for testing.

Frequently Asked Questions

Can I run Windows on a Raspberry Pi VM?

Technically, it is possible to run Windows 11 for ARM, but it is not officially supported. You would need to use a special image and deal with many compatibility issues. Performance is poor, and you cannot use GPU acceleration. It is not recommended for daily use.

How much RAM should I allocate to a VM?

It depends on your Pi's total RAM. For a Pi 4 with 4GB, allocate 2GB to the VM. For an 8GB Pi, you can allocate 4GB. Always leave at least 1GB for the host OS to avoid swapping.

Is a virtual machine on a Pi safe?

Yes, it is safe. The VM is isolated from the host OS, so if the guest is compromised, the host is generally protected. However, do not run untrusted code in a VM with access to sensitive host resources (like mounted folders) unless you understand the risks.

Can I use the Pi's GPIO pins in a VM?

No, you cannot directly access GPIO pins from a VM. The guest OS would need a driver that supports the virtualized hardware, which does not exist. If you need GPIO, run the code on the host OS.

What is the best Pi model for running VMs?

The Raspberry Pi 5 with 8GB RAM is the best choice. It has a faster CPU and more memory bandwidth than the Pi 4. The Pi 4 with 8GB is also workable, but the Pi 3 is too slow for most VMs.