Install Vagrant on RHEL9 or CentOS9

Install Vagrant on RHEL9

In this article, we will review how to install vagrant on RHEL9 or CentOS9 as well as, some usage scenarios to gain a better understanding.

Table of Contents

Introduction

Virtualization has become a cornerstone for efficient software development and testing. Vagrant, when combined with Virtualbox, offers a powerful solution for creating and managing virtual environments. In this guide, we’ll walk you through the process of installing and using Vagrant on Red Hat Enterprise Linux 9 (RHEL 9) or CentOS 9, providing practical examples to streamline your virtualization workflow.

Install Vagrant on RHEL9

Photo by admingeek from Infotechys

Brief Overview of Vagrant

Vagrant

Vagrant is an open-source tool designed to simplify the creation and management of virtualized development environments. Developed by HashiCorp, Vagrant provides a consistent and reproducible workflow for setting up, configuring, and sharing virtual machines (VMs) across different platforms. It focuses on eliminating the “it works on my machine” problem by enabling developers to create standardized environments that can be easily shared and replicated.

Key Features

  • Cross-Platform Compatibility: Vagrant works seamlessly across various operating systems, including Windows, macOS, and Linux, ensuring consistency in development environments regardless of the host machine.

  • Configuration as Code: Environments are defined in a Vagrantfile, a simple and human-readable configuration file. This file specifies the base box, provisioning scripts, network settings, and other configurations, making it easy to version control and share with team members.

  • Box Ecosystem: Vagrant uses “boxes” as base images for VMs. These pre-configured images can be customized and shared, allowing developers to start with a known state and easily distribute environments.

  • Provisioning: Vagrant supports various provisioners, such as shell scripts, Ansible, Puppet, and Chef. This enables automatic installation and configuration of software within the VM during the provisioning process.

  • Networking: Vagrant provides flexible networking options, allowing users to configure port forwarding, create private networks, and establish communication between multiple VMs.

System Requirements

To install Vagrant on Red Hat Enterprise Linux 9 (RHEL 9), ensure that your system meets the following requirements:

Operating SystemRed Hat Enterprise Linux 9 (RHEL 9)
Compatibility with CentOS 9, as CentOS shares a similar codebase with RHEL
Hardware RequirementsA 64-bit x86-based system
Recommended minimum of 2GB RAM (more may be required based on the VMs you plan to create)
Adequate free disk space for storing virtual machine images and configurations
Software DependenciesVirtualization Technology: Ensure that virtualization technology is enabled in the BIOS/UEFI settings. This is necessary for running virtual machines.
Kernel headers: Install the kernel headers for your RHEL 9 system to support Virtualbox kernel modules.
Development Tools: Install essential development tools, including compilers and libraries, to build kernel modules.
Internet ConnectionA stable internet connection is required to download Vagrant and any necessary dependencies during the installation process.
OptionalsVirtualbox: If you plan to use Virtualbox as the provider for Vagrant, you’ll need to ensure that Virtualbox is installed. You can install Virtualbox using the package manager available in RHEL 9.
Other providers: If you plan to use a different provider, such as libvirt or VMware, ensure that the required dependencies and software for that provider are installed.
User PermissionsEnsure that the user account performing the installation has the necessary permissions to install software and configure system settings.
SELinux ConsiderationsIf SELinux is enabled, make sure to configure SELinux policies appropriately to avoid conflicts with Vagrant and its components.
Latest UpdatesKeep your RHEL 9 system up to date with the latest patches and updates to ensure compatibility with the Vagrant version you are installing.
Package Manager

Ensure proper functionality of the dnf package manager by registering your system through the subscription-manager tool in RHEL 9. You can obtain the essential entitlements required for package installation and updates by leveraging the Red Hat Developer License. This license offers 16 entitlements that can be utilized in your computing environment at no cost, playing a vital role in installing the dependencies and packages necessary for Vagrant.

NOTE: You can also consider installing Vagrant on CentOS 9 as an alternative. CentOS, being the open-source counterpart to RHEL 9, will work just as well.

Install Vagrant on RHEL9: Step-by-Step Instructions

Follow these steps to install vagrant on RHEL9 or CentOS9.

Update your machine

Before you begin the installation process, it’s essential to ensure that your system is up-to-date. Run the following command(s) in your terminal:

				
					$ sudo dnf update -y
				
			

Download and Install the hashicorp repository

Run the following dnf command to install the hashicorp repo:

				
					$ sudo dnf config-manager --add-repo=https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
				
			

Upon successfully installing the hashicorp repository, we can proceed to install vagrant.

				
					Updating Subscription Management repositories.
Unable to read consumer identity

This system is not registered with an entitlement server. You can use subscription-manager to register.

Adding repo from: https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
				
			

Install Vagrant on RHEL9 or CentOS9

Install vagrant with the following dnf command:

				
					$ sudo dnf install vagrant 
...
Downloading Packages:
vagrant-2.4.0-1.x86_64.rpm                                                                                                           29 MB/s | 146 MB     00:05    
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                29 MB/s | 146 MB     00:05     
Hashicorp Stable - x86_64                                                                                                            48 kB/s | 3.9 kB     00:00    
Importing GPG key 0xA621E701:
 Userid     : "HashiCorp Security (HashiCorp Package Signing) <security+packaging@hashicorp.com>"
 Fingerprint: 798A EC65 4E5C 1542 8C8E 42EE AA16 FCBC A621 E701
 From       : https://rpm.releases.hashicorp.com/gpg
Is this ok [y/N]: y
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                            1/1 
  Installing       : vagrant-2.4.0-1.x86_64                                                                                                                     1/1 
  Running scriptlet: vagrant-2.4.0-1.x86_64                                                                                                                     1/1 
  Verifying        : vagrant-2.4.0-1.x86_64                                                                                                                     1/1 
Installed products updated.

Installed:
  vagrant-2.4.0-1.x86_64                                                                                                                                            

Complete!

				
			

Post-Installation Procedure

Congratulations! Vagrant was installed successfully. Now, we can initialize it for a project and begin to configure it for our needs.

NOTE: Before moving on to the next section, make sure to install a provider (e.g., VirtualBox) if you haven’t done so already.

 

Initialize Vagrant

Begin by establishing a directory to contain all essential project files. For illustration, let’s form a directory named “testing” to serve as the foundation for our Vagrant project, followed by the initiation of Vagrant:

				
					$ mkdir testing

$ cd testing

$ vagrant init generic-x64/centos9s
				
			

The breakdown

The command vagrant init generic-x64/centos9s initializes a new Vagrant project in the current directory and specifies the base box to use for creating virtual machines. Let’s break down the components of this command:

  • vagrant init: This part of the command initializes a new Vagrant project. It creates a Vagrantfile in the current directory, which is the configuration file for the Vagrant environment.

  • generic-x64/centos9s: This specifies the base box for the virtual machine. In this case, it is the CentOS 9 box. A “box” is a pre-configured virtual machine image that serves as the foundation for the VMs created by Vagrant.

When you run vagrant init generic-x64/centos9s, it sets up a basic Vagrant project using the CentOS 9 box as the starting point. The generated Vagrantfile will have default configurations, and you can further customize it to meet your specific requirements, such as adjusting VM settings, network configurations, and provisioning scripts. After initialization, you typically proceed to customize the Vagrantfile and then use vagrant up to create and launch the virtual machine.

				
					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.

				
			

Customize the Vagrantfile

Here is the default Vagrantfile generated upon initialization. Customize this file to define VM hostnames, adjust network settings, and implement provisioning through configuration management tools such as Ansible, Chef, Puppet, and more.

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

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  config.vm.box = "generic-x64/centos9s"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # NOTE: This will enable public access to the opened port
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # config.vm.network "private_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"

  # Disable the default share of the current code directory. Doing this
  # provides improved isolation between the vagrant box and your host
  # by making sure your Vagrantfile isn't accessable to the vagrant box.
  # If you use this you may want to enable additional shared subfolders as
  # shown above.
  # config.vm.synced_folder ".", "/vagrant", disabled: true

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  # config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  # end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Enable provisioning with a shell script. Additional provisioners such as
  # Ansible, Chef, Docker, Puppet and Salt are also available. Please see the
  # documentation for more information about their specific syntax and use.
  # config.vm.provision "shell", inline: <<-SHELL
  #   apt-get update
  #   apt-get install -y apache2
  # SHELL
end


				
			

Start and connect to VM

Upon completing the configuration changes, you can start the VM and connect to it by running the following commands:

				
					$ vagrant up
$ vagrant ssh

				
			

If you’ve retained the default Vagrantfile generated, your (vagrant up) output should resemble the example below:

				
					Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'generic-x64/centos9s' could not be found. Attempting to find and install...
    default: Box Provider: virtualbox
    default: Box Version: >= 0
==> default: Loading metadata for box 'generic-x64/centos9s'
    default: URL: https://vagrantcloud.com/api/v2/vagrant/generic-x64/centos9s
==> default: Adding box 'generic-x64/centos9s' (v4.3.10) for provider: virtualbox (amd64)
    default: Downloading: https://vagrantcloud.com/generic-x64/boxes/centos9s/versions/4.3.10/providers/virtualbox/amd64/vagrant.box
    default: Calculating and comparing box checksum...
==> default: Successfully added box 'generic-x64/centos9s' (v4.3.10) for 'virtualbox (amd64)'!
==> default: Importing base box 'generic-x64/centos9s'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'generic-x64/centos9s' version '4.3.10' is up to date...
==> default: Setting the name of the VM: testing_default_1705372278225_85098
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    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: Machine booted and ready!
==> default: Checking for guest additions in VM...


				
			

Execute the vagrant ssh command to access your newly created CentOS 9 VM via SSH.

				
					$ vagrant ssh
[vagrant@centos9s ~]$ cat /etc/centos-release
CentOS Stream release 9

				
			

Common Vagrant Errors

Here is a list of common Vagrant errors that users may encounter, along with brief explanations:

Box not found

This error occurs when the specified box is either unavailable or inaccessible. Verify the availability of your box by double-checking the Vagrant Boxes search site. Alternatively, try initializing a different box (vagrant init <box_name>) to determine if the issue is specific to your chosen box

				
					The box 'box_name' could not be found or could not be accessed in the remote catalog. 
If this is a private box on HashiCorp's Vagrant Cloud, please verify you're logged in via 'vagrant login'.

URL: ["https://vagrantcloud.com/<box_name>"]
Error: The requested URL returned error: 404
				
			

Provider-specific error (e.g., VirtualBox, VMware):

 Issues with the provider, such as VirtualBox not being installed or configured correctly.

				
					No usable default provider could be found for your system.

Vagrant relies on interactions with 3rd party systems, known as
"providers", to provide Vagrant with resources to run development
environments. Examples are VirtualBox, VMware, Hyper-V.

The easiest solution to this message is to install VirtualBox, which
is available for free on all major platforms.

If you believe you already have a provider available, make sure it
is properly installed and configured. You can see more details about
why a particular provider isn't working by forcing usage with
`vagrant up --provider=PROVIDER`, which should give you a more specific
error message for that particular provider.

				
			

Network interface errors

Port forwarding conflicts with existing applications or services.

				
					Vagrant cannot forward the specified ports on this VM, 
since they would collide with some other application that is already listening on these ports.
				
			

SSH connection errors

SSH connection issues, often due to firewall settings or misconfigurations.

				
					Warning: Remote connection disconnect. Retrying...
				
			

Shared folder permission issues

Permission issues with shared folders, often related to NFS configuration.

				
					NFS is reporting that your exports file is invalid.
				
			

Memory and CPU allocation errors

Attempting to allocate more resources than the provider allows.

				
					The amount of memory configured via 'v.customize' exceeds the maximum amount allowed by the provider (VirtualBox).
				
			

Provider-specific version mismatch

Incompatibility between Vagrant and the provider versions.

				
					The provider 'virtualbox' that was requested to back the machine 'default' 
is reporting that it isn't usable on this system.
				
			

Missing required plugins

Required plugins are not installed. Run vagrant plugin install plugin_name to resolve.

				
					The following plugins must be installed: plugin_name.
				
			

Vagrantfile syntax errors

Issues with the syntax of the Vagrantfile, such as misplaced or incorrect configurations. Here are some examples:

				
					# Incorrect: Improper nesting of blocks
config.vm.define "web_server" do |web|
end
config.vm.network "forwarded_port", guest: 80, host: 8080
#---------------------------------------------------------------------------------------------#
# Incorrect: Incorrect use of quotation marks around the IP address
config.vm.network "private_network", type: "static", ip: '192.168.1.1'
#---------------------------------------------------------------------------------------------#
# Incorrect: 'config.vm.provision' is misspelled as 'config.vm.provisioning'
config.vm.provisioning "shell", path: "script.sh"

				
			

When encountering these errors, it’s important to carefully read the error messages, check the Vagrant documentation, and seek assistance from relevant forums or communities to troubleshoot and resolve the issues.

Conclusion

In this article, we explored Vagrant, delving into its definition, key features, and its versatile applications. We also provided a succinct overview of common errors users may encounter, offering practical solutions.

Did you find this article informative? Feel free to share your thoughts by leaving a comment below and share!

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *