6 steps to set up your lab with Vagrant and VirtualBox

Recently, I wrote a post about how to install a Linux distribution. You can find the post here: 7 easy steps to install Ubuntu on a VM.

Today, I would like to share with you a different approach in setting up your lab environment with Vagrant and VirtualBox. Here you will mostly use the command line (CLI) and not the graphical user interface (GUI).

1. Install the virtualization software

The first step is to install the virtualization software. You can choose from different options like VirtualBox, VMware.

In my setup, I will use VirtualBox. To install VirtualBox in Ubuntu 22.04, run the below command.

# Install VirtualBox


wget -O- https://www.virtualbox.org/download/oracle_vbox_2016.asc | gpg --dearmor | sudo tee /usr/share/keyrings/oracle-virtualbox-2016.gpg
echo "deb [signed-by=/usr/share/keyrings/oracle-virtualbox-2016.gpg] https://download.virtualbox.org/virtualbox/debian $(lsb_release -cs) contrib" | sudo tee /etc/apt/sources.list.d/virtualbox.list
sudo apt update && sudo apt install virtualbox-6.1
Install VirtualBox
Install VirtualBox

Check if the installation was successful.

vboxmanage -v

Check the VirtualBox version
Check the VirtualBox version

2. Install Vagrant

The second step is to install Vagrant. Vagrant is an open-source software which helps you to easily deploy and manage VMs from the command line.

To install Vagrant in Ubuntu 22.04, run the below command.

# Install Vagrant

wget -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor | sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install vagrant
Install Vagrant
Install Vagrant

Check if the installation was successful.

vagrant -v

Check the Vagrant version
Check the Vagrant version

3. Create a new directory and the Vagrantfile file

The third step is to create a new directory which will be used by Vagrant. In this directory, we need to create a new text file with the name Vagrantfile. In this file you will define the details for the virtual machine (VM).

mkdir lab
cd lab/
Create the working directory
Create the working directory

Define the details for the new VM in the Vagrantfile file.

cat >> Vagrantfile <<EOF
# -*- mode: ruby -*-
 # vi: set ft=ruby :
 Vagrant.configure("2") do |config|
   config.vm.define "ubuntu" do |ubuntu|
     ubuntu.vm.box = "ubuntu/jammy64"
     ubuntu.vm.network "private_network", ip: "192.168.59.10"
     ubuntu.vm.hostname =  "ubuntu"
     ubuntu.vm.provider "virtualbox" do |vb|
       vb.memory = "1024"
     end
   end
 end
 
Specify the VM details
Specify the VM details

4. Start the VM

The fourth step is to start the VM. For this you need to run the following command.

vagrant up
Start the VM
Start the VM

When you run the above command for the first time, it will take some time until it finishes. The box is downloaded from HashiCorp Vagrant Cloud. Depending on your Internet speed, it may take between 5 to 10 minutes, so be patient.

5. Connect (SSH) to the VM

After the VM is up and running, you can connect to it with SSH.

You need to run a command similar to the below one:

vagrant ssh ubuntu
Connect to the VM with SSH
Connect to the VM with SSH

6. Discover your Linux box

The sixth and final step is to discover your new Linux distribution.

cat /etc/os-release
whoami
Discover your Linux VM
Discover your Linux VM

7. Some useful vagrant commands that you can use

To check the status of your VM, use the following command:

vagrant status
Vagrant status
Vagrant status

To pause your VM, use the following command:

vagrant suspend
Vagrant Suspend
Vagrant Suspend
Vagrant Status 2
Vagrant Status 2

To stop the VM, use the following command:

vagrant halt
Vagrant halt & vagrant status
Vagrant halt & vagrant status

To delete the VM, use the following command:

vagrant destroy
Vagrant destroy & vagrant status
Vagrant destroy & vagrant status

If you want to create 2 or more VMs, you can specify their details in the same Vagrantfile file.

Multiple VMs added to the Vagrantfile file
Multiple VMs added to the Vagrantfile file

I hope you find this post helpful.

If you struggle to set up your lab, please share your issue in the comments and I will try to help you.

Processing…
Success! You're on the list.

Leave a Reply