Vagrant: The Best VM Tool You’re Not Using

Amarachi Iheanacho - Dec 5 '22 - - Dev Community

This article was originally posted on Hackmamba.

Complex applications require continuous input and update from various teams. With this continuous input, we have several people with different operating systems and hardware specifications involved in the development of an application. In this case, it is important 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 described by Wikipedia, a virtual machine is the virtualization or emulation of a computer system. Virtual machines have their CPU, memory, and operating system and behave like completely 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 is then available for anyone to 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
Enter fullscreen mode Exit fullscreen mode

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

Image description

Setting up a Vagrant Project

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

    mkdir vagrant_project
Enter fullscreen mode Exit fullscreen mode

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

    cd vagrant_project
Enter fullscreen mode Exit fullscreen mode

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.

Image description

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

    vagrant init geerlingguy/centos7
Enter fullscreen mode Exit fullscreen mode

We should see this in our terminal.

Image description

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

    ls
Enter fullscreen mode Exit fullscreen mode

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

Image description

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

    vagrant up
Enter fullscreen mode Exit fullscreen mode

We should see this in our terminal.

Image description

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
Enter fullscreen mode Exit fullscreen mode

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

Image description

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

    exit
    vagrant halt 
Enter fullscreen mode Exit fullscreen mode

We should see this in our terminal.

Image description

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
Enter fullscreen mode Exit fullscreen mode

Image description

Conclusion

This article discussed what virtual machine and vagrant is, why they are important 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

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .