This document describes how to install the Vagrant libvirt provider on Ubuntu 22.04.2 LTS Desktop or Server. Much of the content is based on a blog post by Philippe Vanhaesendonck of Oracle Corp. describing how to set up the Vagrant libvirt provider on Oracle Linux.
All of the commands shown should be run in a terminal window or SSH session.
KVM on Ubuntu requires a CPU that supports hardware virtualization. To verify that your CPU supports hardware virtualization, run the following command:
grep -E -c '(vmx|svm)' /proc/cpuinfo
If this command returns a number greater than or equal to 1, then your CPU supports hardware virtualization.
Run the following command to make sure that your system is up to date:
sudo apt-get -y update && sudo apt-get -y upgrade
When the Vagrant libvirt plugin starts or stops a virtual machine, it requires root priviliges to configure NFS. To avoid having to enter your password every time you start or stop a guest, it's best to enable password-less sudo for your username. To enable password-less sudo, run the following commands:
sudo su -c "echo \"$(id -un) ALL=(ALL) NOPASSWD: ALL\" > /etc/sudoers.d/$(id -un)"
sudo su -c "chmod 0440 /etc/sudoers.d/$(id -un)"
Run the following commands to install the packages needed for virtualization and start the libvirt service:
sudo apt-get -y install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils
sudo systemctl enable --now libvirtd
Optionally, if you're using Ubuntu Desktop, install virt-manager by running sudo apt-get -y install virt-manager
. virt-manager is helpful for troubleshooting and managing virtual machines.
To run virtualization, your username must belong to the libvirt
and kvm
groups. To add your username to these groups, run the following commands:
sudo adduser "$(id -un)" libvirt
sudo adduser "$(id -un)" kvm
Don't worry if you see a message saying that your username is already a member of one or both of these groups.
Important: You need to log out of your session and log back in before the virtualization group privileges are effective. Log out and log back in before proceeding.
Verify that virtualization was installed successfully by running the following command:
virsh list --all
The output should look like the following:
Id Name State
--------------------
If you see an error message instead, something went wrong. Try re-running the commands above. If that doesn't resolve the error, try rebooting your computer. For further troubleshooting, see KVM/Installation in the Ubuntu documentation.
It's best to install Vagrant from the upstream site, rather than using the Ubuntu package, which may be outdated. To install Vagrant and enable automatic updates when new versions are released, run the commands below (from Vagrant's website):
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install vagrant
Verify that Vagrant was installed successfully by running the following command:
vagrant --version
The output should look similar to the following, although the version may be different:
Vagrant 2.3.7
Some of the prerequisites for the Vagrant libvirt plugin are in Ubuntu's "universe" source code repositories. By default, the source code repositories are disabled (commented out) in /etc/apt/sources.list
. To enable the source code repositories, run the following commands:
sudo cp /etc/apt/sources.list /etc/apt/sources.list."$(date +"%F")"
sudo sed -i -e '/^# deb-src.*universe$/s/# //g' /etc/apt/sources.list
sudo apt-get -y update
Some of the prerequisites for the Vagrant libvirt plugin are in Ubuntu's "universe" source code repositories. By default, the source code repositories are not included in /etc/apt/sources.list.d/ubuntu.sources
. To add the source code repositories, run the following commands:
sudo sed -i 's/^Types: deb$/Types: deb deb-src/' /etc/apt/sources.list.d/ubuntu.sources
sudo apt-get -y update
Run the following commands to install and enable the prerequisites needed by the Vagrant libvirt plugin:
sudo apt-get -y install nfs-kernel-server
sudo systemctl enable --now nfs-server
sudo apt-get -y build-dep vagrant ruby-libvirt
# if you get an "Unable to find a source package for vagrant" error running
# the above command, uncomment and run the following command instead:
# sudo apt-get -y build-dep ruby-libvirt
sudo apt-get -y install ebtables dnsmasq-base
sudo apt-get -y install libxslt-dev libxml2-dev libvirt-dev zlib1g-dev ruby-dev
Run the following command to install the Vagrant libvirt plugin:
vagrant plugin install vagrant-libvirt
Your Ubuntu 22.04.2 LTS Desktop or Server machine is now set up to use the Vagrant libvirt provider.
You can find Vagrant boxes that support the libvirt provider in Vagrant's public box catalog.
Oracle provides Vagrant boxes for Oracle Linux 7, 8 and 9 with libvirt provider support at https://yum.oracle.com/boxes/. Oracle also has a GitHub repository with example Vagrant projects that provision Oracle products including Oracle Database.
Enjoy!
Thank you! This worked like a charm. Exactly what I needed to get some testing done before checking in some code.