Introduction
Vagrant is an open-source tool used to create, configure, and manage virtual machines through simple commands. It works with virtualization providers such as VirtualBox, VMware, Hyper-V, and Docker.
Vagrant makes it easier to create repeatable development environments, reducing the time required to manually configure virtual machines.
In this guide, we will cover how to install Vagrant with VirtualBox and create, access, stop, and delete a virtual machine.
Why Use Vagrant?
Vagrant simplifies the process of creating and managing virtual development environments.
Instead of manually installing and configuring a virtual machine every time, you can use a Vagrantfile and a few Vagrant commands to create the environment.
Some common benefits include:
- Quick virtual machine setup
- Repeatable development environments
- Easy configuration management
- Simple VM lifecycle management
- Easy sharing and reuse of development environments
Prerequisites
Before starting, make sure:
- VirtualBox is installed on your system.
- Vagrant is installed.
- Your system has sufficient disk space and memory for the virtual machine.
- Basic knowledge of virtualization is helpful.
Implementation
Step 1: Install VirtualBox
Download VirtualBox from:
https://www.virtualbox.org/wiki/Downloads
Select the appropriate platform package for your operating system and install VirtualBox.
Step 2: Install Vagrant
Download Vagrant from:
https://www.vagrantup.com/downloads.html
Select the package appropriate for your operating system and complete the installation.
Managing Virtual Machines
Step 3: Create Your First Vagrant VM
Create a directory for the Vagrant project:
mkdir centos-vm && cd centos-vm
Add a CentOS Vagrant box:
vagrant box add centos https://github.com/CommanderK5/packer-centos-template/releases/download/0.6.7/vagrant-centos-6.7.box
Example output:
==> box: Adding box 'centos' (v0) for provider:
box: Downloading: https://github.com/CommanderK5/packer-centos-template/releases/download/0.6.7/vagrant-centos-6.7.box
==> box: Successfully added box 'centos' (v0) for 'virtualbox'!
Initialize the Vagrant environment:
vagrant init centos
This creates a Vagrantfile in the current directory.
Example output:
A `Vagrantfile` has been placed in this directory. You are now ready to `vagrant up` your first virtual environment!
Start the virtual machine:
vagrant up
Vagrant will create and start the VM using VirtualBox.
Note: The CentOS 6.7 box used in the original example is an old image. For new environments, use a current and trusted Vagrant box that matches your project requirements.
You can find other Vagrant boxes at:
Step 4: Access the Virtual Machine
Use the vagrant ssh command to connect to the VM:
vagrant ssh
Example:
Last login: Mon Mar 20 13:49:26 2017 from 10.0.2.2 [vagrant@localhost ~]$ cat /etc/redhat-release CentOS release 6.7 (Final) [vagrant@localhost ~]$
Step 5: Shut Down the Virtual Machine
To gracefully stop the VM:
vagrant halt
Example output:
==> default: Attempting graceful shutdown of VM...
Step 6: Delete the Virtual Machine
To destroy the VM and its associated resources:
vagrant destroy
Vagrant asks for confirmation before deleting the VM:
default: Are you sure you want to destroy the 'default' VM? [y/N] y ==> default: Destroying VM and associated drives...
Conclusion
Vagrant simplifies virtual machine management by allowing you to create and control development environments using a small set of commands.
With VirtualBox and Vagrant, you can quickly create a VM, access it using SSH, shut it down when it is not required, and remove it when you are finished. This makes Vagrant useful for repeatable development and testing environments.
FAQs
1. What is Vagrant used for?
Vagrant is used to create and manage reproducible virtual development environments using configuration files and simple commands.
2. Does Vagrant require VirtualBox?
No. Vagrant can work with several providers, including VirtualBox, VMware, Hyper-V, and others. VirtualBox is one of the commonly used providers.
3. Which command starts a Vagrant VM?
Use:
vagrant up
4. How do I access a Vagrant VM?
Use:
vagrant ssh
5. How do I remove a Vagrant VM?
Use:
vagrant destroy
Related Articles
Install Nginx, PHP-FPM, and MariaDB on CentOS 7
Learn how to install and configure Nginx, PHP-FPM, and MariaDB on a CentOS 7 server.
https://pheonixsolutions.com/blog/install-nginx-php-fpm-mariadb-on-centos-7/
DKIM Setup for Postfix Mail Server on CentOS 6.7
Learn how to configure DKIM for a Postfix mail server running on CentOS 6.7.
https://pheonixsolutions.com/blog/dkim-setup-postfix-mailserver-centos-6-7/
Talk to our experts
Looking for the right technology solution for your business? Our team of experts can help you with development, cloud, DevOps, design, and a wide range of other technology needs. Get in touch with our team here.