5 Vagrant Commands You Need To Know

In this article, I will point out five commands that every Vagrant user needs to know.

Vagrant is a favorite among DevOps professionals as it provides a great way to great re-usable and identical test environments. The Vagrant CLI is very easy to use and provides users a way to manage their Vagrant environment efficiently.

1 – Vagrant Init

To understand Vagrant, you first need to understand one of the basic building blocks – Vagrantfile. A Vagrantfile is basically a configuration file that describes an environment. It will include things like the boxes used, networking, CPU and memory, providers used, shell scripts to run for provisioning among others.

In this example, I want to create a very basic Vagrantfile based on a box used from Vagrant Cloud. To do this I simply run vagrant init with the box address.

First, I will create a directory:

Dans-MacBook-Pro:GitHub dan$ mkdir MyProj

Next, lets cd into that directory

Dans-MacBook-Pro:GitHub dan$ cd MyProj/

Here, I will use vagrant init and the –m option which means I will not add any helper comments into my Vagrantfile:

Dans-MacBook-Pro:MyProj dan$ vagrant init -m hashicorp/precise64

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.

Finally let’s look into the Vagrantfile, which as we see just specifies the box I will be using:

Dans-MacBook-Pro:MyProj dan$ cat Vagrantfile 

Vagrant.configure("2") do |config|
  config.vm.box = "hashicorp/precise64"

Just like that I have enough to boot up a Vagrant box on my local machine.

2 – Vagrant Box

The vagrant box command allows the user to manage the boxes on their local machine. This includes adding, removing, listing and updating. For instance, to view the boxes I have installed on my machine I would run vagrant box list:

Dans-MacBook-Pro:GitHub dan$ vagrant box list

StefanScherer/windows_10        (virtualbox, 2018.04.10)
bento/centos-7.2                (virtualbox, 2.3.1)
bento/centos-7.3                (virtualbox, 201708.22.0)
eratiner/w2016x64vmX            (virtualbox, 1.2.0)
ferventcoder/win2012r2-x64-nocm (virtualbox, 1.0.0)

If I want to add a box, logically I use vagrant box add. Here I want to download and install a CentOS box for the virtualbox provider:

Read more at ipswitch.com

Comments are closed.