# How to create a VirtualBox machine using Vagrant on Ubuntu 22.04

[Vagrant](https://developer.hashicorp.com/vagrant/intro) is a tool for building and managing virtual machine environments in a single workflow. With an easy-to-use workflow and focus on automation, Vagrant lowers development environment setup time, increases production parity, and makes the "works on my machine" excuse a relic of the past.

[Vagrant](https://developer.hashicorp.com/vagrant/tutorials/getting-started/getting-started-index#initialize-vagrant:~:text=Copy-,Next%20steps,-Vagrant%20allows%20you) allows you to work on any project, install every dependency that the project needs, and set up any networking or synced folders, so you can continue working from the comfort of your machine.

## Prerequisites:

* [VirtualBox installed](https://hashnode.com/post/clquxb1id000808kvcyicd00u).
    
* [Vagrant installed](https://karlygash-yakiyayeva.dev/how-to-install-vagrant-on-ubuntu-2204)
    
* [Ubuntu 22.04](https://releases.ubuntu.com/jammy/)
    

## Hands-on Lab Overview

This hands-on lab focuses on the creation of a development environment with Vagrant which uses [CentOS/7](https://app.vagrantup.com/centos/boxes/7) as a base image.

## Hands-on Lab

1. To initialize a new Vagrant environment by creating a new Vagrantfile, run the command:
    
    ```bash
    vagrant init
    ```
    
2. Configure the project to use "CentOS 7" as a base image. Open the newly created Vagrantfile and edit the line:
    
    ```bash
    config.vm.box = "centos/7"
    ```
    
    <div data-node-type="callout">
    <div data-node-type="callout-emoji">💡</div>
    <div data-node-type="callout-text">Vagrant uses a base image to quickly clone a virtual machine, instead of building a virtual machine from scratch, which would be a slow and tedious process. Search for the required box at <a target="_blank" rel="noopener noreferrer nofollow" href="https://app.vagrantup.com/boxes/search" style="pointer-events: none">https://app.vagrantup.com/boxes/search</a>.</div>
    </div>
    
3. To bring up the virtual machine running CentOS 7, enter the command:
    
    ```bash
    vagrant up
    ```
    
    ![](https://i.imgur.com/XHHGK2J.png align="center")
    
    With just one line of configuration and one command in the terminal, a fully functional, SSH-accessible virtual machine was brought up.
    
4. Vagrant runs the virtual machine without a UI, therefore it is not possible to see anything. To prove that it is running, SSH into the machine.
    
    ```bash
    vagrant ssh
    ```
    
    This command will drop into a full-fledged SSH session.
    
    ![](https://i.imgur.com/Rg6l3Eh.png align="center")
    
5. Terminate the SSH session with `CTRL+D`, or by logging out.
    
    ![](https://i.imgur.com/NSnLYJv.png align="center")
    
6. Once you're back on your host machine, stop the machine that Vagrant is managing and remove all the resources (disk space and system resources) created during the machine-creation process. When prompted, confirm with a `yes`.
    
    ```bash
    vagrant destroy
    ```
    
    ![](https://i.imgur.com/oDmcPmH.png align="center")
    
    This command does not remove the downloaded box file.
    
7. To list box files:
    
    ```bash
    vagrant box list
    ```
    
    ![](https://i.imgur.com/6tBO54k.png align="center")
    
8. To remove the box file, run the `remove` subcommand, providing the name of your box:
    
    ```bash
    vagrant box remove centos/7
    ```
    

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">Commit Vagrantfile to version control, thus every person on the project will benefit from Vagrant without any upfront configuration work.</div>
</div>

## References:

1. [Initialize a project directory](https://developer.hashicorp.com/vagrant/tutorials/getting-started/getting-started-project-setup)
    
2. [Boot an environment](https://developer.hashicorp.com/vagrant/tutorials/getting-started/getting-started-up)
    
3. [Updated CentOS Vagrant Images Available (v2004.01)](https://blog.centos.org/2020/05/updated-centos-vagrant-images-available-v2004-01/)
