
Vagrant Crash Course- Development Environments Made Easy
Vagrant is an open-source software product for building and maintaining portable virtual software development environments; e.g., for VirtualBox, KVM, Hyper-V, Docker containers, VMware, and AWS. It tries to simplify the software configuration management of virtualization in order to increase development productivity.

Vagrant Setup
VT(Virtualization Technology) Enabled in Bios.
Setup Oracle VM VirtualBox.
Vagrant Tool
CLI like Git Bash, CMD prompt...
Note: You can use Chocolatey for auto-deployment
VM Setup with vagrant
Create a project directory at any location.
Navigate to "https://app.vagrantup.com/boxes/search" and choose the relevant box.
Create Vagrantfile in Project Directory.
Create a virtual box using the "Vagrant up" command.
Step-By-Step guide
in the following example, I will create a new Centos7 Virtual box using the vagrant file.
Step 1: Creating a project directory
The project directory for this example is "C:\Vagrant-Training\CentosExample"

Step 2: Open CLI to run Vegrant commands
Open Git Bash and navigate to the project directory:

Step 3: Locating the Vagrant file
Navigate to "https://app.vagrantup.com/boxes/search" and query for "Centos", select package, and copy command:


Step 4: Creating the Vagrant file in project folder


Step 5: Create a new VM
To create a new VM, we will use the "Vagrant up" command

Result:

Step 6: Connect to VM using Vagrant command
To access the VM we will use the "Vagrant ssh" Command (It must run from the same directory!)

Additional commands:
Power OFF VM - vagrant halt
Power On (If VM is already created) - vagrant up
Delete VM - vagrant destroy
Power ON VM - vagrant halt
Reboot VM - vagrant reload
How do you get a list of all the VMs in the repository?
On Git Bash command line run the following command:
vagrant global-status
David@DESKTOP-VIK2BBH MINGW64 /
$ vagrant global-status
id name provider state directory
-------------------------------------------------------------------------
587056c default virtualbox poweroff C:/vagrant-vms/ubunto
7f7943a default virtualbox running C:/vagrant-vms
f462fac default virtualbox running C:/vagrant-vms/centos7
bebdf84 default virtualbox running C:/vagrant-training/centosExample
The above shows information about all known Vagrant environments
on this machine. This data is cached and may not be completely
up-to-date (use "vagrant global-status --prune" to prune invalid
entries). To interact with any of the machines, you can go to that
directory and run Vagrant, or you can use the ID directly with
Vagrant commands from any directory. For example:
"vagrant destroy 1a2b3c4d"
let's update the list using the "prune" attribute:
David@DESKTOP-VIK2BBH MINGW64 /
$ vagrant global-status --prune
id name provider state directory
------------------------------------------------------------------------
bebdf84 default virtualbox running C:/vagrant-training/centosExample
How do you get a status of a specific VM?
To get the status on a specific VM, go to its directory and type "Vagrant Status":
David@DESKTOP-VIK2BBH MINGW64 /c/vagrant-vms/ubunto
$ vagrant status
Current machine states:
default running (virtualbox)
The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.
How can we customize the VM specification by changing the vagrant file?
By editing the Vagrant file in a specific VM directory, we can customize the VM specification.
Note: Ls -a will also display hidden files
David@DESKTOP-VIK2BBH MINGW64 /c/vagrant-vms/ubunto
$ ls
Vagrantfile ubuntu-bionic-18.04-cloudimg-console.log
David@DESKTOP-VIK2BBH MINGW64 /c/vagrant-vms/ubunto
$ ls -a
./ ../ .vagrant/ Vagrantfile ubuntu-bionic-18.04-cloudimg-console.log
We will use VIM tools to edit the file:
David@DESKTOP-VIK2BBH MINGW64 /c/vagrant-vms/ubunto
$ Vim Vagrantfile

We can also use IE to open the configuration file located in the folder:


For example, Customize the amount of memory on the VM:
#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
Change to:
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 = "2000"
end
Note that the VM must be rebooted in order for the changes to take effect.
Vagrant Sync Directories
When you create a VM with vagrant, it will sync the vagrant directory (David@DESKTOP-VIK2BBH MINGW64 /c/vagrant-vms/ubunto with the vagrant directory in the VM. Let's check it out. by creating new files and directories in the vagrant directory.
David@DESKTOP-VIK2BBH MINGW64 /c/vagrant-vms/ubunto
$ mkdir SyncDirectories
David@DESKTOP-VIK2BBH MINGW64 /c/vagrant-vms/ubunto
$ touch SyncFile.txt
David@DESKTOP-VIK2BBH MINGW64 /c/vagrant-vms/ubunto
$ ls
DevopsDirectory/ Vagrantfile
SyncDirectories/ testFile.txt
SyncFile.txt ubuntu-bionic-18.04-cloudimg-console.log
vagrant@ubuntu-bionic:~$ cd /vagrant
vagrant@ubuntu-bionic:/vagrant$ ls
DevopsDirectory Vagrantfile
SyncDirectories testFile.txt
SyncFile.txt ubuntu-bionic-18.04-cloudimg-console.log
vagrant@ubuntu-bionic:/vagrant$
David@DESKTOP-VIK2BBH MINGW64 /c/vagrant-vms/ubunto
$
the similarities you just see in the example above are because the /vagrant/ directory in the VM is the same as the /c/vagrant-vms/ubunto directory (This directory is actually mounted from the /vagrant/ directory in the VM by default).

Here is another example:

The following are the most common scenarios for using Sync folders in Vagrant:
You want to save specific files as a backup in case the VM fails or is accidentally deleted.
It eliminates the need for VIM editor because we can add and modify scripts directly on our machine rather than having to change them in the VM.
Is it possible to change the default sync folder?
The sync folder is set to "Vagrant" by default; if we need to change it, we'll change it in the vagrant configuration file. In this example, I will use a new folder created on my local computer:




To make changes, use the "Vagrant reload" command to restart the machine.

Now let's see it in the VM:

Provisioning in Vagrant
Provisioning in vagrant means that you can run commands or scripts when you use the vagrant Up command. So, instead of doing it manually, you can add preliminary tasks that are executed prior to the start of a VM.
To use the provisioning function, you should open the vagrant file and go to line 63:

To enable provisioning, remove # in line 67 and add your code here:

Provisioning on a new Machine
Now when we create a new VM using the Vagrant Up command the creation process will include our new shell commands:


Using an existing machine for provisioning
We can also use provisioning on an existing virtual machine; in that case, you'll follow the same steps for changing the vagrant file, but the command will now be changed:
$vagrant reload --provision