Learn how to install Vagrant with KVM provider in this comprehensive guide. Follow our step-by-step instructions to set up a powerful development environment using Vagrant and KVM on your Linux system. Perfect for developers and system administrators looking to optimize their workflow. #centlinux #vagrant #libvirt
Table of Contents
What is Vagrant?
Vagrant is an open-source tool designed for building and managing virtualized development environments. It simplifies the process of setting up, configuring, and deploying virtual machines by providing a consistent workflow. Vagrant uses a simple configuration file, called a Vagrantfile, to define the properties and behavior of the virtual environments, making it easy for developers to create and share reproducible environments. It supports various virtualization providers, including VirtualBox, VMware, and KVM, and can integrate with configuration management tools like Ansible, Puppet, and Chef. This makes Vagrant a valuable tool for developing, testing, and deploying software in a consistent and isolated environment.
Key Features of Vagrant:
- Portability: Vagrant environments are defined in a Vagrantfile, which makes it easy to share and replicate environments across different systems and teams.
- Multi-Provider Support: Vagrant works with various virtualization providers like VirtualBox, VMware, Hyper-V, Docker, and KVM, allowing flexibility in choosing the underlying infrastructure.
- Provisioning: Vagrant supports provisioning tools like Shell scripts, Ansible, Chef, and Puppet to automatically configure and manage the environment setup.
- Version Control Integration: Vagrant environments can be versioned and stored in version control systems like Git, enabling teams to track changes and collaborate effectively.
- Networking: Vagrant allows easy setup of networking configurations, including forwarded ports, private networks, and public networks, making it suitable for both local development and cloud deployments.
- Synced Folders: Vagrant enables synchronization between the host and guest machines’ directories, facilitating seamless code sharing and development.
Common Use Cases:
- Development Environments: Developers use Vagrant to create isolated environments that mimic production, ensuring that code runs as expected regardless of the developer’s local setup.
- Testing and Continuous Integration: Vagrant is used in CI/CD pipelines to spin up fresh environments for automated testing, ensuring consistency and reliability in test results.
- Learning and Training: Vagrant provides a sandboxed environment ideal for learning new technologies or conducting training sessions without affecting the host system.
- Legacy Applications: Vagrant can be used to maintain and run legacy applications in controlled environments without interfering with modern development setups.
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.
Read Also: Install Vagrant with VirtualBox on Ubuntu Server 18
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.
Recommended Online Training: Vagrant Essentials: Learn DevOps Using Vagrant
Linux Server 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 Linux Software Packages
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
Installing Prerequisites
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.
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.
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.
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.
Conclusion – Install Vagrant with KVM Provider
Setting up Vagrant with KVM provider can significantly enhance your development workflow by providing a consistent and efficient environment. This combination leverages the flexibility of Vagrant and the powerful performance of KVM to create robust virtualized environments. Whether you’re a developer, system administrator, or IT professional, mastering this setup can streamline your projects and boost productivity.
If you need assistance with installing Vagrant with the KVM provider, I’m here to help. With my expertise, I can guide you through the process, troubleshoot any issues, and ensure your environment is optimized for your specific needs.
Enhance your development process with a properly configured Vagrant and KVM setup. Visit my Fiverr gig: DevOps Engineer to get started and take your development environment to the next level!