QEMU is a powerful open-source machine emulator and virtualizer that allows users to run multiple operating systems on a single host machine. It is an excellent tool for developers, system administrators, and hobbyists who want to experiment with different operating systems or test software on various platforms.
If you’re an Ubuntu user, installing and using QEMU is easy and straightforward. In this article, we’ll guide you through the steps to install and use QEMU on Ubuntu, so you can start running multiple virtual machines on your system in no time.
Check for Hardware Virtualization Support
The first thing to determine is whether your machine supports hardware virtualization support. Without it, QEMU wouldn’t even work. So here are the steps to check for hardware virtualization support before installing QEMU on Ubuntu.
- To check for virtualization support, run the below command in the terminal:
egrep -c '(vmx|svm)' /proc/cpuinfo
If the result is higher than 1, the CPU has hardware virtualization capacity.
- Next, confirm if virtualization is enabled in the BIOS:
lscpu | grep Virtualization:
As you can see below, this computer has Intel VT-x enabled. If it were an AMD CPU, you’d see AMD-v.
- Lastly, use the cpu-checker tool to confirm if KVM acceleration is present. But the tool is not installed by default, so run the following commands to install and run it:
sudo apt update sudo apt install -y cpu-checker sudo kvm-ok
Install Qemu on Ubuntu
Now that we’ve covered the prerequisites, let’s install Qemu next. Qemu, its dependencies, and complementing packages are available in the default Ubuntu repositories.
Run the below command in the terminal.
sudo apt install -y qemu qemu-kvm virt-manager bridge-utils
The command installs the following packages:
- qemu: QEMU is an open-source emulator and virtualization software that can run virtual machines of different architectures and operating systems. It can emulate hardware and provide a virtual environment for running guest operating systems.
- qemu-kvm: QEMU-KVM is a set of kernel modules that provide support for running virtual machines on Linux. It’s based on QEMU and adds features to improve performance and compatibility with modern hardware.
- virt-manager: Virt-manager is a graphical user interface tool for managing virtual machines on Linux. It provides an easy-to-use interface for creating, configuring, and managing virtual machines.
- bridge-utils: Bridge-utils is a set of command-line tools for creating and managing network bridges on Linux. A network bridge allows virtual machines to share a physical network interface with the host system, enabling them to communicate with other systems on the network.
Once installed, make sure that the Virtualization daemon is enabled and running.
systemctl status libvirtd
The installation created two groups: libvirt and kvm. Only members of these groups are authorized to run virtual machines. So let’s add our user account as a member of these groups.
sudo adduser $USER libvirt sudo adduser $USER kvm
Create a VM in Qemu
So now we’ve installed Qemu; it’s time we put it to use. In this example, we’ll create a new Linux VM
- Download the ISO of the OS you want to install. In this example, I’ve downloaded the LUbuntu 22.04 ISO.
- Launch the Virtual Machine Manager. You can open it by running virt-manager in the terminal or clicking the Virtual Machine Manager shortcut.
- Once open, click the Create a new virtual machine button.
- Select the location of the installation source. In this example, I’ll choose Local install media and click Forward.
- Click Browse.
- Click Browse Local.
- Locate the ISO file and open it.
- Uncheck the Automatically detect box and find the closest Linux distribution to the one you’re installing. Since we are installing LUbuntu in this example, we’ll select the Ubuntu profile and click Forward.
- Set the CPU and Memory allocation and click Forward.
- Set the storage size for the new VM and click Forward.
- Set the name of the new VM and click Finish.
- The VM boots, and you can continue to install the operating system.
Enable Auto Resize
Using a virtual machine that doesn’t fit in your display is cumbersome. Imagine panning and scrolling up, down, left, and right to view the VM display. It’s good that you can enable auto-resize in Qemu.
Click View → Scale Display. Select Always and check the Auto resize VM with window box.
Enable Clipboard Sharing
Sharing contents of the clipboard is a staple of GUI-based VM solutions. This feature is not built-in to Qemu; rather, it can be enabled by installing the spice-vdagent package in the Qemu guest OS.
The spice-vdagent package provides a daemon that runs inside the virtual machine and communicates with the SPICE client to optimize the virtual desktop experience. It provides features such as clipboard sharing, automatic resolution adjustment, and mouse pointer synchronization between the host and guest systems.
Run this command to install the package.
sudo apt install spice-vdagent
As shown in this example, some distros may already have the spice-vdagent installed out of the box.
You can now copy and paste content between the host and VM.
Shared folders are an excellent way to access a folder on the host from within the VM. It makes for a convenient sharing of files between the host and guest OS.
Follow these steps to add a shared folder.
Add the VirtIO Filesystem (virtiofs)
- Open the virtual machine details.
- Click Memory.
- Check the Enable shared memory box.
- Click Apply.
- You’ll be informed that the changes will take effect after the next guest shutdown. Click OK.
- Shut down the virtual machine.
- Re-open the virtual machine details and click Add Hardware.
- Click FIlesystem.
- Enter the Source path (the path of the folder on the host to share). In my case, I’m sharing the share folder from my home folder (/home/june/share).
- Enter the Target path (the share name inside the VM). In this example, we’ll call it share.
- Click Finish.
Let’s mount the shared folder inside the VM now.
- Start the virtual machine.
- Open the terminal and run the below command:
sudo mount -t virtiofs sharename path/to/shared/directory
In my case, the share name is share, and I will mount it to the /home/junec/share directory.
sudo mount -t virtiofs share /home/junec/share
- Open the File Manager and confirm the shared folder is mounted.
- Add this entry to the /etc/fstab file to make this mount permanent. Replace share with your share name and /home/junec/share with your shared folder mount point.
share /home/junec/share virtiofs defaults 0 0
Once you save the file, the shared folder will be mounted automatically when the virtual machine is restarted.
In conclusion, QEMU is a powerful and flexible virtualization software that can help run multiple operating systems on your Ubuntu system. By following the step-by-step guide in this article, you should now have a basic understanding of how to install and use QEMU on Ubuntu.
With QEMU, you can create virtual machines with different architectures, emulate different hardware components, and even run software designed for different operating systems. This makes QEMU a valuable tool for developers, testers, and anyone who needs to run multiple operating systems on a single machine.