Hashicorp Vagrant 101
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
Ansible. In other words, Vagrant integrates Provisioner and VirtualBox to configure your desired environment.
Install VirtualBox first
$ sudo apt-get install vagrant
Although you can use your package manager to install vagrant, I recommend downloading the Debian package from Vagrant website (here) and installing it directly.
$ sudo dpkg –i vagrant_2.2.2_x86_64.deb
Verify the installation by checking the vagrant version:
$ vagrant --version
Create a Vagrantfile
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:
$ sudo vagrant init ubuntu/bionic64
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
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/bionic64"
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
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.
$ sudo vagrant up
Connect to your VM by SSH
You don’t need to know any IP or port, it is already known to vagrant. Enter the following to connect:
$ sudo vagrant ssh
Voila! Your VM is ready!
Shared folder between your computer and VM box
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.
Useful vagrant commands
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.
For example, you can access the list of available VM boxes on your machine by
$ sudo vagrant box list
Or you can remove VM boxes that you don’t need them anymore by
$ sudo vagrant box remove <vm_name>
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
Remove a VM: You can stops and deletes all traces of the vagrant machine by
$ sudo vagrant destroy 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.
Create a customized image
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.
$ sudo vagrant package
Vagrant creates your customized box. You can access it on your current directory. Its name by default is
You can enter this name on your
Vagrantfile and it boots up this customized package for you.
Vagrant.configure("2") do |config|
config.vm.box = "package.box"
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 firstname.lastname@example.org.