Setting up VMs with Vagrant & installing basic tools on it.
Published: 2024-04-16 20:41:34

Vagrant by Hashicorp in simple terms is a tool that helps manage the lifecycle of virtual machines. It helps us to manage and build development environments. 

As a beginner, you may wonder why I need a tool to help me set up my development environment and machines. Think of the process of creating one machine on your Virtualbox or VMware. Imagine doing that 10 times for 10 machines you want to quickly set up. Simply put, it can be a turnoff.

Well, here comes Vagrant to save the day. You can quickly set up those 10 machines in a small matter of time by using a Vagrantfile. A vagrant file is a set of code written in Ruby containing specific instructions like the Box type(Type of machine version e.g Ubuntu, CentOS, etc), provider (by default Virtualbox, VM Ware, Hyper-V), provisioning, Networking, Folders.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wtfbe00761q5wzqfc60s.png)
A not-so-complex Vagrant Architecture showing the Box and providers as main blocks.

_**PRE-REQUISITES**
Ensure you have installed Vagrant on your local PC and also have box images available.
An IDE such as VSCODE_

I will be setting up 3 VMs using Vagrant and installing basic tools such as nginx & python on it.

I have downloaded and installed Vagrant on my local PC. I have also added box images on my local PC to make it readily available.

To reconfirm, I'll do a **"vagrant box list**" which displays the current box images I have downloaded.


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

Next, I need to create a Vagrantfile. I can do so by creating a directory using the **"mkdir"** command. I go on to create a Vagrantfile by using **"touch Vagrantfile"**


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

I go into my Vagrantfile, using **"code ."** This will open up the file in VScode.


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

I will go to the vagrant cloud(https://vagrantfile-generator.vercel.app/), choose my desired image, and copy the code.


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


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

Doing a **"vagrant up"** just like this will create 3 VMs for us when we run it. Let's install some basic tools such as apache2 and Python3 to spice it up. To do this, we go to the provision section of the vagrant website where we can find other cool stuff.


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

I will make use of inline scripts within our code. I'll simply copy and modify this code by including **"sudo apt update", "sudo apt install nginx -y" & "sudo apt install python3 -y"**


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



I run a "vagrant validate" to validate that the Vagrantfile is ok.


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

Now we can run **"vagrant up"** to spin up the 3 VMs.


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

When it's done we can confirm from VirtualBox GUI and from running **"vagrant global-status"** to see running machines.

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


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

I can also SSH to each server using **"vagrant ssh _SERVER-NAME_"** to confirm if nginx & python3 were installed successfully. I'll do **"python3 --version" & "dpkg -l | grep nginx"** to confirm Python and nginx were installed successfully

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

## CONCLUSION
In this brief article, we explored a means of using Vagrant to create Virtual Machines and installation of some basic tools. This should relatively serve as a building block in the use of vagrant as a tool for managing & builiding development environments.