Vagrant - Getting Started

Vagrant Mar 04, 2021

Vagrant by Hashi Corp enables users to create and configure lightweight, reproducible, and portable development environments. It is basically Virtualization configuration in a single file. This guide will enable you to install and use Vagrant for simple tasks.

Why Vagrant?

You can set up your development environment in many ways. You can use container technologies like Docker or Podman, you can install all packages locally or deploy VMs manually. Vagrant is helping with the VM part, but can also be used to provision containers. All from a single Vagrantfile.

Vagrant offers some features, very handy for developers, but also for operators.

  • Simple to use and easy to maintain
  • Powerful and feature rich
  • Works on Windows, Mac and Linux
  • Can be shared across teams (ex. via Git)
  • Runs on your development workstation
  • Start, Provision, Stop, Clean Up in one tool
  • Can be extended with Ansible, Cloud Init and more

If you want to have your development environment reproducible and transportable on your workstation, you should try Vagrant.

Installation

The Installation of Vagrant very easy. For installation options, you should have a look at the documentation. For this Article, we will install Vagrant with libvirt (KVM) on a Fedora Workstation.

Hint
The guide is tested on Fedora 33 with Vagrant 2.2.9.

Precheck

First, you should check if your machine does support virtualization.

# Check for virtualization support
$ lscpu | grep Virtualization
Virtualization:                  VT-x

The above is the output of an Intel based machine and may be different for other CPU technologies. If the command does not provide any output, your machine may not support virtualization, or you have to enable it in your BIOS/UEFI.

Install

Installing vagrant is as easy as:

# Install vagrant and libvirt dependencies
$ sudo dnf install vagrant vagrant-libvirt

# Start the libvirt service
$ sudo systemctl enable --now libvirt.service

Afterwards, you will be able to create your first Vagrantfile.

Vagrantfile

After the installation, we can already create the first Vagrantfile for testing and demonstration.

# Create project
$ mkdir PROJECT
$ cd PROJECT
$ touch Vagrantfile

The Vagrantfile should have content like this.

Vagrant.configure("2") do |config|
  config.vm.box = "fedora/33-cloud-base"
end
Vagrantfile

And we can already start our first machine.

# Start a Vagrant machine
$ vagrant up

The very first initialization will take some time, since a Vagrant Box will be downloaded from the Vagrant Cloud. After a minute or two (including the download) the machine will be ready, and you can continue to use some commands.

Commands

Interacting with Vagrant machines is quite easy, too. If you know Docker or Podman, you will see some similar behavior.

# Get help
$ vagrant help

# Check status
$ vagrant status

# Check global status (for the entire system)
$ vagrant global-status

# Start machines from a Vagrantfile
$ vagrant up

# Destroy machines
$ vagrant destroy

# Reboot / reload a machines
$ vagrant reload

# Connect to the machine via ssh
$ vagrant ssh

# Or read config to the same with your regular client
$ vagrant ssh-config

These commands should help to get things going.

Next steps

Running a single VM is not something that really helps with development. But what about multiple VMs, with different OS? This can be achieved with a quite simple configuration like the below:

Vagrant.configure("2") do |config|

  # libvirt
  config.vm.provider :libvirt do |libvirt|
    libvirt.cpus = 1
    libvirt.memory = 2048
  end

  # Fedora
  config.vm.define "fedora33" do |fedora33|
    fedora33.vm.box = "generic/fedora33"
    fedora33.vm.hostname = "fedora33"
  end

  # CentOS
  config.vm.define "centos8" do |centos8|
    centos8.vm.box = "centos/stream8"
    centos8.vm.hostname = "centos8"
  end

end
Vagrantfile

These files can be shared in a repository and can be easily used, if all the developers are having Vagrant on their machines. They also serve as a documentation for your environment setup.

Tipp: You should add the .vagrant directory to your .gitignore file, so your local cache, images and snapshots are excluded from the repository.

You can do much more, like snapshots, suspends, manipulate boxes or add provisioners to your Vagrantfile. You can also set up more complex setups with different networks or additional disks. I will ensure to address these in a future article.

Documentation

Vagrant provides a lot of documentation, which is very helpful to get an idea how things work together.

Documentation | Vagrant by HashiCorp
Welcome to the documentation for Vagrant - the command line utility formanaging the lifecycle of virtual machines. This website aims to documentevery feature of Vagrant from top-to-bottom, covering as much detail aspossible.
Getting started with virtualization :: Fedora Docs

The fedora magazine also has some articles about Vagrant and the Fedora docs are addressing virtualization in general. In Fedora, you will see that tools like virt-install, virt-top or virsh can be used on the same machine and even interact with each other via libvirt.

Installing and running Vagrant using qemu-kvm - Fedora Magazine
A simple guide showing how to install and start using Vagrant for virtualization on Fedora using qemu-kvm.
Vagrant beyond the basics - Fedora Magazine
Go into the details of how to configure a Vagrant box. From networking to provisioning and sharing file system, you will go beyond the basics

Conclusion

Vagrant is easy to set up and after just some minutes it can add to your workflow. Having one or more VMs on your fingertips in just some seconds can be very helpful. You can share these Vagrantfiles via Git or even a messenger or E-Mail.

Tags

Daniel Schier

Just a guy doing stuff. Mostly #FLOSS like #Linux, #Ansible, #Podman, #K8s, #Python, #Nextcloud or whatever comes next.

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.