Vagrant is a command-line tool for building and managing reproducible virtual machine environments, and when paired with VirtualBox it gives developers a fast, portable way to spin up isolated Linux VMs on a RHEL 8 host. Using a declarative Vagrantfile, you can define the base box, network settings, shared folders, and provisioning scripts so that any team member can recreate an identical environment with a single command. This tutorial walks you through adding the VirtualBox repository, installing VirtualBox and its required kernel modules, installing Vagrant via the HashiCorp repository, and creating your first VM with synced folders and an automated shell provisioner. By the end you will have a fully working local virtualisation workflow on RHEL 8.
Prerequisites
- RHEL 8 host with hardware virtualisation enabled in BIOS/UEFI (Intel VT-x or AMD-V)
- A non-root user with
sudoprivileges or a root shell - At least 4 GB RAM and 20 GB free disk space for VM storage
- Internet access for package downloads and box retrieval from Vagrant Cloud
curlanddnfavailable
Step 1 — Add the VirtualBox Repository and Install VirtualBox
Oracle publishes official RPM packages for VirtualBox through a dedicated yum repository. Create the repo file manually so you can pin it to a specific major version.
cat > /etc/yum.repos.d/virtualbox.repo <<'EOF'
[virtualbox]
name=Oracle Linux / RHEL / CentOS-$releasever / $basearch - VirtualBox
baseurl=https://download.virtualbox.org/virtualbox/rpm/el/$releasever/$basearch
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://www.virtualbox.org/download/oracle_vbox_2016.asc
EOF
dnf install -y VirtualBox-7.0
VBoxManage --version
If dnf reports a GPG key error, import the Oracle VirtualBox signing key explicitly: rpm --import https://www.virtualbox.org/download/oracle_vbox_2016.asc.
Step 2 — Install Kernel Development Headers and Build VirtualBox Kernel Modules
VirtualBox requires compiled kernel modules (vboxdrv, vboxnetflt, vboxnetadp) that match the running kernel. Install the matching kernel development packages and run the VirtualBox module builder.
dnf install -y kernel-devel kernel-headers gcc make perl
# Ensure the kernel-devel version matches the running kernel
uname -r
rpm -q kernel-devel
# Build and load the VirtualBox kernel modules
/sbin/vboxconfig
# Verify the modules are loaded
lsmod | grep vbox
# Expected output includes: vboxdrv, vboxnetflt, vboxnetadp
If vboxconfig fails with a build error, the most common cause is a mismatch between the running kernel and the installed kernel-devel version. Reboot after installing kernel updates, then re-run /sbin/vboxconfig.
Step 3 — Add the HashiCorp Repository and Install Vagrant
HashiCorp maintains an official RPM repository for all its tools including Vagrant. Add the repo and install Vagrant with a single dnf command.
dnf install -y dnf-plugins-core
dnf config-manager --add-repo
https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
dnf install -y vagrant
vagrant --version
# Expected: Vagrant 2.x.x
# (Optional) Install the VirtualBox guest additions plugin
vagrant plugin install vagrant-vbguest
Step 4 — Initialise and Start Your First VM
Use vagrant init to create a Vagrantfile in a new project directory, then bring the VM up. On the first run, Vagrant downloads the specified base box from Vagrant Cloud, which may take a few minutes depending on connection speed.
mkdir ~/vagrant-demo && cd ~/vagrant-demo
vagrant init ubuntu/focal64
# Start the VM (downloads the box on first run)
vagrant up
# SSH into the running VM
vagrant ssh
# Exit the VM shell
exit
# Suspend (save state) and resume
vagrant halt
vagrant up
# Destroy the VM completely
vagrant destroy -f
The base box ubuntu/focal64 provides Ubuntu 20.04. RHEL-family boxes such as generic/rhel8 are also available on Vagrant Cloud and can be substituted as the base image.
Step 5 — Customise the Vagrantfile with a Synced Folder and Shell Provisioner
Edit the generated Vagrantfile to map a host directory into the VM and run a shell script automatically on first boot. This replaces the default generated file content with a purposeful configuration.
cat > ~/vagrant-demo/Vagrantfile <<'EOF'
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/focal64"
# Map the project directory on the host to /app inside the VM
config.vm.synced_folder ".", "/app", type: "virtualbox"
# Configure VirtualBox provider settings
config.vm.provider "virtualbox" do |vb|
vb.memory = "2048"
vb.cpus = 2
vb.name = "rhel8-demo-vm"
end
# Forward host port 8080 to guest port 80
config.vm.network "forwarded_port", guest: 80, host: 8080
# Shell provisioner — runs once on first `vagrant up`
config.vm.provision "shell", inline: <<-SHELL
apt-get update -y
apt-get install -y nginx
systemctl enable --now nginx
echo "Provisioned by Vagrant
" > /var/www/html/index.html
SHELL
end
EOF
vagrant up --provision
After provisioning completes, open http://localhost:8080 on the RHEL 8 host to see the NGINX page served from inside the VM. Files placed in the project directory on the host are immediately visible at /app inside the VM due to the synced folder mapping.
Step 6 — Common Vagrant Workflow Commands
The following reference commands cover the most frequent day-to-day operations when working with Vagrant environments.
# List all Vagrant VMs and their states
vagrant global-status
# Reprovision a running VM without destroying it
vagrant provision
# Reload the VM (applies Vagrantfile changes without full rebuild)
vagrant reload --provision
# Package the current VM state into a reusable box
vagrant package --output my-custom-box.box
# Add the packaged box to the local box list
vagrant box add my-custom-box my-custom-box.box
# List locally cached boxes
vagrant box list
# Remove a cached box
vagrant box remove ubuntu/focal64
Conclusion
You have set up VirtualBox 7.0 on RHEL 8 with correctly built kernel modules, installed Vagrant via the HashiCorp repository, and created a VM with synced folders, port forwarding, and an automated shell provisioner. The declarative Vagrantfile format makes these environments fully reproducible and version-controllable alongside your application code. Vagrant dramatically reduces “works on my machine” problems by giving every developer an identical, easily disposable local environment.
Next steps: How to Set Up ArgoCD for GitOps on RHEL 8, How to Build a Jenkins CI/CD Pipeline on RHEL 8, and How to Install and Use Helm for Kubernetes Package Management on RHEL 8.