Sunday, November 24, 2024
HomeDevOpsVagrantWhat is vagrant and how to provision first VM

What is vagrant and how to provision first VM

What is Vagrant?

Vagrant is an open-source software product for building and maintaining portable virtual software development environments,[4] e.g. for VirtualBox, Hyper-V, Docker, VMware, and AWS. The core idea behind it lies in the fact that the environment maintenance of virtualizations becomes increasingly difficult in a large software development project. Vagrant simplifies the necessary software configuration management in order to increase development productivity. Vagrant is written in the Ruby language, but its ecosystem supports development in almost all major languages.

Why do you need Vagrant?

To start with, I am sure this must be first question in every beginner’s mind, that Why do I need it? To answer this, Vagrant is a piece of software through which you can create and configure virtual development environments. It is an highly efficient tool for managing virtual machines via CLI. This increases your and your team’s productivity and flexibility.

Here, machines are provisioned on top of any of the virtualization tool, i.e. VirtualBox, VMware, AWS, or of any other provider. Later, industry-standard provisioning tools such as shell scripts, Puppet or Chef, can be used to automatically install and configure software on the machine.

This simply means that you can get a easy to configure, reproducible, and portable work environments as and when you require.

How does it benefit?

Vagrant has benefits to developers, operations engineer, designers and more. Here the key is that Vagrant makes it really easy with the fact that there is no complication or using vim and loads of annoying command line stuff easy to run a development environment. Getting your first development virtual machine ready will take minutes. (Excluding box/image download time).

Once you have finished developing, you can check in your changes, ask your colleague to check them out, and then they run the code on the exact same machine. This even works if they are on any part of world and is platform independent i.e. regardless of whether they are on Windows, Linux or Apple OS X. It is safe to say goodbye to “works on my machine” bugs after using Vagrant.

You’ll surely get to know the benefits better once you start using it. You can read more about benefits here.

Getting Started

Download & install the latest version of Vagrant & Virtual Box, by visiting Vagrant Downloads and VirtualBox Downloads. Usually, the newest version of VirtualBox will work fine, but you should verify the version compatibility with Vagrant, by checking the official Vagrant docs.

Since I am using Ubuntu 16.04 x86_64, I’ll show you how to download & install them on it:

Download them by:

# wget https://releases.hashicorp.com/vagrant/2.0.0/vagrant_2.0.0_x86_64.deb?_ga=2.200063598.111220733.1505802419-941114057.1505802419

# wget http://download.virtualbox.org/virtualbox/5.1.28/virtualbox-5.1_5.1.28-117968~Ubuntu~xenial_amd64.deb

Install them by:

# dpkg -i vagrant_2.0.0_x86_64.deb

# dpkg -i virtualbox-5.1_5.1.28-117968~Ubuntu~xenial_amd64.deb

Here are some terms which you must understand before we run our first vagrant box:

Vagrant Box

A box is basically a package containing a representation of a virtual machine running a specific operating system. To be simpler, it is a base image of any Operating System or Kernel. It may be for a specific Provider.

Providers

The Provider is the piece of software responsible for creating and managing the virtual machines used by Vagrant. The main providers are Virtualbox and VMware, but the default one is VirtualBox, since it’s free and open source.

Provisioners

Provisioner will do some task(s) using the vm instance already provided. The provisioners are used to set up the virtual server, installing all necessary software and executing different tasks. The most used provisioners are: Puppet, Chef and Ansible. Shell Script is also a very common option. You can find more information about vagrant provisioners here.

The Vagrantfile

The basic vagrant configuration is based in one file, the Vagrantfile. It shall be placed in your repository root. In this file, you will define which base box you want – a box is, basically, a package with an operational system to be run in your virtual machine.

Read More: Create Multiple Virtual Machines Using Vagrant

Creation of Instance

Create a test directory where we would be creating our first instance from your command line.

# mkdir -p Vagrant/test
# cd  Vagrant/test
# vagrant init reconz/xenial-16.04
# vagrant up

If all goes well, it will now be running. Below is how the very basic VagrantFile looks like:

If you want to get into this instance, via SSH, use this command:

Vagrant.configure("2") do |config|
  config.vm.define "vagrant" do |vagrant|
  config.vm.box = "reconz/xenial-16.04"
  vagrant.vm.hostname = 'testVM'
  vagrant.vm.box_url = "reconz/xenial-16.04"
  vagrant.vm.network "public_network"
  vagrant.vm.network :forwarded_port, guest: 22, host: 10222, id: "ssh"
end
# vagrant ssh

Your instance is ready, you can access it using “command line” or using virtual box.

RELATED ARTICLES
- Advertisment -

Most Popular

Recent Comments