How to create a VirtualBox machine using Vagrant on Ubuntu 22.04

Photo by Kelvin Han on Unsplash

How to create a VirtualBox machine using Vagrant on Ubuntu 22.04

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

Hands-on Lab Overview

This hands-on lab focuses on the creation of a development environment with Vagrant which uses CentOS/7 as a base image.

Hands-on Lab

  1. To initialize a new Vagrant environment by creating a new Vagrantfile, run the command:

     vagrant init
    
  2. Configure the project to use "CentOS 7" as a base image. Open the newly created Vagrantfile and edit the line:

     config.vm.box = "centos/7"
    
    💡
    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 https://app.vagrantup.com/boxes/search.
  3. To bring up the virtual machine running CentOS 7, enter the command:

     vagrant up
    

    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.

     vagrant ssh
    

    This command will drop into a full-fledged SSH session.

  5. Terminate the SSH session with CTRL+D, or by logging out.

  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.

     vagrant destroy
    

    This command does not remove the downloaded box file.

  7. To list box files:

     vagrant box list
    

  8. To remove the box file, run the remove subcommand, providing the name of your box:

     vagrant box remove centos/7
    
💡
Commit Vagrantfile to version control, thus every person on the project will benefit from Vagrant without any upfront configuration work.

References:

  1. Initialize a project directory

  2. Boot an environment

  3. Updated CentOS Vagrant Images Available (v2004.01)