When it comes to virtualization, QEMU is miles ahead of VirtualBox and VMware with its near-native performance and rich feature set. It uses Linux KVM (Kernel-based Virtual Machine) to create virtual environments on a host computer.

Like any other Linux tool, using QEMU to create virtual machines is straightforward. You can create an Ubuntu virtual machine using QEMU in two different ways: from the command line and using virt-manager, a graphical front-end for QEMU.

Download the Ubuntu ISO

To install Ubuntu on a virtual machine, you need the ISO file available on the Ubuntu website.

Download: Ubuntu Desktop

Download the installation image and save it to a preferred location on your computer.

Prerequisite: Check if Virtualisation Is Enabled on Your PC

Before installing QEMU, check whether your machine supports virtualization. You can do so by running:

        egrep -c '(vmx|svm)' /proc/cpuinfo
    
check whether virtualisation is enabled on linux

If the output returns a number greater than 0, you can safely proceed to the next steps. If not, enable virtualization on your machine by booting into the BIOS.

Install QEMU on Your Linux PC

The next step is to install QEMU and other tools required for running an Ubuntu virtual machine. You need to install qemu-kvm, libvirt-daemon, bridge-utils, and virt-manager.

On Ubuntu and Debian, run:

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

On Arch Linux:

        sudo pacman -S qemu-kvm libvirt bridge-utils virt-manager
    

And to install the packages on Fedora or RHEL, run:

        sudo dnf install @virtualization
    

Post-installation, add your current user to the libvirt and kvm groups:

        sudo useradd -aG libvirt $(whoami)
sudo useradd -aG kvm $(whoami)

Start and enable the libvirtd service to run at boot:

        sudo systemctl start libvirtd
sudo systemctl enable libvirtd

Using virt-manager to Create an Ubuntu VM

With virt-manager, you can manage QEMU virtual machines using a GUI. If you prefer the command line instead, skip to the next section.

Launch Virtual Machine Manager from the applications menu. When it opens, click the Create a new virtual machine icon from the secondary menu and select Local install media (ISO image or CDROM) from the available options.

local install with iso in virtual machine manager

Click Forward to proceed. Select Browse to open the ISO media locator. Here, you'll need to add a storage pool from where you'll select the ISO.

To do that, click the Plus icon located in the bottom left and set a name for the pool (input Downloads if the file is in the Downloads directory). Click Browse to locate the folder and hit Open. Then, click Finish.

adding a storage pool for the iso file

Once done, select the newly created pool from the left sidebar and highlight the Ubuntu ISO file. Click Choose Volume to continue.

choosing the ubuntu iso file virtual machine manager

virt-manager will automatically detect the OS you're trying to install. If not, you can type the name in the search field and select the appropriate OS from the results. When you're done, click Forward.

In the memory and CPU settings window, specify the amount of memory and CPU threads you'd like to allocate to the VM. For this guide, let's go with 4096MB of memory and 6 CPU threads. You can allocate more if your hardware supports it. Click Forward to proceed further.

configuring memory and cpu options for ubuntu vm

On the following screen, configure storage for the Ubuntu virtual machine. You can either create a disk image automatically in the current directory or choose a custom location by creating a new storage pool, as earlier.

Proceed with Create a disk image for the virtual machine and specify the size (in GB) you wish to allocate. For an Ubuntu VM, anything more than 20GB works. Make sure the Enable storage for this virtual machine box is checked off.

configuring ubuntu virtual machine storage

Finally, click Finish and virt-manager will set up the virtual machine for you. As soon as it finishes the preliminary setup, a boot menu will appear asking you to Try or Install Ubuntu. Select that from the list of available options.

When Ubuntu boots, you can either try the Ubuntu desktop or install it in the VM, the choice is yours!

ubuntu running using qemu

The steps to install Ubuntu using QEMU are more or less similar to installing Ubuntu on your PC.

By default, the resolution of the virtual machine will be lower than your display resolution. You can change this by heading over to System Settings on Ubuntu and configuring the resolution manually.

When you're done testing the machine, click Virtual machine > Shut down > Shut down to turn it off.

If you wish to start the virtual machine later, simply launch virt-manager again and double-click the Ubuntu VM entry to launch the environment.

Setting Up an Ubuntu VM Through the QEMU CLI

The terminal is the quickest way to configure virtual machines using QEMU. With a few simple commands, you can set up a usable QEMU VM running Ubuntu.

To begin, first, create a new folder for the virtual machine files and move the downloaded Ubuntu ISO into the newly created directory:

        mkdir Ubuntu-VM
sudo mv /path/to/ubuntu.iso ./Ubuntu-VM

Run the following command to create an image file in the current directory:

        qemu-img create -f qcow2 Image.img 20G
    

The above command will create a virtual disk image file that's 20GB in size. The virtual machine will use this disk image to store data, so make sure to specify a size accordingly.

Next, run the following command to start the Ubuntu virtual machine:

        qemu-system-x86_64 -enable-kvm -cdrom ubuntu.iso -boot menu=on -drive file=Image.img -m 4G -cpu host -vga virtio -display sdl,gl=on
    

The virtual machine window will pop up. Press Escape to open the boot menu and select the appropriate option to boot from the ISO file.

ubuntu vm running qemu cli

Select the Try and Install Ubuntu option from the list and Ubuntu will boot. As earlier, you can either test the desktop or install Ubuntu on your computer.

After installing Ubuntu, make sure you remove the -cdrom flag from the qemu command. This will boot Ubuntu from the disk image file rather than the ISO file.

The command will then become:

        qemu-system-x86_64 -enable-kvm -boot menu=on -drive file=Image.img -m 4G -cpu host -vga virtio -display sdl,gl=on
    

Feel free to create a command-line alias for this command if you plan on running it frequently.

QEMU Is Essential to Virtual Machine Users

Virtualization is a great way to experience new desktops and test your software on different OSes. QEMU is the hypervisor of choice for thousands of virtual machine users, given its high performance and less setup time.

There are several other hypervisors for Linux users, namely KVM, QEMU, VirtualBox, and VMware. Which one you should choose, depends on what you expect from the software.