Vagrant is a computer software for creating and configuring virtual development environments. It can be seen as a wrapper around virtualization software such as VirtualBox, KVM, VMware and around configuration management software such as Ansible, Chef, Salt or Puppet, according to Wikipedia.
However, there are two important terms i.e. Virtualization and Configuration Management.
In other words, Vagrant is a simpler way for Virtualization and Configuration Management. Virtualization and Configuration Management are two big names in devops culture. In this age of devops, a lot of automation is the key to success.
To achieve all this, vagrant is a handy tool for creating and configuring lightweight, reproducible, and portable development environment (Virtual Machines).
Let’s see how to install and create your first virtual development environment using Vagrant:
Step 1: Before we start with Vagrant we need to install Virtual Box as Vagrant uses it to run the virtual machines.
Download the below guide to install Virtual Box:
Step 2: Download Vagrant from the URL below as per your operating system:
Step 3: Once you have the vagrant set up, installing it is as simple as installing VLC media player.
Step 4: Once you have vagrant on your machine, open the terminal (Linux or Mac) or command prompt (Windows)
Step 5: Create a separate directory for vagrant :
$ mkdir vagrant_edureka $ cd vagrant_edureka
Step 6: Initialize a new VagrantFile. A Vagrantfile describes the type of machine required, and how to configure and provision it.
$ vagrant init
A Vagrant file looks like the one below:
# -*- 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://atlas.hashicorp.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. # config.vm.network "forwarded_port", guest: 80, host: 8080 # 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. # Define a Vagrant Push strategy for pushing to Atlas. Other push strategies # such as FTP and Heroku are also available. See the documentation at # https://docs.vagrantup.com/v2/push/atlas.html for more information. # config.push.define "atlas" do |push| # push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME" # end # Enable provisioning with a shell script. Additional provisioners such as # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the # documentation for more information about their specific syntax and use. # config.vm.provision "shell", inline: <<-SHELL # sudo apt-get update # sudo apt-get install -y apache2 # SHELL end
Step 7: After creating a new Vagrantfile, specifying the box to use for your Vagrant environment is always the first step. Instead of building a virtual machine from scratch, which would be a slow and tedious process, Vagrant uses a base image to quickly clone a virtual machine. These base images are known as boxes in Vagrant.
We are using “chef/centos-6.5” for this blog. You can see the list of boxes here.
Use the command below to install it.
$ vagrant box add chef/centos-6.5
Step 8: Update the Vagrant File as below:
# This defines the version of vagrant Vagrant.configure(2) do |config| # Specifying the box we wish to use config.vm.box = "chef/centos-6.5" # Specifying the provider as VirtualBox and naming the VM one_config.vm.provider "virtualbox" do |v| # The VM will be named as edureka_vm1 v.name = "edureka_vm1" end end
Step 9: Let’s start the edureka_vm1:
$ vagrant up
Congratulations! You have created your first VM using vagrant. Ans now, you must be wondering how to use it. You can access it using ssh.
You can connect the VM using the host and port number below:
Host : 127.0.0.1| Port : 2222
Step 10: Download putty (windows shh client) from here. Run the application and enter the details like below –> Click Open :
Step 11: You need to enter the username and password to log in into the VM. Please use the credentials below:
Username : vagrant | Password : vagrant
login as: vagrant email@example.com's password:
Step 12: Finally, you are ready to use your first VM, created using vagrant. Use it and feel the difference:
login as: vagrant firstname.lastname@example.org's password: Last login: Fri Jan 23 11:00:41 2015 from 10.0.2.2 [vagrant@localhost ~]$
The virtual machines created using vagrant are very light and runs smoothly on a machine with 2 -4 Gb RAM and i3 processor.
Before I end this blog, here is something you can work on. Using this blog you create a single virtual machine, but can we create multiple virtual machines using vagrant? Refer to this blog: 10 Steps To Create Multiple Virtual Machines Using Vagrant
Got a question for us? Please mention it in the comments section and we will get back to you.