How to use Vagrant with Libvirt KVM on CentOS 8

You can download this article in PDF format via the link below to support us.

Download the guide in PDF format

turn off


Kernel-based virtual machine (KVM) is an open source virtualization technology that has been adopted by large-scale virtualization and cloud projects (such as OpenStack, CloudStack, oVirt, etc.). It is built into Linux and can help you turn Linux Server into a hypervisor that runs multiple virtual machines (VM). The purpose of creating this guide is to help new users use Vagrant on CentOS 8 and KVM hypervisors to accelerate and manage the life cycle of virtual machines.

If you are using vagrant for the first time, it is caused by Hash company Enable developers to build and maintain a portable software development environment in any virtualization platform (KVM, Virtualbox, VMware, Parallels and even Docker containers). The main requirement of this guide is a running CentOS 8 server or workstation. If you have a Fedora workstation, this guide will also serve you.

We have completed another article about using Libivirt and KVM on Linux distributions earlier, and this article should also be applicable to CentOS8. The only difference I noticed is the Vagrant plugin installed for KVM. When installing on CentOS 8, this guide will involve some additional dependencies.

Step 1: Install KVM on CentOS 8

Before installing and using the Vagrant plugin, we need the KVM virtualization software stack. Before starting the installation, make sure that all system software packages (including the kernel) have been updated to the latest version.

sudo dnf -y update

After the update is complete, restart the system.

sudo systemctl reboot

Wait for the system to restart, and then install the KVM virtualization tool on the CentOS 8 Linux computer.

sudo dnf install -y @virt virt-install libvirt-devel vim bash-completion

Start and enable the libvirtd service.

sudo systemctl enable --now libvirtd

Add your user to the libvirt group

sudo usermod -aG libvirt $USER
newgrp libvirt

Step 2: Install Vagrant on CentOS 8

The next software component is Vagrant. Some of the dependencies we will use are build tools, and you can easily install them using the following dnf command.

sudo dnf groupinstall "Development Tools" -y 
sudo dnf -y install rsync gcc zlib-devel libvirt-devel

Install the Ruby and Ruby Development packages required to use Vagrant.

sudo dnf install -y ruby ruby-devel

Download and install the latest version of Vagrant.

sudo dnf localinstall vagrant_${VERSION}_x86_64.rpm

hit ÿ The key to start the installation:

Dependencies resolved.
 Package         Architecture   Version              Repository            Size
 vagrant         x86_64         1:2.2.9-1            @commandline          42 M

Transaction Summary
Install  1 Package

Total size: 42 M
Installed size: 122 M
Is this ok [y/N]: y

Step 3: Install the Vagrant plugin for Libvirt

After installing KVM, the required Vagrant and tools, you can continue to install KVM’s vagrant plugin. This will enable you to rotate the virtual machine from the Libvirt provider-specific image in CentOS 8 Desktop or Server.

$ CONFIGURE_ARGS="with-libvirt-include=/usr/include/libvirt with-libvirt-lib=/usr/lib64" vagrant plugin install vagrant-libvirt

Installing the 'vagrant-libvirt' plugin. This can take a few minutes...
Building native extensions. This could take a while...
Building native extensions. This could take a while...
Fetching fog-libvirt-0.7.0.gem
Fetching vagrant-libvirt-0.1.2.gem
Installed the plugin 'vagrant-libvirt (0.1.2)'!

If the installation fails due to this dependency, use the gem command to install the nokogiri library.

$ gem install nokogiri
Fetching: nokogiri-1.10.10.gem (100%)
Building native extensions. This could take a while...
Successfully installed nokogiri-1.10.10
Parsing documentation for nokogiri-1.10.10
Installing ri documentation for nokogiri-1.10.10
Done installing documentation for nokogiri after 2 seconds
1 gem installed

Ensure that the installation is successful. For any errors encountered, you can share them in the comments section, and we will help you.

Step 4: Add the Libvirt Wanderer box

box It is the package format of the Vagrant environment. Anyone on any platform supported by Vagrant can use the box to build the same working environment.

To use the provider we just installed, we need the Vagrant box built for the Libvirt provider. You can browse all available boxes below Wandering clouds Find your match.

In my use case, I need several boxes, which can be pulled out using the created vagrant box utility to provide all the functions of managing boxes.

--- Add CentOS 8 box ---
$ vagrant box add centos/8 --provider=libvirt

--- Add CentOS 7 box ---
$ vagrant box add centos/7 --provider=libvirt

--- Add Ubuntu 20.04 box ---
$ vagrant box add generic/ubuntu2004 --provider=libvirt

--- Add Ubuntu 18.04 box ---
$ vagrant box add generic/ubuntu1804 --provider=libvirt

--- Add Fedora 32 box ---
$ vagrant box add generic/fedora32 --provider=libvirt

You can use the following command to get a list of all available Vagrant boxes:

$ vagrant box list
centos/7           (libvirt, 2004.01)
centos/8           (libvirt, 1905.1)
generic/ubuntu2004 (libvirt, 3.0.20)

Run virtual machines with Vagrant and Libvirt on CentOS 8

Vagrantfile describes the types of computers required for the project, and how to configure and configure these computers.

We will create a simple Vagrant file that you can use to run:

$ vim Vagrantfile

To start the virtual machine running:

$ vagrant up

Expected output:

Bringing machine 'default' up with 'libvirt' provider...
==> default: Registering VM image from the base box 'centos/8'...
==> default: Creating new virtual machine as a linked clone of the box image...
==> default: Unregistering the box VM image...
==> default: Setting the default configuration for VM...
==> default: Setting the name of the VM: cent8
==> default: Preparing network interfaces based on configuration...
    default: Adapter 0: shared
==> default: Clearing any previously set network interfaces...
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address:
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Mounting shared folders...
    default: /vagrant => /Users/jmutai/vagrant/cent8

To start the SSH Shell, run:

$ vagrant ssh

This system is built by the Bento project by Chef Software
More information can be found at
Last login: Fri Aug  7 00:29:28 2020

[[email protected] ~]$ cat /etc/redhat-release
CentOS Linux release 8.2.2004 (Core)
[[email protected] ~]$ exit

To stop the server after it is running, use the vagrant halt command.

$ vagrant halt
==> default: Attempting graceful shutdown of VM...

To destroy the virtual machine, run:

$ vagrant destroy
    default: Are you sure you want to destroy the 'default' VM? [y/N] y
==> default: Destroying VM and associated drives...
==> default: Destroying unused networking interface...

All available vagrant command options include:

--version      -- Prints the Vagrant version information
box            -- Box commands
connect        -- Connects to a shared, remote Vagrant environment
destroy        -- Destroys the vagrant environment
docker-logs    -- Shows Docker logs
docker-run     -- Run one-off commands against a Docker container
global-status  -- Reports the status of all active Vagrant environments on the system
halt           -- Halts the currently running vagrant environment
help           -- [TASK] Describe available tasks or one specific task
init           -- [box_name] [box_url] Initializes current folder for Vagrant usage
list-commands  -- Outputs all available Vagrant subcommands
login          -- Authenticates against a Vagrant Cloud server to access protected boxes
package        -- Packages a vagrant environment for distribution
plugin         -- Manage plugins
provision      -- Run the provisioner
push           -- Deploys code in this environment to a configured destination
rdp            -- Connects to machine via RDP
reload         -- Reload the vagrant environment
resume         -- Resumes a suspend vagrant environment
rsync          -- Syncs rsync synced folders to remote machine
rsync-auto     -- Syncs rsync synced folders automatically when files change
share          -- Shares the Vagrant environment and allows remote access
ssh            -- SSH into the currently running environment
ssh-config     -- Outputs .ssh/config valid syntax for connecting to this environment via ssh
status         -- Shows the status of the current Vagrant environment
suspend        -- Suspends the currently running vagrant environment
up             -- Creates the vagrant environment
version        -- Prints the currently installed Vagrant version and checks for new updates

You can learn more by reading the official documents Wanderer Document.

You can download this article in PDF format via the link below to support us.

Download the guide in PDF format

turn off