The Talent500 Blog
How Vagrant Can Help You Ace At Virtual Machine Management 1

How Vagrant Can Help You Ace At Virtual Machine Management

It is the year 2023, and DevOps has become mainstream. Today DevOps engineers have access to a lot of tools, and the number keeps going up. While they contributed immensely to the scalability and quality of DevOps, they have also boosted developers’ productivity over the years. Vagrant is one such DevOps tool that helps developers build and distribute development environments at scale.

On top of that, Vagrant is open-source, thereby allowing the smaller companies and even students to harness the same libraries and approaches as larger companies like Shopify, Accenture, Coursera, and Airbnb that use it.

Since we have noticed that an increasing number of DevOps openings on our job portal are showing interest in candidates with proficiency in Vagrant, the DevOps experts at Talent500 have compiled this mini-guide on leveraging Vagrant to ace virtual machine management.

Let’s get started:

What is VagrantHow Vagrant Can Help You Ace At Virtual Machine Management 2

Vagrant falls under the infrastructure as code (IaC) category and it is used to automate the process of building and management of VMs with the help of tools like Docker, VirtualBox, AWS, or VMWare.

These virtual environments i.e. Vagrant boxes can be installed, run, and monitored on your computer/cloud and supports cross-platform interoperability between macOS, Windows, and Linux.

DevOps engineers use virtual environments to replicate production environments for troubleshooting purposes with the help of tools like Vagrant. However, many find Vagrant to be an alternative to Docker or VirtualBox but it is not the case. Docker is a containerization tool while VirtualBox is a virtualization platform and thus, despite seemingly overlapping functionalities, Vagrant is a distinct tool.

How to set up Vagrant in Ubuntu, MacOS, Windows, and AWS?

In this section, we will have a quick overview of setting up Vagrant in Linux, MacOS, and Windows. We will also have a look at creating it in AWS:

Installing Vagrant in Ubuntu 22.10

Step 1: Update the Ubuntu package by running the below command in the terminal window

sudo apt update

Step 2: Install VirtualBox since it requires a hypervisor tool. Use the following command:

sudo apt install virtualbox

Step 3: Download and install the latest version of Vagrant using this code:

wget https://releases.hashicorp.com/vagrant/2.2.18/vagrant_2.2.18_x86_64.deb

.
sudo apt install ./vagrant_2.2.18_x86_64.deb

Step 4: Verify the installation using the below command:

(bash)

vagrant –version

Installing Vagrant on macOS

Step 1: Install VirtualBox from its official website

Step 2: Install Vagrant using the below code:

(bash)

brew install vagrant

Step 3: Create a Vagrant project

(bash)

mkdir my-vagrant-project
cd my-vagrant-project
vagrant init ubuntu/xenial64

Step 4: Start the virtual machine

(bash)

vagrant up

Installing Vagrant in Windows

Prerequisites:

Install VirtualBox from the official website and verify VirtualBox installation

and

vboxmanage –version

Step 1: Install VirtualBox from its official website

Step 2: Run the Vagrant installer

Go to the folder where the Vagrant installer was downloaded and double-click the file to begin the installation process.

Step 3: Choose installation options like the installation directory, components to be installed, and the option to add Vagrant to the system path

Step 4: Confirm the installation and check the version running the following command in a Command Prompt or PowerShell window:

vagrant –version

Installing Vagrant on AWS

Step 1: Install Vagrant from the official website

Step 2: Install the Vagrant AWS plugin

(bash)

vagrant plugin install vagrant-aws

Step 3: Configure the Vagrant AWS plugin

Create a file named ‘Vagrantfile’ in your project directory and add the following code to it:

(lua)

Vagrant.configure(“2”) do |config|
config.vm.provider :aws do |aws, override|
aws.access_key_id = “YOUR_ACCESS_KEY”
aws.secret_access_key = “YOUR_SECRET_KEY”
aws.region = “us-east-1”
aws.instance_type = “t2.micro”
aws.security_groups = [“YOUR_SECURITY_GROUP”]
aws.keypair_name = “YOUR_KEYPAIR_NAME”
override.ssh.username = “ubuntu”
override.ssh.private_key_path = “PATH_TO_YOUR_PRIVATE_KEY”
end

config.vm.define “aws” do |aws|
aws.vm.box = “dummy”
end
end

Note: Replace ‘YOUR_ACCESS_KEY’, ‘YOUR_SECRET_KEY’, ‘YOUR_SECURITY_GROUP’, ‘YOUR_KEYPAIR_NAME’, and ‘PATH_TO_YOUR_PRIVATE_KEY’ with the appropriate values for your AWS account.

Step 4: Launch the AWS instance

(bash)

vagrant up –provider=aws

Launch an Amazon Elastic Compute Cloud (EC2) instance with the given settings using this command. Connecting to the instance is as simple as using the vagrant ssh command.

Also, you must keep in mind that setting up environment variables or a configuration file to store your Amazon credentials may be required for the process. If needed, check the official Vagrant Amazon plugin documentation.

How to create a virtual machine using Vagrant in Ubuntu, MacOS, Windows, and AWS?

Let us go through the steps to create a virtual machine using Vagrant:

Creating a virtual machine using Vagrant in Ubuntu

Step 1: Create a new directory for your project

(bash)

mkdir my-vagrant-project
cd my-vagrant-project

Step 2: Initialize a new Vagrant environment

(bash)

vagrant init ubuntu/xenial64

Step 3: Start the virtual machine

(bash)

vagrant up

Step 4: Connect to the virtual machine

(bash)

vagrant ssh

Creating a virtual machine using Vagrant in macOS

Step 1: Create a new directory for your project using this code:

(bash)

mkdir my-vagrant-project
cd my-vagrant-project

Step 2: Initialize a new Vagrant environment

(bash)

vagrant init ubuntu/xenial64

Step 3: Start the virtual machine

(bash)

vagrant up

Step 4: Connect to the virtual machine

(bash)

vagrant ssh

Creating a virtual machine using Vagrant in Windows

Step 1: Create a new directory for your project
(batch)

mkdir my-vagrant-project
cd my-vagrant-project

Step 2: Initialize a new Vagrant environment

(batch)

vagrant init ubuntu/xenial64

Step 3: Start the virtual machine

(batch)

vagrant up

Step 4: Connect to the virtual machine

(batch)

vagrant ssh

Note: In addition to enabling Virtualization in the BIOS options, you may need to use the command prompt as an administrator.

Creating a virtual machine using Vagrant in AWS

After you have installed Vagrant, follow these steps:

Step 1: Create a file called Vagrantfile in a new directory and add the following contents:

(lua)

Vagrant.configure(“2”) do |config|
config.vm.provider :aws do |aws, override|
aws.access_key_id = ENV[‘AWS_ACCESS_KEY_ID’]
aws.secret_access_key = ENV[‘AWS_SECRET_ACCESS_KEY’]
aws.keypair_name = “your_keypair_name”
aws.security_groups = [“your_security_group”]
aws.region = “your_aws_region”
aws.instance_type = “your_instance_type”
aws.ami = “your_ami_id”
override.ssh.username = “your_username”
override.ssh.private_key_path = “path/to/your/private_key”
end
end

Make sure to replace the values in quotes with the applicable values.

Step 2: Run the following code in your Vagrantfile to launch the VM:

(CSS)

vagrant up –provider=aws

Step 3: Connect to the VM using SSH by running

vagrant ssh

Benefits of using Vagrant for virtual machine (VM) management

Here, we will have a look at the benefits of using Vagrant for VMs:

Vagrant simplifies the setting up of multi-VM networks as you can launch multiple virtual machines through a single command and a single configuration file. Thus, you can avoid work duplication by using the Vagrantfile to make edits across all VMs.

Using VirtualBox snapshots, you can implement source control to the virtual machine at the cost of extra resources. Provisioning remains the most significant benefit as you can use combinations of various operating systems and distributions without having to spend long hours.

Vagrant helps control multi-machine stacks using tools like Jenkins when doing test runs for continuous integration and integration tests.

Last but not the least, Vagrant ties every tool including Chef, Ansible, or Puppet in the DevOps ecosystem together with just a ‘vagrant up’, thereby minimizing surprises at deployment time.

All in all, it drastically reduces the set-up time for VMs from a couple of days to virtually an hour or two while simplifying the process which would be otherwise too complicated for sophisticated environments.

Wrap Up

Vagrant is an easy-to-learn yet versatile tool that serves day-to-day activities like testing multi-server use cases, and moving code into different stages. It aids DevOps engineers in testing multiple software workflows across different environments and OSes. It also enables them to experiment with new DevOps workflow ideas since it helps separate the software code and the infrastructure.

Looking for new opportunities? Join Talent500 now!

0
Avatar

Neel Vithlani

Add comment