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.
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.
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"
I go into my Vagrantfile, using "code ." This will open up the file in VScode.
I will go to the vagrant cloud(https://vagrantfile-generator.vercel.app/), choose my desired image, and copy the code.
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.
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"
I run a "vagrant validate" to validate that the Vagrantfile is ok.
Now we can run "vagrant up" to spin up the 3 VMs.
When it's done we can confirm from VirtualBox GUI and from running "vagrant global-status" to see running machines.
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
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.
Top comments (0)