Introduction

Vagrant is an open-source tool used to create, configure, and manage virtual development environments using a simple command-line interface. It works with virtualization providers such as VirtualBox, VMware, Hyper-V, and Docker.

Vagrant allows developers and system administrators to create reproducible and portable virtual environments with predefined configurations. Instead of manually installing and configuring a virtual machine each time, users can define the required configuration in a Vagrantfile and recreate the environment whenever required.

In this guide, we will learn how to install Vagrant with VirtualBox and create, access, shut down, and delete a virtual machine.


Prerequisites

Before getting started with Vagrant, make sure you have:

  • A system running Windows, macOS, or Linux.
  • Basic knowledge of virtual machines.
  • Basic knowledge of the command line or terminal.
  • VirtualBox installed on the system.
  • Vagrant installed on the system.
  • Sufficient disk space and system resources for running virtual machines.
  • Internet access to download Vagrant boxes.

Implementation

1. Install VirtualBox

First, download and install VirtualBox for your operating system.

Download VirtualBox

After installation, verify that VirtualBox is available on your system.


2. Install Vagrant

Download and install Vagrant based on your operating system.

Download Vagrant

After installation, verify the installation:

vagrant --version

The command should display the installed Vagrant version.


3. Create a Directory for the VM

Create a separate directory for your Vagrant project:

mkdir centos-vm
cd centos-vm

Keeping each Vagrant environment in its own directory makes it easier to manage the associated Vagrantfile and configuration.


4. Add a Vagrant Box

A Vagrant box is a packaged virtual machine image that Vagrant uses as the base for creating a VM.

You can add a box using:

vagrant box add <box-name>

For example:

vagrant box add centos <box-url>

For new environments, it is recommended to use a currently supported and maintained CentOS-compatible box rather than the old CentOS 6.7 box referenced in the original article.

You can also search available boxes through the official Vagrant Cloud.

Vagrant Cloud


5. Initialize the Vagrant Environment

After adding the box, initialize the Vagrant environment:

vagrant init centos

This creates a file named:

Vagrantfile

The Vagrantfile contains the configuration used by Vagrant to create and manage the virtual machine.


6. Start the Virtual Machine

Start the VM using:

vagrant up

Vagrant reads the configuration from the Vagrantfile, communicates with VirtualBox, and creates and starts the virtual machine.

Once the process is completed, the VM is ready to use.


7. Access the Virtual Machine

You can connect to the running VM using:

vagrant ssh

After connecting, you can execute commands inside the virtual machine just as you would on a normal Linux system.

For example:

cat /etc/os-release

This displays information about the operating system running inside the VM.


8. Check the VM Status

To check whether the Vagrant VM is running:

vagrant status

This displays the current state of the virtual machine.


9. Shut Down the Virtual Machine

When you no longer need the VM temporarily, you can shut it down gracefully:

vagrant halt

This powers off the virtual machine without deleting it.

You can start the same VM again later using:

vagrant up

10. Delete the Virtual Machine

If you no longer need the VM, you can remove it using:

vagrant destroy

Vagrant will ask for confirmation before deleting the virtual machine.

Are you sure you want to destroy the 'default' VM? [y/N]

Enter y to confirm.

Note: vagrant destroy removes the virtual machine and its associated resources. Make sure you do not have important data stored only inside the VM before destroying it.


Common Vagrant Commands

CommandDescription
vagrant initInitializes a Vagrant environment
vagrant upCreates and starts the VM
vagrant sshConnects to the VM through SSH
vagrant statusDisplays the VM status
vagrant haltShuts down the VM
vagrant reloadRestarts the VM and reloads configuration
vagrant suspendSuspends the VM
vagrant resumeResumes a suspended VM
vagrant destroyRemoves the VM
vagrant box listLists installed Vagrant boxes
vagrant box removeRemoves an installed box

Conclusion

Vagrant simplifies the process of creating and managing virtual machines by providing a consistent command-line workflow. When used with VirtualBox, it allows developers and system administrators to quickly create reproducible environments without manually configuring each virtual machine.

With basic commands such as vagrant init, vagrant up, vagrant ssh, vagrant halt, and vagrant destroy, users can efficiently manage the complete lifecycle of a Vagrant-based virtual environment.

Vagrant is particularly useful for development, testing, learning, and creating consistent environments across multiple systems.


FAQs

1. What is Vagrant?

Vagrant is a tool for creating and managing portable and reproducible virtual development environments. It simplifies VM setup by allowing the environment configuration to be defined in a Vagrantfile.

2. What is a Vagrant Box?

A Vagrant box is a packaged base image used to create a virtual machine. It contains the operating system and other basic components required to initialize a Vagrant environment.

3. What is the difference between vagrant halt and vagrant destroy?

vagrant halt shuts down the virtual machine but keeps it available so that it can be started again with vagrant up.

vagrant destroy removes the virtual machine and its associated resources. Therefore, it should be used carefully when the VM contains data that has not been backed up.


Related Articles

  1. How to SSH Remote Server via Terminal – Learn how to connect to a remote Linux server using SSH from the terminal.
    How to SSH Remote Server via Terminal
  2. How to Reset Password on Ubuntu Linux – Learn the steps involved in resetting a forgotten Ubuntu Linux password.
    How to Reset Password on Ubuntu Linux

Leave a Reply