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.
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.
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.
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.
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.
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.
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.
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.
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:
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.
Vagrant provides a lot of documentation, which is very helpful to get an idea how things work together.
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.
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.