Hashicorp Vagrant 101

For Ubuntu 18.04

Saeed Mohajeryami
May 23, 2020 · 5 min read

Vagrant is a tool for managing pre-configured Virtual Machines (VM). The emphasis is on management; Vagrant doesn’t build them, it just manages VMs created by existing hypervisors such as VirtualBox. It simplifies the process of downloading VM images and configuring them.

Vagrant also allows you to package your own box with your own environment variables, softwares, … and distribute it to the others. This way, you save a lot of time for people who need to have your own exact environment to replicate your work.

After booting up your VM box, you can configure other tools you need on your VM through several provisioners such as the shell, Terraform, Chef and Ansible. In other words, Vagrant integrates Provisioner and VirtualBox to configure your desired environment.

Source: https://www.softqubes.com/blog/introduction-of-vagrant-development/

Although you can use your package manager to install vagrant, I recommend downloading the Debian package from Vagrant website (here) and installing it directly.

Verify the installation by checking the vagrant version:

This is a config file that Vagrant creates for you with vagrant init command. You should just give Vagrant your OS of choice. You can find the list of all available OS images on Vagrant website. For example, all Ubuntu images are here. For example, if you need Ubuntu Bionic 64-bit OS, the command is as below:

The result of this step is a small file named ‘Vagrantfile’ that would be placed in your current directory.

The result of this step is only a small declarative file named Vagrantfile, which has all information to provision and configure a VM. Now, you are ready to spin your virtual environment. Look at the below Vagrantfile example.

As you see, the most important attribute of this config file is config.vm.box which specifies the base image you want to have. That is the only attribute that matters for a basic VM box configuration.

spin up your VM by using vagrant up command. It uses the Vagrantfile in the previous step to configure your virtual machine. Then, it downloads the OS image from the cloud and make it ready for you to connect.

The Ubuntu bionic 64-bit image is downloaded and configured. You can see the information for SSH.

You don’t need to know any IP or port, it is already known to vagrant. Enter the following to connect:

Voila! Your VM is ready!

You are connected to your VM

There is a folder shared between your local computer and VM box. It is your current local directory. This directory is accessible on your VM box. It is located on the root directory and its name is “vagrant”. You can see this in the below picture.

I created a file on my local current directory first, then connected to my VM box and went to root directory. then cd into vagrant folder and as you see it contain exactly the same files as my local computer current directory.

Manage VMs: You can manage your VM boxes with $ sudo vagrant box command. This command needs a subcommand, which you can see a list of them in the below picture.

Available subcommands for vagrant box

For example, you can access the list of available VM boxes on your machine by $ sudo vagrant box list

As you see, I have two VMs available to boot up on my machine.

Or you can remove VM boxes that you don’t need them anymore by $ sudo vagrant box remove <vm_name>

I had two boxes on my machine and I remove one

Stop a VM: You can stop a running VM with the following command $ sudo vagrant halt command. It stay in the same state and you can continue with vagrant up.

Remove a VM: You can stops and deletes all traces of the vagrant machine by $ sudo vagrant destroy command.

It asks you first if you want to kill the vm, if yes, then it destroy the VM with all its files. Be careful with this command.

Suspend/Resume a VM: You can suspends a running machine by $ sudo vagrant suspend and resume its operation whenever you want by $ sudo vagrant resume command. When you suspend a machine, it saves the VM state, which allows you to resume it later from where you suspended.

So far what you have is a bare OS. But you can do better. You can customize the box by installing the packages you need and create a customized image to distribute to whoever you want.

Assuming you have installed all necessary packages on your VM box, now it is time to freeze your VM to have a snapshot of the current state. It saves you a lot of time later when you need to reinstall everything on another computer.

You can create a snapshot of the current state of the VM box by using vagrant package command.

As you see, vagrant attempts to shutdown the vm, then export the box and ultimately compress it into a package, which is accessible on your current directory.

Vagrant creates your customized box. You can access it on your current directory. Its name by default is package.box.

You can enter this name on your Vagrantfile and it boots up this customized package for you.

If you distribute this package box and the Vagrantfile, everyone can replicate your environment with the exact packages you installed.

If you found this article helpful, share it with your friends and colleagues. If you have any other questions, you can find me on Linkedin or send me an email smohajer85@gmail.com.

The Startup

Get smarter at building your thing. Join The Startup’s +724K followers.

Saeed Mohajeryami

Written by

PhD in Electrical Engineering, Data Engineer, Lives in Redwood City, CA

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +724K followers.

Saeed Mohajeryami

Written by

PhD in Electrical Engineering, Data Engineer, Lives in Redwood City, CA

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +724K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store