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

> Learn how to efficiently start, stop and manage a virtual machine using Vagrant.

**Source:** https://hackmamba.io/engineering/vagrant-the-best-virtual-machine-tool-you-re-not-using/
**Published:** 4 Dec 2022
**Author:** Asjad Khan
**Category:** Engineering

---
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](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 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](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` 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](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.

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

![](https://paper-attachments.dropboxusercontent.com/s_0D329AD16465F55DA1467777BB2BDF18EFAF312C2767922A6B0EC6558BCE0099_1670355838295_vagrant-v.png)

## Setting up a Vagrant Project

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

```shell
mkdir vagrant_project
```  

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

```shell
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. 


![](https://paper-attachments.dropbox.com/s_0D329AD16465F55DA1467777BB2BDF18EFAF312C2767922A6B0EC6558BCE0099_1662037868264_Screenshot+546.png)


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

```shell
vagrant init geerlingguy/centos7
```

We should see this in our terminal.

![](https://paper-attachments.dropboxusercontent.com/s_0D329AD16465F55DA1467777BB2BDF18EFAF312C2767922A6B0EC6558BCE0099_1670355857030_2.png)


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

```shell
ls
```

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

![](https://paper-attachments.dropboxusercontent.com/s_0D329AD16465F55DA1467777BB2BDF18EFAF312C2767922A6B0EC6558BCE0099_1670355872504_3.png)


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

```shell
vagrant up
```

We should see this in our terminal.

![](https://paper-attachments.dropboxusercontent.com/s_0D329AD16465F55DA1467777BB2BDF18EFAF312C2767922A6B0EC6558BCE0099_1670355888428_4.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. 

```shell
vagrant ssh
```

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

![](https://paper-attachments.dropboxusercontent.com/s_0D329AD16465F55DA1467777BB2BDF18EFAF312C2767922A6B0EC6558BCE0099_1670355902691_5.png)


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

```shell
exit
vagrant halt 
```

We should see this in our terminal.

![](https://paper-attachments.dropboxusercontent.com/s_0D329AD16465F55DA1467777BB2BDF18EFAF312C2767922A6B0EC6558BCE0099_1670355979449_Actual-6.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.

```shell
vagrant destroy
```

![](https://paper-attachments.dropboxusercontent.com/s_0D329AD16465F55DA1467777BB2BDF18EFAF312C2767922A6B0EC6558BCE0099_1670356004046_7.png)

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

- [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)
