How to Install Vagrant on CentOS 8

Posted 

3 min read

Vagrant is a command-line tool for building and managing virtualized development environments. By default, Vagrant can provision machines on top of VirtualBox, Hyper-V, and Docker. Support for other providers such as Libvirt (KVM), VMware and AWS can be enabled via the Vagrant plugin system.

Vagrant is typically used by developers to set up a development environment, that matches production.

In this tutorial, we will explain how to install Vagrant on CentOS 8. We’ll also show you how to create a development environment.

Installing Vagrant on CentOS 8

At the time of writing this article, the latest stable version of Vagrant is version 2.2.6. Visit the Vagrant downloads page to see if there is a new version of Vagrant available.

To install Vagrant on your CentOS machine, enter the following command as root or user with sudo privileges:

sudo dnf install https://releases.hashicorp.com/vagrant/2.2.6/vagrant_2.2.6_x86_64.rpm

Once the installation is completed, verify that Vagrant was successfully installed by running:

vagrant --version

The command will print the Vagrant version:

Vagrant 2.2.6

Getting Started with Vagrant

Now that you have Vagrant installed on your CentOS system, let’s create a development environment using the VirtualBox provider, which is the default provider for Vagrant. Make sure you have VirtualBox installed on your CentOS 8 system.

The first step is to create a directory that will be the project root directory. Create the project directory and switch to it with:

mkdir ~/my-vagrant-projectcd ~/my-vagrant-project

The next step is to initialize a new Vagrantfile using the vagrant init command and specify the box you want to use. Vagrantfile is a configuration file that describes how the virtual machine is configured and provisioned. It uses Ruby syntax to define the configuration.

Boxes are the package format for the Vagrant environments and are provider-specific. You can find a list of publicly available Vagrant Boxes at the Vagrant box catalog page.

In this example, we’ll use the ubuntu/bionic64 box. Run the following command to initialize a new Vagrantfile:

vagrant init ubuntu/bionic64
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 open the Vagrantfile with your text editor and make adjustments according to your needs.

Run the vagrant up command to create and configure the virtual machine as defined in Vagrantfile:

vagrant up
==> default: Configuring and enabling network interfaces...
    default: SSH address: 192.168.121.24:22
    default: SSH username: vagrant
    default: SSH auth method: private key
==> default: Rsyncing folder: /home/linuxize/Vagrant/my-vagrant-project/ => /vagrant

If the box does not exists locally it will be downloaded. Vagrant also mounts the project directory at /vagrant in the virtual machine, which allows you to work on your project’s files on your host machine.

To ssh into the virtual machine, type:

vagrant ssh

When you are done with your work, to stop the virtual machine, run:

vagrant halt

Use the command below to destroy the virtual machine and all associated resources:

vagrant destroy

If the virtual machine is running, it will be stopped before it is removed.

Conclusion

We have shown install Vagrant on CentOS 8 and how to create a basic development environment.

For more information about Vagrant visit the official Vagrant documentation page.

If you have any questions, please leave a comment below.