You can support us by downloading this article as PDF from the Link below. Download the guide as PDF

How to create a CentOS 8 VM using Vagrant?, how can I run CentOS 8 on KVM/VirtualBox/VMWare using Vagrant?, Can I use Vagrant to manage CentOS 8 virtual machine?. Vagrant enables you to create and configure reproducible, and portable development environments using Virtual machine images called Boxes.

With Vagrant, you can setup your development environments in seconds on various virtualization platforms such as VirtualBox, KVM, VMware, Hyper-V, e.t.c. CentOS 8 has been available for a while now. if you love working with new technologies and already started building your apps from CentOS 8, you can use this guide to setup your Vagrant Lab.

Step 1: Install Vagrant

Check our guides below:

How To Install Vagrant and VirtualBox on Fedora

Install Vagrant on Debian / Ubuntu / Kali Linux

Step 2: Download CentOS 8 Vagrant box

Download Vagrant box for your virtualization environment using one of the commands below:

For KVM users:

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

==> box: Loading metadata for box 'centos/8'
    box: URL: https://vagrantcloud.com/centos/8
==> box: Adding box 'centos/8' (v1905.1) for provider: libvirt
    box: Downloading: https://vagrantcloud.com/centos/boxes/8/versions/1905.1/providers/libvirt.box
    box: Download redirected to host: cloud.centos.org
    box: Progress: 48% (Rate: 1305k/s, Estimated time remaining: 0:04:05)^    box: Calculating and comparing box checksum...
==> box: Successfully added box 'centos/8' (v1905.1) for 'libvirt'!

For VirtualBox users:

$ vagrant box add centos/8 --provider=virtualbox
==> box: Loading metadata for box 'centos/8'
    box: URL: https://vagrantcloud.com/centos/8
==> box: Adding box 'centos/8' (v1905.1) for provider: virtualbox
    box: Downloading: https://vagrantcloud.com/centos/boxes/8/versions/1905.1/providers/virtualbox.box
    box: Download redirected to host: cloud.centos.org
    box: Calculating and comparing box checksum...
==> box: Successfully added box 'centos/8' (v1905.1) for 'virtualbox'!

For Parallels:

$ vagrant box add generic/centos8 --provider=parallels
==> box: Loading metadata for box 'generic/centos8'
    box: URL: https://vagrantcloud.com/generic/centos8
==> box: Adding box 'generic/centos8' (v2.0.6) for provider: parallels
    box: Downloading: https://vagrantcloud.com/generic/boxes/centos8/versions/2.0.6/providers/parallels.box
    box: Download redirected to host: vagrantcloud-files-production.s3.amazonaws.com
==> box: Successfully added box 'generic/centos8' (v2.0.6) for 'parallels'!

VMware:

$ vagrant box add generic/centos8 --provider=vmware_desktop
==> box: Loading metadata for box 'generic/centos8'
    box: URL: https://vagrantcloud.com/generic/centos8
==> box: Adding box 'generic/centos8' (v2.0.6) for provider: vmware_desktop
    box: Downloading: https://vagrantcloud.com/generic/boxes/centos8/versions/2.0.6/providers/vmware_desktop.box
    box: Download redirected to host: vagrantcloud-files-production.s3.amazonaws.com
==> box: Successfully added box 'generic/centos8' (v2.0.6) for 'vmware_desktop'!

Docker:

$ vagrant box add generic/centos8 --provider=docker
==> box: Loading metadata for box 'generic/centos8'
    box: URL: https://vagrantcloud.com/generic/centos8
==> box: Adding box 'generic/centos8' (v2.0.6) for provider: docker
    box: Downloading: https://vagrantcloud.com/generic/boxes/centos8/versions/2.0.6/providers/docker.box
    box: Download redirected to host: vagrantcloud-files-production.s3.amazonaws.com
==> box: Successfully added box 'generic/centos8' (v2.0.6) for 'docker'!

Listing available CentOS 8 boxes:

$ vagrant box list
centos/8        (libvirt, 1905.1)
centos/8        (virtualbox, 1905.1)
generic/centos8 (parallels, 2.0.6)
generic/centos8 (vmware_desktop, 2.0.6)
generic/centos8 (docker, 2.0.6)

Step 3: Create Vagrantfile for CentOS 8

Start by creating a Vagrant file.

$ mkdir -p ~/vagrant/centos8 && cd ~/vagrant/centos8
$ vim Vagrantfile

Basic Vagrantfile contents looks like this:

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

ENV['VAGRANT_DEFAULT_PROVIDER'] = 'libvirt'

Vagrant.configure("2") do |config|

  ##### DEFINE VMS #####
  config.vm.define "centos8" do |config|
  config.vm.hostname = "centos8"
  config.vm.box = "centos/8"
  config.vm.box_check_update = false
  end
  config.vm.provider :libvirt do |v|
    v.memory = 1024
    v.cpus = 2
  end
end
  • VirtualBox
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  config.vm.box = "centos/8"
  config.vm.box_check_update = false
  config.vm.hostname = "centos8"
  config.vm.provider "virtualbox" do |vb|
  # Display the VirtualBox GUI when booting the machine
     vb.gui = false
     vb.memory = "2048"
     vb.cpus = 2
  end
end

For other providers, visit the official Vagrant documentation. Once the file has been created, start the VM by running:

 vagrant up            
Bringing machine 'centos8' up with 'libvirt' provider...
==> centos8: Uploading base box image as volume into libvirt storage...
==> centos8: Creating image (snapshot of base box volume).
==> centos8: Creating domain with the following settings...
==> centos8:  -- Name:              centos8_centos8
==> centos8:  -- Domain type:       kvm
==> centos8:  -- Cpus:              2
==> centos8:  -- Feature:           acpi
==> centos8:  -- Feature:           apic
==> centos8:  -- Feature:           pae
==> centos8:  -- Memory:            1024M
==> centos8:  -- Management MAC:    
==> centos8:  -- Loader:            
==> centos8:  -- Nvram:             
==> centos8:  -- Base box:          centos/8
==> centos8:  -- Storage pool:      default
==> centos8:  -- Image:             /var/home/jkmutai/.local/share/libvirt/images/centos8_centos8.img (11G)
==> centos8:  -- Volume Cache:      default
==> centos8:  -- Kernel:            
==> centos8:  -- Initrd:            
==> centos8:  -- Graphics Type:     vnc
==> centos8:  -- Graphics Port:     -1
==> centos8:  -- Graphics IP:       127.0.0.1
==> centos8:  -- Graphics Password: Not defined
==> centos8:  -- Video Type:        cirrus
==> centos8:  -- Video VRAM:        9216
==> centos8:  -- Sound Type:	
==> centos8:  -- Keymap:            en-us
==> centos8:  -- TPM Path:          
==> centos8:  -- INPUT:             type=mouse, bus=ps2
==> centos8: Creating shared folders metadata...
==> centos8: Starting domain.
==> centos8: Waiting for domain to get an IP address...
==> centos8: Waiting for SSH to become available...
    centos8: 
    centos8: Vagrant insecure key detected. Vagrant will automatically replace
    centos8: this with a newly generated keypair for better security.
    centos8: 
    centos8: Inserting generated public key within guest...
    centos8: Removing insecure key from the guest if it's present...
    centos8: Key inserted! Disconnecting and reconnecting using new SSH key...
==> centos8: Setting hostname...
==> centos8: Configuring and enabling network interfaces...
    centos8: SSH address: 192.168.122.2:22
    centos8: SSH username: vagrant
    centos8: SSH auth method: private key
==> centos8: Rsyncing folder: /var/home/jkmutai/vagrant/centos8/ => /vagrant

Try ssh:

vagrant ssh
[[email protected] ~]$ cat /etc/os-release 
NAME="CentOS Linux"
VERSION="8 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="8"
PLATFORM_ID="platform:el8"
PRETTY_NAME="CentOS Linux 8 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:8"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-8"
CENTOS_MANTISBT_PROJECT_VERSION="8"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="8"

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

[[email protected] ~]$ 
logout
Connection to 192.168.122.2 closed.

If you want to destroy the VM, use:

$ vagrant destroy   
    centos8: Are you sure you want to destroy the 'centos8' VM? [y/N] y
==> centos8: Removing domain...

Whenever you make a change to the Vagrantfile, restart the machine for the changes to take effect.

$ vagrant reload

To stop the instance, use:

$ vagrant halt

If you would like to save the current state of the VM while stopping it, use:

$ vagrant suspend

With this, you’ll return to exactly the same state at a later time when VM is started.

Destroy the Vagrant machine when done  by running:

$ vagrant destroy

More guides:

Best Linux Books for Beginners & Experts

Best CCNA R&S (200-125) Certification Preparation Books

Best LPIC-1 and LPIC-2 certification study books

How To Create CentOS 8 KVM Image Template on OpenStack

You can support us by downloading this article as PDF from the Link below. Download the guide as PDF