Skip to content

Instantly share code, notes, and snippets.

@PaulNeumann
Last active June 17, 2024 05:09
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 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

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

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
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!

@WannaBeGeekster
Copy link

Thank you! This worked like a charm. Exactly what I needed to get some testing done before checking in some code.

@PaulNeumann
Copy link
Author

@WannaBeGeekster Great! I'm glad it helped.

@jpplatzer
Copy link

Worked great! Thanks for putting this together!

@PaulNeumann
Copy link
Author

@jpplatzer Excellent! Glad it helped.

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