Virtualization is a very old technology but still finds high use, especially in cloud computing. It involves creating software-based or virtual versions of computer resources such as storage, networks, applications, and servers. This makes it possible to partition a single server into multiple virtual instances that run and interact independently.

There are mainly two types of virtualization. These are:

  • Type 1/Bare Metal virtualization where the hypervisor is installed directly on top of the physical machine. For example Microsoft Hyper-V, open-source Kernel-based VMs (KVMs), VMware ESXi
  • Type 2/Hosted virtualization where the hypervisor is installed on an existing operating system. For example VMware Workstation and Oracle VirtualBox

In this guide, we will discuss how to install and use Vagrant With VirtualBox / KVM on Rocky Linux 9.

Vagrant is an open-source tool that can be used to build and manage virtual machine environments in a single workflow. It provides the simplest and easiest command line way to manage virtual environments. This reduces the development environment setup time while increasing the production parity.

Vagrant is preferred due to the following features:

  • It is built on top of industry-standard technology and is controlled by a single consistent workflow.
  • It offers simplicity when setting up an environment.
  • It standardizes environments across platforms
  • Virtual machines can easily be provisioned on top of VirtualBox, VMware, AWS, or any other provider.
  • It is a cross-platform tool

There are terminologies involved when dealing with Vagrant. These are:

  • Vagrant Box: This is the Vagrant setup unit. just like docker, Vagrant is a self-contained image. This is a packaged Vagrant environment.
  • Vagrant File: This is the configuration file for Vagrant. It contains the configurations used to create a VM.
  • Provider: is the location in which the virtual environment runs, the default option is Virtualbox but can be set to any other tool such as KVM, docker e.t.c

Now let’s plunge in and enjoy the awesomeness of this tool!

Before you Begin

Before we begin, you need a hypervisor installed on your Rocky Linux 9 machine. For this case, we will use KVM or Virtualbox, which can be installed with the aid below;

1. Install Vagrant on Rocky Linux 9

Once a preferred hypervisor has been installed, proceed and install Vagrant on Rocky Linux 9.

To achieve this, begin by adding the Vagrant repository to the system.

sudo yum install yum-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo

Install Vagrant on Rocky Linux 9 with the command:

sudo yum install vagrant

Dependency Tree:

Dependencies resolved.
=======================================
 Package
        Arch   Version Repo       Size
=======================================
Installing:
 vagrant
        x86_64 2.3.0-1 hashicorp 108 M

Transaction Summary
=======================================
Install  1 Package

Total download size: 108 M
Installed size: 257 M
Is this ok [y/N]: y

2. Using Vagrant with VirtualBox / KVM on Rocky Linux 9

Once installed, Vagrant can be used to create/provision Virtual Machine on your hypervisor.

First, create a working directory for Vagrant and navigate into it:

mkdir Vagrant && cd Vagrant

Install the Vagrant Libvirt Provider(KVM Only)

For KVM, you need to download the Vagrant plugin. First, enable the PowerTools CRB repo:

sudo dnf install epel-release
sudo dnf config-manager --set-enabled crb

Install the required build tools:

sudo dnf install gcc make perl kernel-devel kernel-headers bzip2 dkms elfutils-libelf-devel
sudo yum groupinstall "Development Tools
sudo dnf install libvirt-devel

Install the Vagrant Libvirt Provider

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

Verify the installation with the command:

$ vagrant plugin list
vagrant-libvirt (0.10.1, global)

Configure and Initialize the VM

Now download a Vagrant box file for the desired VM and set the provider. For example Ubuntu 20.04

##For VirtualBox
vagrant box add generic/ubuntu2004 --provider=virtualbox

##For KVM
vagrant box add generic/ubuntu2004 --provider=libvirt

Sample Output:

==> box: Loading metadata for box 'generic/ubuntu2004'
    box: URL: https://vagrantcloud.com/generic/ubuntu2004
==> box: Adding box 'generic/ubuntu2004' (v4.1.6) for provider: libvirt
    box: Downloading: https://vagrantcloud.com/generic/boxes/ubuntu2004/versions/4.1.6/providers/libvirt.box
    box: Calculating and comparing box checksum...

==> box: Successfully added box 'generic/ubuntu2004' (v4.1.6) for 'libvirt'!

Create a Vagrant file for the VM using the init command:

$ vagrant init generic/ubuntu2004
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.

You can view the Vagrantfile:

cat Vagrantfile

Start the VM with the command:

vagrant up

Sample Output:

Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'generic/ubuntu2004'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'generic/ubuntu2004' version '4.1.6' is up to date...
==> default: Setting the name of the VM: Vagrant_default_1660834980416_30148
.....
    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: Machine booted and ready!
==> default: Checking for guest additions in VM...

After this, you will have the VM created and running on your hypervisor.

For VirtualBox

Vagrant With VirtualBoxKVM on Rocky Linux 9

For KVM

Vagrant With VirtualBoxKVM on Rocky Linux 9 1

Creating a Custom Vagrantfile

It is also possible to create your own Vagrantfiles to run the VMs. This can be done as shown:

vim Vagrantfile

Add the lines below replacing them appropriately:

# -*- mode: ruby -*-
# vi: set ft=ruby :

ENV['VAGRANT_DEFAULT_PROVIDER'] = 'virtualbox'

Vagrant.configure("2") do |config|
  ##### DEFINE VM #####
  config.vm.define "centos01" do |config|
  config.vm.hostname = "centos7.example.com"
  config.vm.box = "centos/7"
  config.vm.box_check_update = true
  end
end

Now start the VM with the command:

vagrant up

SSH into the machine

You can ssh to the VM with the Vagrant command below:

$ vagrant ssh 
[email protected]:~$ 

You are now set to interact with your VM as preferred. To terminate the session. use CTRL+D or the