Skip to content

Instantly share code, notes, and snippets.

@PaulNeumann
Last active November 12, 2024 22:33
Show Gist options
  • Save PaulNeumann/81f299a7980f0b74cec9c5cc0508172b to your computer and use it in GitHub Desktop.
Save PaulNeumann/81f299a7980f0b74cec9c5cc0508172b to your computer and use it in GitHub Desktop.
How to Install the Vagrant libvirt Provider on Ubuntu 22.04.2 LTS Desktop or Server

How to Install the Vagrant libvirt Provider on Ubuntu 22.04.2 LTS (and above) Desktop or Server

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.

Before You Start

Verify That Your CPU Supports Hardware Virtualization

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.

Ensure Ubuntu is Up to Date

Run the following command to make sure that your system is up to date:

sudo apt-get -y update && sudo apt-get -y upgrade

Enable Password-less Sudo

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)"

Install Virtualization

Install KVM Packages and Start libvirt

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.

Add Your Username to Virtualization Groups

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.

Log Out and Log Back In

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 Virtualization Installation

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.

Install Vagrant

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

Install the Vagrant libvirt Plugin

For Ubuntu releases PRIOR TO 24.04: Enable "universe" Source Code Repositories

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

For Ubuntu release 24.04 and above: Add deb-src to ubuntu.sources

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

Install Prerequisites Needed by the Plugin

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

Install the Plugin

Run the following command to install the Vagrant libvirt plugin:

vagrant plugin install vagrant-libvirt

Done

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!

@PaulNeumann
Copy link
Author

@fahmimmaliki Great! Glad it helped.

@AlexRuiz7
Copy link

AlexRuiz7 commented Nov 12, 2024

Worked until the last command, where I stumped upon a problem with the dependencies issue.

Installing the 'vagrant-libvirt' plugin. This can take a few minutes...
Vagrant failed to properly resolve required dependencies. These
errors can commonly be caused by misconfigured plugin installations
or transient network issues. The reported error is:

conflicting dependencies logger (= 1.6.0) and logger (= 1.6.1)
  Activated logger-1.6.1
  which does not match conflicting dependency (= 1.6.0)

  Conflicting dependency chains:
    logger (= 1.6.1), 1.6.1 activated

  versus:
    logger (= 1.6.0)

  Gems matching logger (= 1.6.0):
    logger-1.6.0

Great guide nevertheless. Thanks!

Update: fixed by upgrading Vagrant to 2.4.3.

Installing the 'vagrant-libvirt' plugin. This can take a few minutes...
Fetching xml-simple-1.1.9.gem
Fetching racc-1.8.1.gem
Building native extensions. This could take a while...
Fetching nokogiri-1.16.7-x86_64-linux.gem
Fetching ruby-libvirt-0.8.4.gem
Building native extensions. This could take a while...
Fetching formatador-1.1.0.gem
Fetching fog-core-2.6.0.gem
Fetching fog-xml-0.1.4.gem
Fetching fog-json-1.2.0.gem
Fetching fog-libvirt-0.13.0.gem
Fetching diffy-3.4.3.gem
Fetching vagrant-libvirt-0.12.2.gem
Installed the plugin 'vagrant-libvirt (0.12.2)'!

@PaulNeumann
Copy link
Author

@AlexRuiz7 I'm glad it helped! The problem you saw is a Vagrant bug that prevents installing any plugin with Vagrant 2.4.2 (see hashicorp/vagrant#13527). Vagrant 2.4.3 was released a little earlier today, and it should resolve the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment