DEV Community

Cover image for Setting up VMs with Vagrant & installing basic tools on it.
Bowei
Bowei

Posted on

Setting up VMs with Vagrant & installing basic tools on it.

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

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

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

Image description

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

Image description

Image description

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

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

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

Image description

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

Image description

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

Image description

Image description

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

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)