How to install Vagrant with KVM Provider

Share on Social Media

In this article, you will learn how to install Vagrant with KVM Provider on CentOS/Rocky Linux 8 server. We will also show you how to create a virtual machine by using Vagrant boxes and KVM provider. #centlinux #vagrant #libvirt

What is Vagrant? :

Vagrant is a free and open source software for easy provisioning and orchestration of virtual software development environments. This virtual environment is called Vagrant Box and it is portable, so it can be used over many platforms without any change. Vagrant is developed by HashiCorp and distributed under MIT License.

Vagrant uses a declarative configuration file called Vagrantfile, that describes all the software requirements, packages, operating system configurations, users and more.

Vagrant was originally tied with Oracle VirtualBox. But later Vagrant added the support for other virtualization platforms such as VMWare and KVM.

In technical terminology, these virtualization platforms (VirtualBox, KVM, VMWare, etc) are called Providers, while the Vagrant itself is termed as Provisioner.

What is KVM:

KVM (Kernel based Virtual Machine) is a virtualization module in Linux kernel, that allows Kernel to function as a Hypervisor. In CentOS 7 or later, KVM has replaced legacy Xen Kernel, and now the KVM is used in most of the Linux distros for virtualization.

KVM is free and open source and we can use it to configure a high performance and feature enrich virtualization server.

Environment Specification:

We are provisioned a CentOS 8 Server with following specifications.

  • CPU – 3.4 Ghz (4 cores)
  • Memory – 8 GB
  • Storage – 80 GB
  • Operating System – CentOS 8.1
  • Hostname – vagrant-02.centlinux.com
  • IP Address – 192.168.116.206 /24

Update Software Packages in CentOS 8:

Connect with vagrant-02.centlinux.com as root user by using PuTTY.

We are updating installed software packages in our CentOS 8 server by using dnf command.

# dnf update -y

Install Libvirt on CentOS 8:

Since, we are planning to use Vagrant with KVM provider, therefore, we need to install libvirt and KVM/QEMU packages on our CentOS 8 server.

# dnf install -y @virt virt-install

Here, we have installed libvirt and KVM/QEMU on CentOS 8 but for a detailed step by step guide you can refer to our previous article: Install KVM based Virtualization Server.

Install Ruby on CentOS 8:

Vagrant is written in Ruby and it requires Ruby development tools for compilation of Vagrant extensions and plugins. Thus, we are required to install Ruby on CentOS 8 before installing Vagrant.

Ruby 2.5.5 is available in AppStream yum repository and we can install it using dnf command.

# dnf install -y ruby ruby-devel

Check version of installed ruby command.

# ruby -v
ruby 2.5.5p157 (2019-03-15 revision 67260) [x86_64-linux]

Install Nokogiri on CentOS 8:

Ruby requires Nokogiri library for parsing HTML and XML while compliling Vagrant extensions.

We can either install nokogiri from EPEL (Extra Packages for Enterprise Linux) or by using RubyGem package manager.

Since, we have RubyGem package manager already installed with installation of Ruby, therefore, we can use it to install nokogiri gem.

But first we have to install the nokogiri dependencies as follows.

# dnf install -y make gcc rpm-build ruby-devel zlib-devel

Now, install nokogiri library using gem command.

# gem install nokogiri
Fetching: mini_portile2-2.4.0.gem (100%)
Successfully installed mini_portile2-2.4.0
Fetching: nokogiri-1.10.9.gem (100%)
Building native extensions. This could take a while...
Successfully installed nokogiri-1.10.9
2 gems installed

Install Vagrant on CentOS 8:

At the time of this writeup, latest stable version is Vagrant 2.2.9. You can download Vagrant 2.2.9 for your respective operating system from Vagrant website.

Vagrant Download Page

Copy the URL of your required Vagrant RPM and use wget command to download it.

# wget https://releases.hashicorp.com/vagrant/2.2.9/vagrant_2.2.9_x86_64.rpm
--2020-05-08 15:40:41--  https://releases.hashicorp.com/vagrant/2.2.9/vagrant_2.2.9_x86_64.rpm
Resolving releases.hashicorp.com (releases.hashicorp.com)... 151.101.241.183, 2a04:4e42:39::439
Connecting to releases.hashicorp.com (releases.hashicorp.com)|151.101.241.183|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 43598220 (42M) [application/x-redhat-package-manager]
Saving to: âvagrant_2.2.9_x86_64.rpmâ

vagrant_2.2.9_x86_6 100%[===================>]  41.58M   937KB/s    in 48s

2020-05-08 15:41:30 (890 KB/s) - âvagrant_2.2.9_x86_64.rpmâ saved [43598220/43598220]

Install Vagrant RPM on CentOS 8 by using dnf command.

# dnf install -y vagrant_2.2.9_x86_64.rpm

Check version of installed vagrant command.

# vagrant --version
Vagrant 2.2.9

Vagrant has been installed on CentOS 8 server.

Install Vagrant plugin for KVM Provider:

We need to install a Vagrant plugin to use KVM provider.

libvirt-devel package is required for vagrant-libvirt plugin installation, and we are installing it by using dnf command.

# dnf install –y libvirt-devel

Now, install vagrant-libvirt plugin as follows.

# 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.0.45.gem
Installed the plugin 'vagrant-libvirt (0.0.45)'!

We have successfully installed Vagrant with KVM on CentOS 8.

Use Vagrant Boxes to create a KVM Virtual Machine:

Boxes are the package format for Vagrant environments. A box can be used by anyone on any platform that Vagrant supports to bring up an identical working environment.

You can search for your required boxes at Vagrant Cloud.

Download Vagrant Boxes

We are downloading the centos/7 box by using vagrant command.

# vagrant box add centos/7 --provider=libvirt
==> box: Loading metadata for box 'centos/7'
    box: URL: https://vagrantcloud.com/centos/7
==> box: Adding box 'centos/7' (v1905.1) for provider: libvirt
    box: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/1905.1/providers/libvirt.box
==> box: Box download is resuming from prior download progress
Download redirected to host: cloud.centos.org
==> box: Successfully added box 'centos/7' (v1905.1) for 'libvirt'!

Vagrant also needs rsync utility, so install it using dnf command.

# dnf install -y rsync

Create a project directory for our CentOS 7 virtual machine.

# mkdir centos7
# cd centos7

Use vagrant command to initialize our CentOS 7 project. This command will create a Vagrantfile in our project directory, that can be used to create the desired virtual machine. You can customize this Vagrantfile according to your requirements.

# vagrant init centos/7
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

Create and start CentOS 7 virtual machine as follows.

# vagrant up
Bringing machine 'default' up with 'libvirt' provider...
==> default: Checking if box 'centos/7' version '1905.1' is up to date...
==> default: Creating image (snapshot of base box volume).
==> default: Creating domain with the following settings...
==> default:  -- Name:              centos7_default
==> default:  -- Domain type:       kvm
==> default:  -- Cpus:              1
==> default:  -- Feature:           acpi
==> default:  -- Feature:           apic
==> default:  -- Feature:           pae
==> default:  -- Memory:            512M
==> default:  -- Management MAC:
==> default:  -- Loader:
==> default:  -- Nvram:
==> default:  -- Base box:          centos/7
==> default:  -- Storage pool:      default
==> default:  -- Image:             /var/lib/libvirt/images/centos7_default.img (41G)
==> default:  -- Volume Cache:      default
==> default:  -- Kernel:
==> default:  -- Initrd:
==> default:  -- Graphics Type:     vnc
==> default:  -- Graphics Port:     -1
==> default:  -- Graphics IP:       127.0.0.1
==> default:  -- Graphics Password: Not defined
==> default:  -- Video Type:        cirrus
==> default:  -- Video VRAM:        9216
==> default:  -- Sound Type:
==> default:  -- Keymap:            en-us
==> default:  -- TPM Path:
==> default:  -- INPUT:             type=mouse, bus=ps2
==> default: Creating shared folders metadata...
==> default: Starting domain.
==> default: Waiting for domain to get an IP address...
==> default: Waiting for SSH to become available...
    default:
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default:
    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: Configuring and enabling network interfaces...
    default: SSH address: 192.168.121.18:22
    default: SSH username: vagrant
    default: SSH auth method: private key
==> default: Rsyncing folder: /root/centos7/ => /vagrant

To check status of our CentOS 7 virtual machine.

# virsh list
 Id    Name                           State
----------------------------------------------------
 2     centos7_default                running

We have successfully created our first virtual machine by using Vagrant boxes and KVM provider.

Conclusion – Install Vagrant with KVM Provider:

In this article, you have learned how to install Vagrant with KVM Provider on CentOS 8. If you planning to implement Vagrant in your environment then we strongly recommend you to read Hands-On DevOps with Vagrant (PAID LINK) by Packt Publishing or enroll yourself or your team in Fundamentals of Vagrant course by Udemy.

Scroll to Top