When you host an application on a production server, you are one mistake away from causing application crashes. What’s the solution? you can stage the changes and preview it. If everything is going fine before you can make it into the production. That’s the core idea of the Versioning control system, the life savior for the developers and even for testers. The question is, what platform gives you the option to stage such changes and review them? well, Vagrant comes into the picture. How does that work you ask? To answer and get you started, we bring you how to use Vagrant for testing locally.
In today’s post, we will check what Vagrant is, the use cases of Vagrant, how to install Vagrant, how to access Vagrant using Putty, and how to upload files from your device to Vagrant VM. So without further due, let’s dive in.
What is Vagrant?
Vagrant is a Virtual Machine Automation tool. It maintains the lifecycle of VMs from creating the VM automatically to the destruction of the VM. You may have heard of Virtual Box software that lets us install multiple operating systems on our device, which is a type-2 Hypervisor. Vagrant is not a replacement for Hypervisor, it runs on top of Type-2 hypervisors like Virtual Box & VMware. All the required OS installation comes through self-managed Vagrant images and that’s easy to install.
Use cases of vagrant?
1) The first use case is when you want to preview something of a production-grade application and use Vagrant to test it.
2) Vagrant has an immense library of almost all types of OS from its Server grade to generic OS, it gives you so many options to install from.
3) You can manage the VM file through the Vagrant’s file.
4) Suppose you are to host multilevel VM architecture, you can use Vagrant as it has the multi-vagrant file that does it.
5) It is super faster to deploy VMs than doing it manually, which means cutting most of the human error and time.
How to install Vagrant?
To install Vagrant, you would need 3 tools. 1 is a Virtual box that lets you create VMS, 2nd is Vagrant itself which you would use to install VM OS, and the last is Gitbash. Gitbash is important, this makes us navigate through Vagrant VMs and initiate them.
Step 1) Install Virtual Box, Vagrant, and Gitbash SSH client.
Open your favorite browser, and Google Virtual Box, and go to the genuine link of it. Tap to download the Virtual Box and install it on your device. You can also click on the link to the Virtual Box download page here. Same with Vagrant and Gitbash, here is the link for Gitbash and Vagrant download.
How to use the centOS7 box in Vagrant?
In this section, we are going to install centOs7 on Vagrant. RPM-based and Debian-based Linux are widely used in the industry so it is worth having these VMs installed on your Vagrant.
Step 2) Create a Directory and CD into it.
Open your Gitbash application and create a directory where you wish to install Virtual Machine. Mostly, this would be the name of the OS for naming convention. I would name it CentOS7 because we are going to install centOS7 here.
Step 3) Goto Vagrant cloud boxes.
You need to navigate to the Vagrant cloud images to find the OS of your choice. Tap on the link here, and in the search box, search the OS of your choice. I would go with centOS7, and copy the name of the OS.
Step 4) Vagrant Init & up.
Once you have copied the image name, come back to the directory that you have created and paste the init command. Tap enter and then paste the vagrant up command and the Vagrant will do the initialization for you.
Step 5) Final step.
Wait for the Vagrant to do the initialization and when it finishes, it’s time we log into the box using the ssh command. Do vagrant SSH to access the box and, you have the gate opened to test your app or anything that you wish to before you want to make changes to the production app or site. You can also install the LAMP stack, deploy a website, and test your changes.
How to use Ubuntu Box in Vagrant?
As shared earlier, you can install any box you wish to, by following the same procedure shared above. Want an Ubuntu? Go to the Vagrant Cloud site and just search Ubunut with the version to install the one of your choice.
How to access Vagrant using Putty client?
You can access Vagrant boxes on the Putty SSH client if you like Putty over GitBash. To do so, some extra steps are needed, and they are as follows. Ensure you have already downloaded the putty application, or click on the link here to go to the download page.
Step 1) Install putty-plugin.
vagrant plugin install vagrant-multi-putty
You need to go to the directory of the VM you wish to use through the Putty client. Once you are there, apply the plugin command and it should install the plugin for putty.
Step 2) Verify the host details.
This step deals with gathering the host details. To get the host details, up the machine and review the SSH configuration by the command.
3) Pick the hostname and connect.
You need to pick the hostname that you got in step 2 and replace the hostname with yours in the above command. Once you hit enter, it will generate pop-up on the putty application, you click on it and accept the authentication banner to connect.
How to upload files from your Device to a Vagrant VM?
There are times when you want to use the files that you have on your machine, that is Laptop, or Desktop. You can upload these files to the Vagrant VM you just installed. This helps in testing the files of your local machine to the VM, you can also use it to hide things (if you want to).
Final step) Upload.
vagrant upload file-or-directory
To make this successful, you need to place the files of your choice in the same directory where the VM is installed. Once the file is placed, then run the upload path command to upload and you can SSH into the box to make the changes you wish.
Note:- This blog is based on devices that have Windows OS as the base OS.
You don’t need to opt for several machines to experience the multiple operating systems. This is provided by the Virtualization technology, Vagrant uses this tech to optimize and automate the VM installation to the best possible. Lots of Developers and testers have Vagrant installed to test things before going to the release level. This concludes How to use Vagrant for testing locally. What do you think about the Vagrant, do you use any other alternative approach? do let us know in the comments section below. If you need any help or have any suggestions to make, then do reach us via the contact page here. Happy One-Liners Day!