Vagrant: The Best VM Tool You’re Not Using
Tags: vagrant, virtualmachine, beginners, tutorial
Published: 2022-12-05 08:34:07

This article was originally posted on [Hackmamba](https://hackmamba.io/blog/2022/12/vagrant-the-best-virtual-machine-tool-you-re-not-using/).

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](https://en.wikipedia.org/wiki/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](https://www.thewindowsclub.com/disable-hardware-virtualization-in-windows-10#:~:text=ON%20the%20System.-,Press%20F2%20key%20at%20startup%20BIOS%20Setup.,changes%20and%20Reboot%20into%20Windows.)
- 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](https://git-scm.com/downloads)
- A virtualization product like [VirtualBox](https://www.virtualbox.org/), install VirtualBox from [here](https://www.virtualbox.org/wiki/Downloads)

## What is Vagrant?

[Vagrant](https://www.vagrantup.com/intro) 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](https://www.vagrantup.com/downloads). We choose and download the appropriate package for our operating system.

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

```bash
    vagrant -v
```
The command above prints the version of the Vagrant we have installed and then exits.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9j1muxo1czurzxkokgfc.png)

## Setting up a Vagrant Project
By running this terminal command, we create a directory that will house our vagrant project.

```bash
    mkdir vagrant_project
```
Next, we change the directory to our newly created directory.

```bash
    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.](https://app.vagrantup.com/boxes/search) 

In this article, we will download the [**geerlingguy/centos7**](https://app.vagrantup.com/geerlingguy/boxes/centos7) virtual box. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aw9qgailq9ekqs5miwf2.png)

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

```bash
    vagrant init geerlingguy/centos7
```
We should see this in our terminal.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/556pz281jyf5fhdt6hw3.png)

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

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gpmb8m5sde0jhk1leggs.png)

**Bringing up a virtual machine**
We run this terminal command to create a virtual machine from our newly created vagrant file.
```bash
    vagrant up
```
We should see this in our terminal.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rhyclpzr79a67fed14n4.png)

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. 

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cykku5pr6fnrxuszth2f.png)


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

```bash
    exit
    vagrant halt 
```
We should see this in our terminal.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3ekbd0rjepvl98cvm1zg.png)

**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.

```bash
    vagrant destroy
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/41x3nrrdxykzzfvtfx1n.png)

## 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
- [Introduction | Vagrant by HashiCorp](https://www.vagrantup.com/intro)
- [What is a virtual machine (VM)?](https://azure.microsoft.com/en-us/resources/cloud-computing-dictionary/what-is-a-virtual-machine/#overview)