Amarachi Iheanacho

Dec 04 2022

Vagrant: The Best Virtual Machine Tool You’re Not Using

Slightly open computer with a colorful screeen in dark background

Photo by Tianyi Ma on Unsplash

Complex applications require continuous input and update from various teams. With this continuous input, several people with different operating systems and hardware specifications are involved in developing an application. In this case, it is essential to create an isolated computer system non-dependent on a user’s computer hardware.

This isolated computer system duplicate is a virtual machine known as VM.

As Wikipedia describes, a virtual machine is the virtualization or emulation of a computer system. Virtual machines have their CPU, memory, and operating system and behave like utterly separate computers in an application window or desktop.

In this article, we will discuss what Vagrant is and how to use it to create and manage virtual machines.

Prerequisites

To get started with this article, we must have the following:

  • Virtualization Technology (VT) enabled in BIOS. To understand how to enable VT, check out this article
  • A Command line interface (CLI) installed on the computer. For Windows users, it is advisable to use GIT Bash to run the vagrant commands. To download GIT Bash, check out this page
  • A virtualization product like VirtualBox, install VirtualBox from here

What is Vagrant?

Vagrant is an open-source software product for automating the creation and management of virtual machines. With Vagrant, we can specify configurations in a file known as the Vagrantfile and create a virtual machine with this file. The Vagrantfile can recreate the environment with a few commands.

Why Vagrant?

Before Vagrant, developers created virtual machines manually, which proved to be time-consuming, prone to errors, and difficult to replicate.

With Vagrant, we can create virtual machines with a single command, making it easier for developers to quickly set up multiple virtual machines.

Vagrant also allows us to manage virtual machines with a single text file (Vagrantfile). The text file enables the configuration to be put under source control, which makes it easier to revert any changes in the virtual machine.

How to Install Vagrant in our Project

To get started with Vagrant, we install it by visiting the Vagrant download page. We choose and download the appropriate package for our operating system.

To verify that we have successfully installed Vagrant, run this terminal command.

vagrant -v

The command above prints the version of the Vagrant we have installed and then exits.

Setting up a Vagrant Project

By running this terminal command, we create a directory that will house our vagrant project.

mkdir vagrant_project

Next, we changed the directory to our newly created directory.

cd vagrant_project

Creating virtual machines using vagrant box

Vagrant boxes are packaged ready-made vagrant environment. Vagrant boxes can be downloaded and used to create virtual machines. We discover vagrant boxes from the Vagrant cloud.

In this article, we will download the geerlingguy/centos7 virtual box.

Run this terminal command to create a vagrant file with a virtual box.

vagrant init geerlingguy/centos7

We should see this in our terminal.

We run this command to verify that we have installed a vagrant file.

ls

The above command will list all the files and directories in our current directory.

Bringing up a virtual machine

We run this terminal command to create a virtual machine from our newly created vagrant file.

vagrant up

We should see this in our terminal.

Next, we log into our virtual machine by running this terminal command. It is important to run this command in the same directory we created the virtual machine.

vagrant ssh

We can verify that we have logged into our virtual machine by looking at the prompt.

Powering off our virtual machine

To power off our virtual machine, we run these terminal commands.

exit vagrant halt

We should see this in our terminal.

Deleting a virtual machine

Now that we have created and logged into this virtual machine, we might no longer need it. To delete a virtual machine, run this terminal code.

vagrant destroy

Conclusion

This article discussed what virtual machine and vagrant is, why they are essential in creating applications, and, more importantly, how to use vagrant to create, log in, and destroy a virtual machine.

Resources

The resources below might be helpful.

Related Articles
HomeBlogCommunityContact Us
instagram icon
instagram icon