In this post, I’ll discuss what Vagrant is, some details about getting it installed, and then the structure of a basic Vagrant deployment.
What is Vagrant?
Vagrant is one of those tools that, once you begin using it, you find yourself saying: “Wait – How long has this existed?” and “Why haven’t I been using it all these years?!”
So, the, what is Vagrant? Vagrant is a tool for defining virtual machines as code in order to make the process of creating, maintaining, sharing, and cleaning up after them much faster, easier, more consistent, and less error prone.
You define in text what you want virtual machines you want to build and what configuration they should have, then just type
vagrant up in your terminal, and it does all the deployment and configuration for you.
Let take a brief look at what this extra bit of functionality does for you….
The Problems Vagrant Solves
Explaining the problems Vagrant solves and how it works is best done by example. Let’s say you want to create virtual machine for testing your web application. You could just install VirtualBox and manually configure your VMs. But manual configuration is rife with problems and Vagrant helps solve many of them.
Manual configuration is:
- Slow: Manually typing commands in and clicking through screens is takes time.
- Tedious: If you need to do a task more than one time, it will quickly become tiresome to perform those same tasks again and again.
- Inconsistent: The way you configure things one time is invariably different from the way you configuration them next time.
- Error-Prone: Because we as humans are inconsistent and prone to distraction when things are slow and tedious, we lose focus, make mistakes, and omit steps.
- Opaque: If you wish reproduce your configuration steps or share them with others, you really have no way of doing so, outside of recording your working session; and if you’re going to do that, you might as well just use Vagrant.
In contrast, Vagrant allows for that process to be:
- Fast: Machines work faster than humans.
- Easy: Write once, run as many times as you want, tweak as necessary.
- Consistent: Every time you run it, the same steps are being performed.
- Error-Free: Every time you run it, you get the same results.1
- Transparent: Because your configuration is defined in code, you can share it with others.
1 This assumes, of course, that the context is the same. Even when it is not, however, recovering from changes in context (platform, software revisions, feature changes, etc.) becomes a lot easier when your configuration is defined as code.
In addition to all this, you gain the ability store your files in version control and copy between your projects (and other people’s) as well.
BIOS Configuration Settings
Before you install anything, make certain that your computer BIOS settings are configured for virtualization with Intel VT-x or AMD-V.
On Windows, for example, run the following from a Command Prompt or PowerShell with Administrative privileges:
systeminfo | findstr "Hyper Virtualiz Transl Prevent"
You should see the following:
Hyper-V Requirements: VM Monitor Mode Extensions: Yes Virtualization Enabled In Firmware: Yes Second Level Address Translation: Yes Data Execution Prevention Available: Yes
If you don’t see this output, first verify that your computer has a 64-bit processor with Second Level Address Translation (SLAT), and that it has CPU support for VM Monitor Mode Extension (VT-c on Intel CPUs). Then, consult the support documentation for your computer hardware for information on how to enable VT-c if you have an Intel processor, or AMD-V, if you are using AMD.
If you are running Linux and are using KVM, you may get an error that “VT-x is being used by another hypervisor.” Consult the Vagrant documentation on Running Multiple Hypervisors.
Note: Event if you a running Windows 10, as of this writing, I suggest that you still use VirtualBox instead of Hyper-V. Why? Hyper-V networking is problematic with Vagrant, because it is unable to make customizations which are necessary for build that are anything more than basic, single virtual machine deployments. In addition, VirtualBox is more widely used, and by defining your configurations for VirtualBox, you will be able to share them more widely and utilize the configurations used by others as well.
You will need install VirtualBox, which is very well-documented and fairly simple, so I will not cover that here.
You simply need to verify the following:
Default Machine Folder(Preferences > General) is on storage with sufficient space to store the virtual machines you wish to build.
- Vagrant’s executable is on your path (in other words, you can open a terminal or Command Prompt, type “vagrant –version” and do not get an error).
Per the “Installing Vagrant” documentation:
The installer will automatically add vagrant to your system path so that it is available in terminals. If it is not found, please try logging out and logging back in to your system (this is particularly necessary sometimes for Windows).
After installing VirtualBox, to install Vagrant, simply download Vagrant for your platform and install the package using standard procedures for your operating system.
Now that you have Vagrant installed, there any number of ways you may want to run it. Once it’s on your
PATH, theoretically any shell2 that has access to it should be able to run the commands, but that doesn’t always turn out the case. Here are a couple of “gotchas” that you want to look out for and what has consistently worked for me.
2 I use the term “shell” here in the loosest possible sense: Command Prompt, terminal emulator, command interpreter, etc.
- Problem: Some emulators do not display output well.
- Solution: Install Git for Windows and use the “Git Bash” shortcut (which is a suite of tools running in MinGW).
- Problem: You encounter problems using the Hyper-V provider due to lack of privileges.
- Solution: Run your shell “as Administrator” using the context menu, or by holding down
Ctrl + Shiftwhile selecting it in the Start menu or Taskbar.
- VirtualBox on Windows 10
- Vagrant installed and available on the system wide PATH (
$PATHdepending on your shell).
- Git Bash using the MinTTY terminal emulator.
This setup runs flawlessly and has remained stable over the years that I’ve used it.
The easiest way to get started is to create a new directory for your project, change into it, and run
vagrant init $BOX where
$BOX is the name of a Vagrant Box you wish to run.
For example, the following will initialize a Vagrantfile for an Ubuntu 18.04 virtual machine:
vagrant init ubuntu/bionic64
You should see the following output:
A `Vagrantfile` has been placed in this directory. You are now ready to `vagrant up` your first virtual environment! Please read the comments in the Vagrantfile as well as documentation on `vagrantup.com` for more information on using Vagrant.
Now that the basic configuration is created, simply type
vagrant up to have Vagrant create the defined virtual machine in VirtualBox:
Note: The first time you run
vagrant upon a given Vagrant box, it will need to first download the box image before creating the virtual machine. The next time you run
up, you will not need to wait for this download.
Once the virtual machine is running, you should see this message in the output:
==> default: Machine booted and ready!
You can access your VM using the VirtualBox console, Remote Desktop, or ssh. To access your VM using SSH, Vagrant provides the
vagrant ssh sub-command:
When you are through with a virtual machine and want to delete it and all its files completely, use
vagrant destroy --force
Note that this permanently deletes everything related to the virtual machine instance that Vagrant created. The Vagrantfile and the Vagrant Box (image) from which it was derived are not.