Grab Deal : Flat 30% off on live classes + 2 free self-paced courses! - SCHEDULE CALL

- DevOps Blogs -

What is Vagrant? Vagrant Tutorial Guide for Beginners

What is Vagrant?

A vagrant is a common tool for virtual machines management combined with a simple command line interface. The tool is expected to make the entire process easy without polluting the main installation with tons of configuration files. In the backend, there is one Virtual Box utilized by the Vagrant tool to run virtual machines. A Vagrant box is just another term for virtual machines handled specifically by this utility. Vagrant supports Hyper-V, Docker, Virtual Box, and this tool can manage other machines like Amazon EC2, VMWare, etc., as well.

Vagrant Tutorial for Beginners

In this blog, we would cover Vagrant in detail, along with its installation, features, benefits, virtual boxes, setting up the environment, etc. Learning Vagrant is not rocket science, but you should start from basics. Either you are a fresher or a rocket scientist, learning basics will save your years of frustration, misery, and pain. Also, it helps in building real-world projects quickly that dazzles potential employers.

There are plenty of Companies worldwide who are looking for skilled employees who know how to streamline development processes with VMWare and other cool stuff. Vagrant is easy to learn for programmers who belong to different programming backgrounds like Java, Python, Ruby, PHP, or JavaScript, etc. Before we dive deep, let us first discuss how problematic life was without Vagrant and how it helped to overcome those difficulties.

What led to the creation of Vagrant?

For programmers, especially in the Python domain, “pip” and “virtual” are two popular commands that work perfectly to get all the stuff done. Let us understand these two commands in detail below.

For programmers outside the Python world, “pip” is a package manager in the Python world allows you to install modules and libraries super easily. Basically, it allows you ethically steal the code and get infinite power. 

Pip install django

With this simple command, now you are ready to use Django and create Django apps.

Pip install flask

With this simple command, now you are ready to use Flask and create Flask apps. Yeah, it is that much easy! You must have the clear idea of “pip” command and how it is useful. The best part is that 99% you won’t face any problem in installing libraries or packages. There may be some issues if you are using Windows, so try avoiding Windows operating system in that case. Buy MAC operating system or get a Virtual Machine installed as fast as possible. Luckily, Vagrant can save your $1500 here, if you know how to use it perfectly.

SQL Server Curriculum

“Virtualenv” command allows you to create a virtual environment. For example, Django project does not need to know about the Flask project. SO, you should create a separate virtual environment for both platforms first.

Let us go a little deep now and learn how Vagrant can help in overcoming real-life challenges. Before moving ahead, take a quiz, and evaluate your existing skills now.

  • Get your favorite OS: Getting your favorite OS is the first step when you start working on a project. MAC operating system is more mature than Ubuntu and VMWare. When You use other operating systems, they can slow down your system, and they are clunky too.
  • Learn new things quickly: When you are using Vagrant, there is no environment problem because you are working on virtual machines, and things are automatically managed in the background.
  • Native code generation: The code can run on any machine, and it is a big achievement for developers working with complex projects in a team. In this way, all developers can come together on a single page without any conflicts.
  • Create an impressive resume: Employers love hiring those people who know how to use virtual environments, virtual machines, package managers, or wonderful tools like Vagrant.
  • Accelerate your software journey 10x: You don’t have to get frustrated and won’t give up in between. Just get your stuff done fast and progress in your software journey 10x.

Now you must be convinced enough How Vagrant has made the life of developers easy and more robust. Let us go little technical now and learn to install Vagrant and Virtual box on your favorite operating system. If you are not sure what to learn as a Vagrant user, check the syllabus first, before moving ahead.

Install Vagrant and Virtual Box

  • First of all, download and install the latest version of Vagrant from the link - https://www.vagrantup.com/downloads.html
  • Once you have installed the package, try checking its version by using the following command: $ vagrant –v Vagrant 2.0.0
  • You can see that Vagrant version is printed below, it means you have downloaded and installed the package successfully.

Sometimes, Vagrant packages are already available in the system upstream packages repo. You should not install Vagrant in this manner. Here, updated files are missing, and it generally includes an older version of the tool. The best idea is to go to the download page by accessing the link given earlier and install the latest package that is highly in demand and suitable for today.

In the next section, let us see how to avail virtual boxes:

In Vagrant, standard templates of virtual machines are named as boxes. You may check the public list of vagrant boxes here - https://app.vagrantup.com/boxes/search

VirtualBox ≠VmWare

Vagrant boxes are generally provider-specific, and not all boxes are available for all providers. You should sort out the search by provider name so that you may find the desired box quickly. Once you find the right box, create a directory to store the project. You can use the following command for this purpose.


$ mkdir –p vagrant –projects/tutorial
$ cd vagrant-project/tutorial

Now initialize the new Vagrant environment within the project’s directory by typing the following command: $ vagrant init Ubuntu/xenia164

Here “init” command will create a “Vagrantfile” in the current directory, and the name of the box is given in the end that you want to initialize. Let us start the Vagrant Environment in the next step.

Read: Top 50 Kubernetes Interview Questions and Answers for 2023

Vagrant Commands

To start the Vagrant virtual environment, type: $ vagrant up

This command creates, configures, and starts the virtual machine based on the Vagrant file. When the command is run, it will automatically download the needed Vagrant box from the box repository and performs the provisioning.

To SSH any running Vagrant machine or to access the shell, you may use the following command: $ vagrant ssh

To suspend the development, or to stop the virtual machines, just suspend it with the following command given below: $ vagrant halt

To suspend a virtual machine, you can write: $ Vagrant suspend

If you want to clean up the development environment, you should destroy the machine. Here is the command to stop and destroy the Vagrant machine: $ vagrant destroy

When you need a clear Vagrant environment based on a standard template, just type: $ vagrant up

Accessing Ubuntu Vagrant box is not rocket science, but you should use the following commands here:


Mkdir folder_of_awesomeness
Cd folder_of_awesomeness
Vagrant init bento/Ubuntu-16.04
Vagrant up
Vagrant ssh

These commands will simply enable you accessing shiny new Ubuntu Vagrant box. In a few cases, you may get the “Timeout” error. But don’t panic, you just have to upgrade the Virtual Box in that case. If you are a Windows User, then you should use a different command to SSH:


Protocol: SSH
Hostname: 127.0.0.1
Port 2222

Boom!! You just have entered a completely new and different virtual world. Are you still confused, what is so special about it? If yes, let us take the discussion ahead.

The Infinite power behind the Virtual World

With a virtual screen, you are in a completely different world. As a Windows user, if you think you are on Windows, you are not anymore. So, just saved $1500, isn’t it splendid. Command line is more powerful when compared to User Interfaces, and it allows you to hack the ultimate setups with three simple lines of code. With Vagrant, you may get entire setups with few lines of code.

One more thing cool about the Vagrant is that it helps in stealing another virtual box using Vagrant and finally follow the advanced image recognition without installing all modules and libraries on your system. In this way, Vagrant allows sharing the virtual environment and box with anyone else in the world without installing extra things.

SQL Server Quiz

If you face any problem in getting tools to work on Windows or MAC, then ethically steal boxes and get all the things you want working. Even if you want something more complicated like a Postgres database, then it is super easy with a simple command: sudo apt-get install postgresql

Let us start the tour from Beginning

If you want to brush up the basic commands first before getting deep into the concept, let us break it down here.

To create a directory, use mkdir

To change a directory, use cd

To create a Vagrant file in your directory, type: vagrant init bento/Ubuntu-16.04

To list all the directories, Type “ls.”

To see the content of the file, you should use the following command: cat vagrantfile

Read: DevOps Engineer Role and Responsibilities: Let’s Guide You Here!

To run and install Ubuntu image automatically from the web, you can use the following command: config.vm.box = ”bento/Ubuntu-16.04”

To run the Vagrant file and start the virtual Vagrant environment, use the following command: vagrant up

To SSH the Vagrant box, use the following command: Vagrant ssh

SSH sound boring sometimes, instead you can use Hack or takeover words here. These words are suitable here because it happens when you SSH into something. With a simple command, you take control of another system.

Synced Folders – The Powerful Secret about Vagrant

With Vagrant, you live in a completely fake environment, a virtual box that you may share with anyone. Let us go to it.

You put all the stuff in the /vagrant folder, but it can be seen on the local computer where your Vagrant file is. Not, it is the time to edit your code with the help of an editor on your favorite operating system. Just run the code from the virtual box and SSH to the command line.


Touch /vagrant/new_python.py
Exit
Is

Here, touch is the command to create a file quickly, and new-python is the freshly generated file. Obviously, this command will not work on Windows, but it is good for command line interface only. If you are in a vagrant box, it will work. WOW, you are done again.

With exit command, you can come back to the local computer again. The “ls” command is used to list all files on your local computer. You can use the “dir” command if you are on a Windows command line instead of Linux command line. If you find the file, you can open it on GUI as well and edit the content as required. Save the file, SSH to the box to run the code.

Tip: SSH the device to multiple systems together, open the file, and edit the content into your favorite editor and save it. It will save a lot of time, and you may focus on other things now.

Getting Started with Vagrant

Once you are sure of basic commands in Vagrant and how to install Vagrant and Virtual boxes. This is the time getting started with Vagrant and set up your first project. For this purpose, you need a Vagrant file. The purpose of the Vagrant file is two-fold, as discussed below.

  • It marks the root directory of the project and multiple configuration options that are related to the root directory.
  • It describes the kind of machine and resources you needed to run a project. It also gives you an idea of software and how to access it.

Here are a few commands that will allow you to set up a Vagrant project:


$ mkdir vagrant_getting_started
$cd vagrant_getting_started 
$ vagrant init hashicorp/precise64

It will create a Vagrant file in your directory. Observe the file closely that is full of comments and examples. Don’t be scared if there is something wrong; we will fix it soon. You can also use these commands for an existing project and use a pre-existing directory in that case. The vagrant file is committed to version control for the project. In this way, almost everyone can take benefits from the Vagrant without any upfront work.

Setting up Virtual Boxes in Vagrant

Now, when you know how to set up a project in Vagrant, this is just the right time to set up a virtual box in Vagrant. Vagrant does not use a virtual machine from scratch that could be a tedious and time-consuming process. Vagrant uses a base image to clone a virtual machine which is named as “boxes” in Vagrant. Boxes are used to set up the virtual environment, once the Vagrant file is generated.

Installing Box:

You already know how to install a virtual box as we have discussed in basics already. So, we will not repeat those steps again here.  However, this section is worth reading to know more about virtual boxes. To add a new virtual box to the Vagrant environment, you should use the following command. $vagrannt box add hashicorp/precise64

This command adds a new virtual box with the name specified in the command. Now it can be used by multiple Vagrant environments in future when required. It is easy to add boxes from the local computer or custom URL, etc. Boxes are stored globally so that they can be accessed by different Vagrant when necessary.

 Each user can clone the existing box and use it for their own project. He is not allowed to modify the actual base image. So, the original file will never get affected, and users can modify their cloned copy according to requirements.

Boxes are generally namespace and broken down into parts with the username and the box name using a slash. You can see the above example carefully for the same purpose. Boxes can be specified with URLs or file pathname depends on your knowledge and coding style.

It is a misconception that boxes namespaced with Ubuntu or Hashicorp are managed by the same Company, but it is not true. Boxes are published by third-Company generally, and Ubuntu support team does not have any control over it.

Using Box:

Now, when you have added a box to the Vagrant, it is the time to configure a project and use it as the database. First of all, open the Vagrant file and change its content. You may use the following command for the same purpose.


Vagrant.configure(“2”) do |config|
 Config.vm.box =”hashicorp/precise64”
end

It matches the name you used to add the box above. In this way, Vagrant verifies how to use a Vagrant box. If the box is not added earlier, it will be automatically added and installed when it is run. You may also specify the explicit version of the box with the following command:

Read: What is DevOps Model and What it can do?

vagrant.configure("2") do |config|
config.vm.box ="hashicorp/precise64"
config.vm.box_version = "1.1.0"
end

You may also specify the URL instead of box name, as shown below.


vagrant.configure("2") do |config|
config.vm.box ="hashicorp/precise64"
config.vm.box_url ="https://vagrantcloud.com/hashicorp/precise64"
end

In the next, let us bring up the virtual environment and learn a little bit more about it.

Bring up the Virtual environment

To bring up the Vagrant virtual environment, you can use the following command. $vagrant up

This command creates, configures, and starts the virtual machine based on the Vagrant file. When the command is run, it will automatically download the needed Vagrant box from the box repository and performs the provisioning. To SSH any running Vagrant machine or to access the shell, you may use the following command: $ vagrant ssh

This command will allow you to interrupt another system and do whatever you want. However, it may be tempting, be careful here. Wait a moment and think about what has been done here. With two simple commands, we set up the virtual environment and SSH to other systems as per the requirements. Isn’t it sounds cool? Obviously, Yes! To terminate the SSH session, you should use the following command:


vagrant@precise64:~$ logout
Connection to 127.0.0.1 closed.

Now destroy or remove commands can be used to destroy the virtual machine completely and come back to your local system.

Vagrant Tutorial Guide: Provisioning

Is this possible editing files on your own system and sync them with the virtual machine? Yes, Vagrant can make it possible with the provisioning feature. First SSH in and install a web server, every Vagrant user should work in the same manner. Vagrant has built-in support for automated provisioning. In this way, it automatically downloads software when starting the virtual environment and makes your machine ready to use.

SQL Server training

Installing Apache:

Let us see how to set up Apache for your first Vagrant project. It can be done using a shell script with the following commands:


#!/usr/bin/env bash

apt-get update
apt-get install -y apache2
if ! [-L /var/www ]; then
rm -rf /var/www
ln -fs /vagrant /var/www
fi

Now configure the Vagrant to run this shell script and set up your own machine. The configuration is possible by editing the Vagrant file. Here is the set of commands to follow:


vaggrant.configure ("2") do |config|
config.vm.box = "hashicorp/precise64"
config.vm.provision :shell, path: "bootstrap.sh"
end

Once everything is done, run the Vagrant up command to start the virtual environment and Vagrant will provision it automatically. You should also monitor the output from the shell script appearing to the terminal. If the guest machine is already running, then restart it and skip the important initial steps. Once Vagrant completes running, the web server will be up and starts running. You can see the website from your own browser and verify the provision work by loading a file from SSH from the machine.


$ vagrant ssh
...
vagrant@precise64:~$ wget -qo- 127.0.0.1

You are free to play around commands by creating more file and view the content from your own terminal. In the next section, let us learn networking with Vagrant.

Networking with Vagrant

At this point, we have a web server and modified files that are already synced with the guest machine. However, accessing webpages through the terminal is not that much satisfying. Here, Vagrant is useful where you can use its networking features to give us additional options for accessing the machine from the host machine.

One excellent option here is port forwarding. It allows you to specify ports on a guest machine and support them through a host machine. You may also access ports on your personal machine and forward the all network traffic to a specific port on the guest machine. Doing so with Vagrant is so simple, and it looks like this:


Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/precise64"
config.vm.provision :shell, path: "bootstrap.sh"
config.vm.network :forwarded_port, guest: 80, host:4567
end

Once the machine starts running again, load the IP address in your browser, and you will observe that web page on the virtual machine is automatically set up by the Vagrant. Further, it allows other networking options too like assigning a static IP address, bridging guest machine with the existing network, and more. In the next section, let us learn how to set up shares with Vagrant.

Setting up shares with Vagrant

Now when a web server is running and accessible from different machines, we are working on a fully functional development environment. Vagrant makes it easy to share and collaborate information among different environments. The primary feature of Vagrant is a share that allows sharing Vagrant with anyone around the world. You just need the internet to establish the connection and share the environment. It will generate a URL that can be accessed from anywhere in the world who is connected to the internet. Here is the simple command for the same purpose:


$ vagrant share
...
===> default: Creating Vagrant Share Session...
===> default: HTTP URL: http://b1fb1f3f.ngrok.io
...

Your URL could be different, but it should be accessible and working in a web browser. When you share the folder and refresh the URL, you can see that it is updated. The URL with the route you to the Vagrant environment directly and press the CTRL+C button to end the session and make sure that the environment is not more shared.

Wrap up

It is just wonderful when you control a computer in a completely different part of the world and come right back to your own world with just a simple command. Now you must be sure of the benefits and flexibility of Vagrant. It is certainly a better option than VMware, and it makes your life ten times smoother and productive as a software developer. Vagrant skills certainly put you out of the crowd when you add them to your resume.

Read: What is Puppet? Puppet Tutorial Guide for Beginners


fbicons FaceBook twitterTwitter google+Google+ lingedinLinkedIn pinterest Pinterest emailEmail

     Logo

    JanBask Training

    A dynamic, highly professional, and a global online training course provider committed to propelling the next generation of technology learners with a whole new way of training experience.


  • fb-15
  • twitter-15
  • linkedin-15

Comments

Trending Courses

Cyber Security Course

Cyber Security

  • Introduction to cybersecurity
  • Cryptography and Secure Communication 
  • Cloud Computing Architectural Framework
  • Security Architectures and Models
Cyber Security Course

Upcoming Class

7 days 27 Apr 2024

QA Course

QA

  • Introduction and Software Testing
  • Software Test Life Cycle
  • Automation Testing and API Testing
  • Selenium framework development using Testing
QA Course

Upcoming Class

-0 day 20 Apr 2024

Salesforce Course

Salesforce

  • Salesforce Configuration Introduction
  • Security & Automation Process
  • Sales & Service Cloud
  • Apex Programming, SOQL & SOSL
Salesforce Course

Upcoming Class

7 days 27 Apr 2024

Business Analyst Course

Business Analyst

  • BA & Stakeholders Overview
  • BPMN, Requirement Elicitation
  • BA Tools & Design Documents
  • Enterprise Analysis, Agile & Scrum
Business Analyst Course

Upcoming Class

-0 day 20 Apr 2024

MS SQL Server Course

MS SQL Server

  • Introduction & Database Query
  • Programming, Indexes & System Functions
  • SSIS Package Development Procedures
  • SSRS Report Design
MS SQL Server Course

Upcoming Class

7 days 27 Apr 2024

Data Science Course

Data Science

  • Data Science Introduction
  • Hadoop and Spark Overview
  • Python & Intro to R Programming
  • Machine Learning
Data Science Course

Upcoming Class

6 days 26 Apr 2024

DevOps Course

DevOps

  • Intro to DevOps
  • GIT and Maven
  • Jenkins & Ansible
  • Docker and Cloud Computing
DevOps Course

Upcoming Class

5 days 25 Apr 2024

Hadoop Course

Hadoop

  • Architecture, HDFS & MapReduce
  • Unix Shell & Apache Pig Installation
  • HIVE Installation & User-Defined Functions
  • SQOOP & Hbase Installation
Hadoop Course

Upcoming Class

-0 day 20 Apr 2024

Python Course

Python

  • Features of Python
  • Python Editors and IDEs
  • Data types and Variables
  • Python File Operation
Python Course

Upcoming Class

14 days 04 May 2024

Artificial Intelligence Course

Artificial Intelligence

  • Components of AI
  • Categories of Machine Learning
  • Recurrent Neural Networks
  • Recurrent Neural Networks
Artificial Intelligence Course

Upcoming Class

7 days 27 Apr 2024

Machine Learning Course

Machine Learning

  • Introduction to Machine Learning & Python
  • Machine Learning: Supervised Learning
  • Machine Learning: Unsupervised Learning
Machine Learning Course

Upcoming Class

41 days 31 May 2024

 Tableau Course

Tableau

  • Introduction to Tableau Desktop
  • Data Transformation Methods
  • Configuring tableau server
  • Integration with R & Hadoop
 Tableau Course

Upcoming Class

-0 day 20 Apr 2024

Search Posts

Reset

Receive Latest Materials and Offers on DevOps Course

Interviews