Getting Started With Vagrant

2 minute read

Updated:

Introduction

A vagrant is a tool for building and managing virtual machine environments in a single workflow. With an easy-to-use workflow and focus on automation, Vagrant lowers development environment setup time, increases production parity, and makes the “works on my machine” excuse a relic of the past.

Source: https://www.vagrantup.com/intro

In this blog, I’m showing you how to use Vagrant on Ubuntu with Virtualbox.

Requirement of the lab

Install the Vagrant software on your machine as below:

sudo apt update
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
sudo apt update
sudo apt install vagrant -y

See Vagrant website for other operating systems.

To install Virtualbox see website, the default provider for Vagrant.

What we’ve done here is installed a working Vagrant environment that consists of:

A hypervisor application that can contain virtual machines Vagrant, a tool that makes managing these machines simpler and available in code. It’s important here to note that Vagrant is simply a framework to manage virtual machines, not an application to create and host virtual machines. When using a Vagrant environment, you’ll often encounter errors that are not only related to Vagrant itself, but also related to the hypervisor application.

Getting ready

Verify that Vagrant is installed by typing the command:

╭─$ ~  
╰─➤  vagrant version  
Installed Version: 2.2.19
Latest Version: 2.2.19
 
You're running an up-to-date version of Vagrant!

Installation

I’m installing Centos 7 box for demonstration, but the procedure is the same for any other box. Create a folder/directory on your machine named my-vagrant-lab. Open your terminal and navigate to the specific folder.

Initialize the Vagrant to create a Vagrantfile as below:

╭─$ ~  
╰─➤ vagrant init centos/7
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.
╭─$ ~  
╰─➤ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'centos/7' could not be found. Attempting to find and install...
    default: Box Provider: virtualbox
    default: Box Version: >= 0
==> default: Loading metadata for box 'centos/7'
    default: URL: https://vagrantcloud.com/centos/7
==> default: Adding box 'centos/7' (v2004.01) for provider: virtualbox
    default: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/2004.01/providers/virtualbox.box

truncated

This command might output several results; we’ll note a few important ones:

  • A status message indicating that the default machine is being started with the VirtualBox provider.
  • For the first time, a message will be displayed noting that the box cannot be found.
  • Vagrant will automatically attempt to download a box file.
  • After the box file is downloaded, you should see messages that note machine startup, port forwarding, shared folders, and networking.

Now Your Vagrant box is up and running. You can check the status of the Vagrant box as below with the vagrant status command:

╭─$ ~  
╰─➤ 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`.

SSH into this machine with vagrant ssh, and explore your environment. Leave the SSH session with ctrl-d or exit command, and shutdown with vagrant halt command.

For more information see the quick start guide on the official website.

Comments