Install Vagrant on Ubuntu and create first VM

Vagrant is a Open Source software used for creating Virtual Machines in automated way. The software is written in Ruby language. The Vagrant is very useful for increasing the development productivity. We have seen the journey of Vagrant from Virtual box to current cloud providers, truly it is awesome to experience the growth of such a wonderful tool.

Basically Vagrant has two important building blocks

  • Providers: Vagrant supports providers like VirtualBox, Hyper-V, VMWare,Docker and AWS. Even you can develop your own custom providers. Find the Vagrant Providers List here.
  • Provisioners: In provisioners, it supports Ansible, Chef, Puppet, Salt etc. You can find the Vagrant Provisioning List here

Install Vagrant On Ubuntu

Download Vagrant from https://www.vagrantup.com/downloads.html. Ubuntu is debian based OS hence download the deb package.

As per your Operating System arch download the deb package either 32 bit or 64 bit. Our Ubuntu system is 64 bit so we will download 64 bit Vagrant Deb package. (use arch command to know – is your Operating System 32 or 64 bit)

wget command will download the package.

sudo apt -y update
sudo apt -y install wget
wget https://releases.hashicorp.com/vagrant/2.2.9/vagrant_2.2.9_x86_64.deb

And dpkg -i package-name will install the .deb package.

sudo dpkg -i vagrant_2.2.9_x86_64.deb

Once it is installed, you can see the find the absolute path of vagrant command. Here is the example, we have used the command which vagrant.

sharad@linuxworld:~$ which vagrant
/usr/bin/vagrant
sharad@linuxworld:~$ 

Create your first Virtual Machine with Vagrant

Now after installing vagrant, we all are eager to create our first Virtual Machine through vagrant.

VERY IMPORTANT: By Default Vagrant provider is Virtual Box.

Prerequisite

Here in this post, we will use the Virtual Box. So first you should have Virtual Box installed on your system.
Follow our post and install VirtualBox on Ubuntu.

You can also use any other Vagrant Providers for creating Virtual Machines apart of Virtual Box. We have to do some slight changes for using other Vagrant Providers

  • Step 1: Create a directory from where you will manage the vagrant boxes.
    mkdir vagrant_boxes
    
  • Step 2: Create a new directory inside vagrant_boxes directory. The new directory name should represent the Virtual Machine Name. For example, to create CentOS 8 Virtual Machine, we will create directory called CentOS-8.
    mkdir -p vagrant_boxes/CentOS-8
    
  • Step 3: Now change to directory CentOS-8 and do vagrant init for initializing the vagrant base directory.
    This will create a Vagrantfile inside in that directory.
    In case you only run vagrant init, it will create a barebone Vagrantfile.Then you have to update image name value at config.vm.box in Vagrantfile.

    cd vagrant_boxes/CentOS-8
    vagrant init centos/8
    

    Given below is the screenshot depicting Step 1,2 and 3.

    vagrant init
    vagrant init

  • Step 4: Now we will run the command to pull the image from ‘Vagrant Cloud‘ and make the Virtual Machine running in system at a same time.
    In this example,we will pull the CentOS 8 image. Run the below given command.

    vagrant up
    

    This will pull the CentOS 8 image from Vagrant Cloud and once it is downloaded in system, it will run the Virtual Machine.

    vagrant cloud flow
    vagrant cloud flow

    Screenshot: It depicts the output of command ‘vagrant up’.

    vagrant up
    Output: vagrant up

    Open the Virtual Box, you can find the CentOS 8 Virtual Machine is up and running.

    vagrant

  • Step 5: To access the Virtual Machine, your prompt should be in same directory from where vagrant was initialized. Run the below command from there.
    vagrant ssh
    

    Now you can start doing your work. In Linux Virtual Machine, you can do sudo su and do your work as a superuser.

    vagrant ssh

Manage Virtual Machine with Vagrant

  • vagrant init: initializes a new Vagrant environment by creating a Vagrantfile
  • vagrant up: starts and provisions the vagrant environment
  • vagrant halt: stops the vagrant machine
  • vagrant suspend: suspends the machine
  • vagrant destroy: stops and deletes all traces of the vagrant machine

To learn more on vagrant, we will strongly suggest to go through with vagrant --help. It has many parameters. You can also create your own Virtual Machine image and push in ‘Vagrant Cloud’ and this is how many people voluntarily contribute in this awesome project.