Vagrant what is it and how it works ???

What exactly is vagrant ?

Vagrant is a tool that makes it easy to build and share virtual environments, inn normal way , it takes time and effort to start a virtual machine with VirtualBox or Hyper-V and build a development environment there. With Vagrant makes the burden somewhat lighter by automating the work. By describing the virtual environment settings in the configuration file called , the tool will automatically set the environment.

Main benefits of introducing Vagrant

  • The same virtual environment can be easily provided to multiple people
  • We can easily build the same virtual environment multiple times
  • We u can easily build an environment with the same settings in different environments.

By sharing a we can easily provide the same virtual environment to multiple people, it is possible to easily provide the same environment to multiple people.

Vagrant can easily build the same virtual environment multiple times, and you can easily destroy the built virtual environment with a single command. After destroying it once, if you need it again, you can rebuild the same environment by using the previously created Vagrantfile. Since you can easily build and destroy a virtual environment, it is also suitable for building a learning environment. We can easily build an environment with the same settings on different host machines. Vagrant also makes it easy to provide the same virtual environment to different host machines.

How to install Vagrant ?

On the official Vagrant website you will find installation files for each system (Windows / Linux / MacOS)

For the correct operation of Vagrant, we will also need Virtualbox or Hyper-V or VMware installed. After installing Vagrant, bring up a command prompt and enter the following command and check if vagrant is installed properly.

C:\Tools\cmder
λ vagrant -v
Vagrant 2.2.14

If the version is output to the command prompt, that means installation is successful.

Vagrant initialize

If we want to initiate a new Vagrantfile first we need create directory and then we can run command

C:\Temp
λ mkdir test_vagrant
C:\Temp
λ cd test_vagrant\
C:\Temp\test_vagrant
λ vagrant init
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

This completes the initialization routine, resulting in the generated default Vagrantfile template

C:\Temp\test_vagrant
λ cat Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.
# Every Vagrant development environment requires a box. You can search for
# boxes at https://vagrantcloud.com/search.
config.vm.box = "base"
# Disable automatic box update checking. If you disable this, then
# boxes will only be checked for updates when the user runs
# `vagrant box outdated`. This is not recommended.
# config.vm.box_check_update = false
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
# NOTE: This will enable public access to the opened port
# config.vm.network "forwarded_port", guest: 80, host: 8080
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine and only allow access
# via 127.0.0.1 to disable public access
# config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"
# Create a private network, which allows host-only access to the machine
# using a specific IP.
# config.vm.network "private_network", ip: "192.168.33.10"
# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
# config.vm.network "public_network"
# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
# config.vm.synced_folder "../data", "/vagrant_data"
# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
# Example for VirtualBox:
#
# config.vm.provider "virtualbox" do |vb|
# # Display the VirtualBox GUI when booting the machine
# vb.gui = true
#
# # Customize the amount of memory on the VM:
# vb.memory = "1024"
# end
#
# View the documentation for the provider you are using for more
# information on available options.
# Enable provisioning with a shell script. Additional provisioners such as
# Ansible, Chef, Docker, Puppet and Salt are also available. Please see the
# documentation for more information about their specific syntax and use.
# config.vm.provision "shell", inline: <<-SHELL
# apt-get update
# apt-get install -y apache2
# SHELL
end
C:\Temp\test_vagrant

Vagrant Add Box

Box this is a package of OS image files and environment settings built with that OS.

This time we will use the box mmichal/ubuntu18_04, inside this box there is the Ubuntu OS image. First we need download this box with following commnad.

C:\Temp\test_vagrant
λ vagrant box add mmichal/ubuntu18_04
==> box: Loading metadata for box 'mmichal/ubuntu18_04'
box: URL: https://vagrantcloud.com/mmichal/ubuntu18_04
==> box: Adding box 'mmichal/ubuntu18_04' (v1.1.20210331) for provider: virtualbox
box: Downloading: https://vagrantcloud.com/mmichal/boxes/ubuntu18_04/versions/1.1.20210331/providers/virtualbox.box
Download redirected to host: vagrantcloud-files-production.s3-accelerate.amazonaws.com
box:
==> box: Successfully added box 'mmichal/ubuntu18_04' (v1.1.20210331) for 'virtualbox'!

Next, we need confirm that the download is completed with the following command.

C:\Temp\test_vagrant
λ vagrant box list
mmichal/ubuntu18_04 (virtualbox, 1.1.20210331)

This completes the procedure for adding a box.

Vagrant Start/ Stop VM

After adding our box, it’s time to start the virtual machine.To do this, edit the previously generated Vagrantfile and replace the line config.vm.box

#BEFORE
config.vm.box = "base"
#AFTER
config.vm.box = "mmichal/ubuntu18_04"

After saving the Vagrantfile, we can to start the virtual machine with following command.

C:\Temp\test_vagrant
λ vagrant up

If the VM starts successfully, the following message will be output to the command prompt.

==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
default: /vagrant => C:/Temp/test_vagrant

Now you can log in to the VM directly from the command prompt by entering the following command. Subsequent operations are the same as on UNIX.

C:\Temp\test_vagrant
λ vagrant ssh
Welcome to Ubuntu 18.04.5 LTS (GNU/Linux 4.15.0-112-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
New release '20.04.2 LTS' available.
Run 'do-release-upgrade' to upgrade to it.
vagrant@vagrant:~$

We can also use Putty

Default login and password is vagrant/vagrant.

To stop the started virtual machine, enter the following command.

C:\Temp\test_vagrant
λ vagrant halt
==> default: Attempting graceful shutdown of VM...

When you no longer need a virtual machine just destroy it with following command.

C:\Temp\test_vagrant
λ vagrant destroy --force
==> default: Destroying VM and associated drives...

Vagrant Shared Directory

By default, Vagrant can synchronize the folder on the host side (the PC on the side where the virtual machine is running) with the directory in the virtual machine in real time. Directory to be synchronized can be changed by writing the following in the Vagrantfile.

In the example below, the folder on the host side and the directory on the virtual machine side have been changed to synchronize.

# config.vm.synced_folder "../data", "/vagrant_data"

Vagrant Provisioning

You can preset the processing you want to perform when you start the virtual machine for the first time. Provisioning can be set by writing the following in the .

The following example runs the update commands and installs the apache2 server

config.vm.provision "shell", inline: <<-SHELL
apt-get update
apt-get install -y apache2
SHELL

It is also possible to execute a shell script on the host PC.

config.vm.provision "shell", path: "scripts/install-docker.sh"

Vagrant Networking

The IP address of the virtual machine built with Vagrant is 127.0.0.1 by default. This is the so-called loopback address. You can assign a static IP address to a virtual machine by writing the following in the Vagrantfile.

config.vm.network "private_network", ip: "192.168.123.123"

If we need setup port forwarding we can use this with following setting writing in the Vagrantfile

config.vm.network "forwarded_port", guest: 80, host: 8080

Vagrant Build Our Box

Virtual machine that was launched in the vagrant is, by making the run, you can simply box the state of the virtual machine that was launched. After executing this command , a box file called package.box will be generated in the folder on the host PC side.

C:\Temp\test_vagrant
λ vagrant package
==> default: Attempting graceful shutdown of VM...
==> default: Clearing any previously set forwarded ports...
==> default: Exporting VM...
==> default: Compressing package to: C:/Temp/test_vagrant/package.box
C:\Temp\test_vagrant
λ ls -l
total 1110800
-rw-r--r-- 1 Admin 197121 1137453266 May 25 20:26 package.box
-rw-r--r-- 1 Admin 197121 3095 May 25 19:55 Vagrantfile
C:\Temp\test_vagrant

If you want to use a custom box name and a custom box file name, execute the command below

C:\Temp\test_vagrant
λ vagrant box add ubuntu_1804 ubuntu_1804.box

If we would like to create boxes automatically, a packer is perfect for this, more information can be found in the link below

Summary

Vagrant is a good and simple tool with which we are able to quickly set up a local development environment. In the link below I include the reference to the official documentation and ready-made images that can be used.

DevOps Consultant. I’m strongly focused on automation, security, and reliability.