Sometimes you just want to quickly set up a testing or development environment. Typically, you download the .iso, create your machine, boot, and complete the installation process. If you’re doing that many times, it can really get annoying. Wouldn’t it be cool, to just automate that whole virtual machine deployment? Fortunately, there is such a solution that’s called Vagrant.
I’ve come across Vagrant because I often need to set up virtual machines for making tutorials or development tasks. Let me show you how you can easily do an automated virtual machine deployment.
What is Vagrant?
Vagrant is an open-source tool that is developed by Hashicorp. It’s mainly for developers, operators, or designers. It integrates with common hypervisors to automate the creation and provisioning process for your virtual machines. Sounds complicated? It may be easier than you think, let me show you.
First, you set up a template of your desired virtual machine in a simple configuration file. These files are called a Vagrantfile. Then you would choose a prepared image that contains your desired Operating System or applications called boxes. You find lots of different boxes for Linux distributions, Windows machines, and much more on the Vagrant box catalog. After that, you simply execute the Vagrant command in your console.
Vagrant connects to your hypervisor through an interface that is called a provider. Therefore it can easily create, boot, and provision your virtual machine. Providers exist for many hypervisors, you can even develop your own custom ones! You can find a lot of providers also for cloud environments like Azure, AWS, etc.
How to install Vagrant?
You can easily install Vagrant on almost all Operating Systems. It runs on Windows, Mac OS, and many Linux distributions. Because it integrates with hypervisors like VirtualBox, Hyper-V, and also custom ones, it’s highly flexible.
Always remember to install your hypervisor before you install Vagrant. Just follow the installation instructions on the official Vagrant Homepage.
Once you have installed Vagrant, you simply can run the command “vagrant” in your console.
Create our first Virtual machine deployment with Vagrant
Create the VagrantFile Template
Let’s set up our first virtual machine deployment with Vagrant. You should create a new directory with enough disk space because Vagrant will store the configuration and virtual hard disk in this location. Then, we will initialize a new Vagrantfile with the following command.
This will create a new Vagrantfile, we can easily inspect and customize. First, we need to change the configuration file to use a specific box. In this tutorial, we’re using the “hashicorp/bionic64” box as a quick example. We edit the Vagrantfile and change the box with this line.
config.vm.box = "hashicorp/bionic64"
(Optional) If you’re running Hyper-V on windows 10
If you’re running Hyper-V on Windows 10, Microsoft recommends adding the following entries according to this blogpost.
config.vm.provider "hyperv" config.vm.synced_folder ".", "/vagrant", disabled: true config.vm.provider "hyperv" do |h| h.enable_virtualization_extensions = true h.linked_clone = true end
Note: If you’re having an AMD processor like me, setting “enable_virtualization_extensions = true” will fail, because of the missing nested virtualization support in Windows 10! If this is the case, simply change it to false.
Start the virtual machine
Now, we can simply start the creating and provisioning of your virtual machine via the console. To start a virtual machine with Vagrant execute this command in the console.
If you’re running this the first time, it should automatically download the box image and ask you to generate an SSH key. Confirm this and continue with this tutorial.
After the machine is booted and ready we can now connect via SSH to the virtual machine.
If you have made changes to the Vagrantfile, you need to stop and reboot the machine again with this command.
If you want to delete a virtual machine, simply type this command. Note that this will delete the virtual disks and the config on your hypervisor, but not the Vagrantfile or the folder.
Explore Vagrant Boxes
Probably, you want to install other Operating Systems like Windows Servers, Windows 10 or also other Linux distributions as well. You can find all those different boxes on the Vagrant box cataloge. Don’t forget to filter the results for your desired provider. Otherwise, the results may not work on your hypervisor.
For example, I’ve also tested the “generic/ubuntu2004” box. This contains an Ubuntu 20.04 LTS image. If you want to create a second virtual machine, simply create a new folder. Note, you don’t need to init a new Vagrantfile with the “vagrant init” command. You can also copy the Vagrantfile of your first virtual machine and use this as a template.
If you start the virtual machine, Vagrant will automatically download the box and store this on your computer. That means, you don’t need to download the box image again. If you want to check what boxes are currently stored on your computer, use this command.
vagrant box list
You can also manage boxes, for example remove them to clean up your system with this command.
vagrant box remove <boxname>
Manage virtual machines with vagrant
Check what virtual machines are currently running with Vagrant using this command. This can be executed from any directory.
This will probably show orphaned records as well. This is because Vagrant is cashing the data and this may not be fully up to date. To clear the cash and remove invalid entries, execute this command
vagrant global-status --prune
What should I Do next?
You can now simply create a virtual machine deployment with Vagrant. I strongly recommend you to have a look at the Vagrant documentation to see what you can configure and how it integrates with your hypervisor.
It’s also possible to execute custom scripts after the deployment or integrate with other automation tools like Ansible or Puppet.