Vagrant is a command line tool for building and managing virtual development environments. By default, Vagrant prepares the environment on VirtualBox, Hyper-V, and Docker. Support other providers, for example, Libvirt (KVM), VMware and AWS can be enabled through the Vagrant plug-in system.
Vagrant is usually used by developers to build a development environment that matches the production environment.
In this guide, we will explain how to install Vagrant on CentOS 8. We will also show you how to create a development environment.
At the time of writing this article, the latest stable version of Vagrant is 2.2.6. Browse Vagrant download page to see if a new version is available.
To install Vagrant on your CentOS machine, enter the following command as root or another sudo user:
sudo dnf install https://releases.hashicorp.com/vagrant/2.2.6/vagrant_2.2.6_x86_64.rpm
Once the installation is complete, run the following command to verify that Vagrant was successfully installed:
vagrant --version
This command will print the Vagrant version number:
Vagrant 2.2.6
Now assuming that you have installed Vagrant on your CentOS system, let's use VirtualBox to create a development environment, which is the default provider of Vagrant. Make sure you have installed VirtualBox on your CentOS 8 system.
The first step is to create a directory as the project root directory. Create a project directory and switch to it:
mkdir ~/my-vagrant-project
cd ~/my-vagrant-project
The next step is to use vagrant init
to initialize a new Vagrantfile and specify the box you want to use. Vagrantfile is a configuration file that is used to describe how the virtual machine is configured and allocated. It uses Ruby syntax to define configuration files.
The box is a package format used in the Vagrant environment. You can find a series of publicly available Vagrant boxes in Vagrant box catalog
In this example, we use ubuntu/bionic64
. Run the following command to initialize a new Vagrantfile:
vagrant init ubuntu/bionic64
A `Vagrantfile` has been placed inthis 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 Vagrantfile with your text editor and make adjustments according to your needs.
Run the vagrant up
command to create and configure the virtual machine defined in the Vagrantfile.
vagrant up
==> default: Configuring and enabling network interfaces...default: SSH address:192.168.121.24:22default: SSH username: vagrant
default: SSH auth method:private key
==> default: Rsyncing folder:/home/linuxize/Vagrant/my-vagrant-project/=>/vagrant
If the box does not exist locally, it will be downloaded automatically. Vagrant also mounts the project directory to the /vagrant
of the virtual machine, which allows you to continue working on the host.
To enter the virtual machine via ssh, enter:
vagrant ssh
When you are done, stop the virtual machine and run:
vagrant halt
Use the following command to destroy the virtual machine and related resources:
vagrant destroy
If the virtual machine is running, it will be stopped first and then removed.
We have shown you how to install Vagrant on CentOS 8 and how to create a basic development environment.
To learn more about Vagrant, visit Vagrant official documentation page.
Recommended Posts