Install & Setup QEMU/KVM to Run Virtual Machines in Ubuntu 24.04

Last updated: December 5, 2024 — Leave a comment

This is a step by step guide shows how to install and manage virtual machines using qemu/kvm solution in Ubuntu 24.04.

KVM, Kernel-based Virtual Machine, is a module in Linux Kernel that can function as a hypervisor. It offers a high performance virtualization environment with hardware virtualization extensions, such as Intel VT or AMD-V that’s built in CPU.

KVM does not directly function in user-space. It requires a user-space application, such as QEMU, to interact with it and manage virtual machines.

As QEMU is a command line tool, there’s also graphical tool, such as Virtual Machine Manager, to make it easy to use.

1. Load KVM Kernel module and install QEMU:

The KVM module requires host machine’s processor has virtualization support. It’s VT-x for Intel processors and AMD-V for AMD processors. And, all the Intel and AMD CPUs in past 10 years support virtualization.

To check if your CPU support virtualization, open terminal (Ctrl+Alt+T) and run command:

LC_ALL=C.UTF-8 lscpu | grep Virtualization

If it outputs nothing, then make sure you have enabled the virtualization support in your BIOS.

And, check if KVM module loaded, run command:

lsmod | grep kvm

The kernel module is usually loaded by default in Ubuntu. If NOT, try sudo modprobe -a kvm kvm_intel to load it (replace kvm_intel with kvm_amd for AMD processor).

To install QEMU in Debian, Ubuntu, Linux Mint and their based systems, run command:

sudo apt install qemu-system

2. Create Virtual Machine from command line

NOTE: If you hate Linux commands, just skip this step and use the graphical tool in next step!

1. First create a virtual disk image file by running similar command below:

qemu-img create -f raw /media/ji/600g/windows10.raw 80G

Here you need to replace the command options accordingly. They are:

  • -f raw tells to create raw image. The creating process is very slow, and it will take the full space you assigned (80G in my case) on host. Not-used space on the guest cannot be used on the host. For choice, you may use -f qcow2 instead, which only allocates space to the image file when the guest operating system actually writes to the virtual hard disk. However, use qcow2 will likely affect performance.
  • /media/ji/600g/windows10.raw tells to create windows10.raw image file as virtual disk, and it’s saved to the external disk mounted on /media/ji/600g. You can skip the path, so the image file will be created in current directory.
  • 80G, the full capacity of the guest hard drive on the host.

And, at any time, you can move or delete the file (windows10.raw in my case) as you want. Though, you need to change path to file in next commands.

2. To install virtual operating system, use command looks like:

qemu-system-x86_64 -cdrom /path/to/iso-image -boot menu=on -drive file=/path/to/virtual-disk-image,format=raw -m 8G -cpu host -smp sockets=1,cores=2,threads=2 --enable-kvm

This command tells to use:

  • qemu-system-x86_64 to simulate amd64 guest CPU architecture. Replace x86_64 with x86, s390x, ppc, etc depends on guest OS type.
  • -cdrom /path/to/iso-image to attach .iso image via CD Rom.
  • -boot menu=on to show the menu to select which device to boot.
  • file=/path/to/virtual-disk-image,format=raw to attach virtual disk via image file, replace raw according what you created in last command.
  • -m 8G – assign RAM memory.
  • -cpu host to use CPU same to host. For choice, you may run qemu-system-x86_64 -cpu help to get more choices.
  • -smp sockets=1,cores=2,threads=2 – set 1 CPU with 2 cores and each 2 threads, meaning (2cores/4threads).
  • --enable-kvm to enable KVM. For choice, you may use -accel kvm instead.

In my case, the command is:

qemu-system-x86_64 -cdrom ~/Documents/windows_10_enterprise_ltsc_2019_x64_dvd_9c09ff24.iso -boot menu=on -drive file=/media/ji/600g/windows10.raw,format=raw -m 8G -cpu host -smp sockets=1,cores=2,threads=2 --enable-kvm

The command above will launch the virtual machine window, allowing to boot from .iso image, then install the OS to the virtual disk image.

Afterward, run the guest OS via command:

qemu-system-x86_64 /path/to/virtual-disk-image

As well, you may add similar options above to specify memory size, how may CPU cores to use, enable KVM, etc. See the official documentation for more.

3. Use graphical tool to install & manage QEMU/KVM VMs

The QEMU command line tool is NOT convenient and hard for beginners. To make life easier, there are a few graphical tools to install & manage the VMs. And, here I’m going to show you “Virtual Machine Manager”.

Install Virtual Machine Manager:

First, open terminal (Ctrl+Alt+T) and run command to install the graphical tool:

sudo apt install virt-manager

Then, add your user into libvirt group and restart the daemon service:

sudo gpasswd --add $USER libvirt
sudo systemctl start libvirtd

Launch the tool and create new VMs

Next search for and launch Virtual Machine Manager from start menu (or GNOME Overview). It should automatically connect to QEMU/KVM system on startup.

If NOT, just double-click on it in the list. And, try restarting computer if connection failed!

Then, click the first toolbar icon, or go to menu “File -> New Virtual Machine” to start the create VM dialog.

In the pop-up wizard, you can choose install from local .iso image, network install, existing virtual disk image, as well choose the guest OS architecture type.

Then follow the pop-up wizard to choose .iso image or CD Rom install media. If you clicked “Browse…” button instead of manually type path to iso image, then, you may manually add pool for non-default location, e.g., external disk, and create virtual disk there.

Finally, click “Browse Local” and choose the .iso image. NOTE: it will ask and change the file permission of selected iso image and virtual disk image. If, you don’t like this behavior, see next step. 

Next, set memory size and how many CPU cores to use, create virtual disk or choose existing disk image you just created, and so forth until the wizard done.

If everything goes well, the virtual machine should start and load the iso image for installing the operating system.

Configure KVM to use as normal user

Creating virtual disk image in non-default location (e.g., external disk) and attaching .iso image will change the ownership of them, so they will NOT belong to you (your user). And, it may even fail sometimes due to permission issue.

To workaround it, you may configure to use as a normal user.

First, run the command in terminal (Ctrl+Alt+T) to edit the libvirtd config file:

sudo nano /etc/libvirt/libvirtd.conf

Then, find out and set unix_sock_group = 'libvirt' and unix_sock_rw_perms = '0770' by removing # at the beginnings and change value. Finally, press Ctrl+S to save, Ctrl+X to exit.

Next, edit qemu config file by running command:

/etc/libvirt/qemu.conf

Then find out and set user = "your_user" and group = "your_user" and save file (Ctrl+S, then Ctrl+X).

Finally, restart the daemon via command:

sudo systemctl restart libvirtd.service

Then, close and re-launch Virtual Machine Manager.

Create share folder between host and guest OSes

To share files between host and guest operating systems, you may create a share folder via the step below.

1. First, open terminal and install spice-vdagent and virtiofsd packages in both host and guest OSes:

  • For Debian, Ubuntu, Linux Mint, etc, use command:
    sudo apt install spice-vdagent virtiofsd
  • For Fedora and based systems, run:
    sudo dnf install spice-vdagent virtiofsd
  • And, Arch based systems may run the command instead:
    sudo pacman -S spice-vdagent virtiofsd
  • For Windows, just click download & install the spice-guest-tools exe file.

2. Next go to menu “View -> Details” in virtual machine window. Then do:

  • Navigate to “Memory” in left, and enable “Enable shared memory”.
  • Click on “Add Hardware” button, then choose “Filesystem” in pop-up dialog.
  • Next set:
    • Driver: virtiofs.
    • Source path: path to the folder you want to share in the host OS.
    • Target path: whatever share-name

3. Finally, start or restart the virtual machine.

For Linux guest OS, run command to mount the share folder that works until reboot:

sudo mount -t virtiofs share /home/USERNAME/Documents/share

Here replace share with the share-name you set in last step, as well as the mount point in guest OS to yours. When done, open that mount point folder and see if it works!

To make it permanent, use sudo nano /etc/fstab command to edit /etc/fstab and add new line below in the end:

share-name-here   /path/to/mount/in/guest  virtiofs   defaults  0   0

Finally, press Ctrl+S to save and Ctrl+X to exit.

For Windows, do following steps one by one on the guest OS:

  1. First download & install WinFSP from here.
  2. Then click download virtio-win.iso from Fedora website.
  3. Open file explorer, navigate to the Downloads folder and click mount the virtio-win.iso image you just downloaded.
  4. Right-click on start menu icon and open “Device Manager”, navigate to “Other Devices -> Mass Storage Controller”, and right-click on it and select “Update driver”.
  5. Next click “Browse my computer for driver software”, browse and choose the DVD Drive (you just mounted for virtio-win.iso image) as the location for drivers, finally click ‘Next’ to automatically search and install the driver from that directory.
  6. Then open the DVD Drive in file explorer, and navigate to viofs folder. Next, copy either w10 or w11 sub-folder, depends guest OS version, to local directory, e.g., C:\virtiofs (create virtiofs folder in C manually).
  7. Finally, open cmd (Command Prompt) as admin and run commands to create and start service to auto-mount the share folder:
    sc.exe create VirtioFsSvc binpath="path\to\virtiofs.exe" DisplayName="Virtio FS Service"
    sc.exe start VirtioFsSvc

    Here you need to replace path\to\virtiofs.exe. In my case, I copied the “w10” folder from DVD Drive to C:\virtiofs, so the path is C:\virtiofs\w10\amd64\virtiofs.exe.

If everything goes well, you should now see the share folder in the left pane of file explorer.

In Addition

There are also many other things you may want to do for VMs in the manager, such as enable 3D acceleration by switching “Video QXL” video module to “Virtio” then checking the 3D acceleration option, then enable OpenGL in Display Spice.

As well, there’s a good tutorial shows how to enable GPU passthrough, so your virtual machine can access GPU directly for native performance for gaming and graphics editing.

Due to limit of tutorial length, I will not add more here. Please feel free to leave comment below.

Twitter

I'm a freelance blogger who started using Ubuntu in 2007 and wishes to share my experiences and some useful tips with Ubuntu beginners and lovers. Please comment to let me know if the tutorial is outdated! And, notify me if you find any typo/grammar/language mistakes. English is not my native language. Contact me via [email protected] Buy me a coffee: https://ko-fi.com/ubuntuhandbook1 |

No Comments

Be the first to start the conversation.

Leave a Reply

Text formatting is available via select HTML.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

*