Introducing Sandbox Tools: VirtualBox & Vagrant
Published: 2024-01-17 10:48:51

_**It’s common for IT professionals to come across new problems that require testing to find a solution. In these cases, it’s important to determine whether there is a staging or sandbox environment available (Cloud or On-Premise), or if you can test locally using virtual machines. In this article, I’ll give you an overview of two very important tools for creating virtual environments.**_
 

VirtualBox and Vagrant are two very useful tools for creating virtual environments. VirtualBox is a Hypervisor that allows you to create virtual machines, while Vagrant is a tool that allows you to automate the creation and configuration of virtual machines.

Vagrant is a great option for local testing because it’s easy to use and can be used to create a variety of virtual environments. Additionally, Vagrant makes it easy to delete and recreate virtual machines, which can help test new features or troubleshoot problems.


Before we dive into the main theme, I would like to define some basic concepts:


**Hypervisor** – According to the definition of VMware: “ A hypervisor, also known as a virtual machine monitor or VMM, is software that creates and runs virtual machines (VMs). A hypervisor allows one host computer to support multiple guest VMs by virtually sharing its resources, such as memory and processing. “

**Cloud** – According to the definition of IBM: “ Cloud computing is on-demand access, via the internet, to computing resources—applications, servers (physical servers and virtual servers), data storage, development tools, networking capabilities, and more—hosted at a remote data center managed by a cloud services provider (or CSP). ”

**On-Premise** – According to the definition of HPE: “ On-prem refers to private data centers that companies house in their own facilities and maintain themselves. On-prem infrastructure can be used to run private clouds, in which computing resources are virtualized in much the same way as those of public clouds (however, private clouds can also be run on leased third-party hardware). ”

**YAML** – According to the definition of RedHat: “ YAML is a human-readable data serialization language that is often used for writing configuration files. Depending on whom you ask, YAML stands for yet another markup language or YAML ain’t markup language (a recursive acronym), which emphasizes that YAML is for data, not documents. ”

**Vagrant Plugins** – According to the definition of Hashicorp: “ Vagrant comes with many great features out of the box to get your environments up and running. Sometimes, however, you want to change the way Vagrant does something or add additional functionality to Vagrant. This can be done via Vagrant plugins. ”

 

 

## Virtual Machines
 

In many cases, it is more agile and secure to use local virtual machines to perform initial tests and validate whether it is the best way forward. (I think it is better to start this way before requesting a testing environment when necessary.)

According to the definition of VMware: 

> 
A Virtual Machine (VM) is a compute resource that uses software instead of a physical computer to run programs and deploy apps. One or more virtual “guest” machines run on a physical “host” machine. Each virtual machine runs its own operating system and functions separately from the other VMs, even when they are all running on the same host. ”

The illustration below shows a Virtual Machine being defined as a Hypervisor:


![Virtual Machines](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7txm5edvmckfdauap9z2.png) 


_Image taken from: techtarget.com/searchitoperations/definition/virtual-machine-VM_
  
 

## Oracle VM VirtualBox
 

There are a few tools (Hypervisors) that allow you to create and manage virtual machines, and the one I use most often is VirtualBox.

According to the definition of Oracle: “ Oracle VM VirtualBox, the world’s most popular open source, cross-platform, virtualization software, enables developers to deliver code faster by running multiple operating systems on a single device. IT teams and solution providers use VirtualBox to reduce operational costs and shorten the time needed to securely deploy applications on-premises and to the cloud. ”

 


![Oracle VM Virtual BoxManager](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kbp31iqv9xa10odxf4yb.png)


_Image taken from: oracle.com/a/ocom/docs/oracle-vm-virtualbox-ds-1655169.pdf_
 
This is supported by Oracle and is compatible with ​Windows, macOS, and major Linux distributions (the installation process is simple and quick on all of the platforms mentioned). 

For further information on how to install VirtualBox, check here.

 

 

## Vagrant
 

Now let’s talk about the tool that streamlines the entire process of creating and managing virtual machines (and is the focus of this article), Vagrant.

According to the definition of Hashicorp:  

> 
Vagrant is the command line utility for managing the lifecycle of virtual machines. Isolate dependencies and their configuration within a single disposable and consistent environment. ”

 
![Streamlines process of creating and managing virtual machines](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/na8e74ue43khpeua1pco.png)


_Image taken from: developer.hashicorp.com/vagrant_
 
This is supported by Hashicorp and also has compatibility with ​Windows, macOS, and major Linux distributions. 

For further information on how to install Vagrant, check here.

PS: Remember that it is important to have already installed a Hypervisor (e.g. VirtualBox), before proceeding with the installation of Vagrant. 

## Hands-On

Before we move on, I recommend installing a few Vagrant plugins that I use to expand its functionality during the creation and management of virtual machines.
Below I will give you a brief description of each plugin and the commands to install them:

**vagrant-vboxmanage** – Vagrant plugin that simplifies calling VBoxManage on your Vagrant VM, by automatically injecting the machine uuid argument into the right spot

**vagrant-vbguest** – The VirtualBox Guest Additions are a set of drivers and applications to be deployed on a virtual machine to have better performance and enable features such as folder sharing. While it’s possible to include the Guest Additions directly in the box, not all the boxes you’ll find have it, and even when they do, they can be outdated very quickly.

**vagrant-disksize** – Vagrant plugin to resize VirtualBox disks at creation time

**vagrant-env** – Vagrant plugin to load environment variables from .env into ENV

**vagrant-reload** – Vagrant plugin that adds a reload provisioning step that can be used to do a reload on a VM during provisioning

### Plugin installation:

> 
vagrant plugin install vagrant-vboxmanage
vagrant plugin install vagrant-vbguest
vagrant plugin install vagrant-disksize
vagrant plugin install vagrant-env
vagrant plugin install vagrant-reload

Now, we can finally start creating our first virtual machine with Vagrant. 😉

In this example, we will create a Linux virtual machine with the Ubuntu 22.04.3 LTS distribution.

To ensure that everything works, create a new folder and run the commands below inside this new folder:

> 
vagrant init ubuntu/jammy64
vagrant up

PS: The command vagrant init creates a Vagrantfile file with the basic structure of what is needed for Vagrant to create the virtual machine according to the chosen Linux distribution.

Once the process is complete, run the command below to connect to the newly created virtual machine:

> 
vagrant ssh
 

![Linux](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/se101nhq1445vy2sylww.png)
 
After completing the use of the virtual machine, go back to the same folder created earlier and you can turn it off with the command vagrant halt or delete it with the command vagrant destroy -f 

The Vagrantfile file can be modified for another Linux distribution or to include the creation of more virtual machines as you wish.

Here’s a link to a list of all the templates you can use to create virtual machines.

 

 

## Introducing Sandbox Tools: VirtualBox & Vagrant – Final Thoughts
 

I hope that by now you can understand just how quick and simple the process of creating virtual machines is, regardless of the operating system you are using. If not, I guarantee you that the process will become even easier in my next article, in which we will discuss how to create a Kubernetes cluster using the already-mentioned tools.

Thanks for reading, and see you soon!

Article written by Fabio Kerber and originally published at https://kwan.com/blog/introducing-sandbox-tools-virtualbox-and-vagrant/ on December 5, 2023.