Vagrant+VirtualBox on MacOS Catalina: But It Works on My Machine!
While Vagrant on Mac will typically run flawlessly, installation errors can and do happen. Let’s address the potential stumbling blocks on every system.
Join the DZone community and get the full member experience.
Join For FreeWhat Is Vagrant?
First launched back in March 2010 by Hashicorp’s Mitchell Hashimoto and his partner in crime John Bender, Vagrant is an open-source command line tool for virtual machine (VM) lifecycle management. The utility aims to increase development productivity by making this process a lot more simple. While Vagrant was originally tied to Oracle VirtualBox, since version 1.1, it has also included support for VMware, KVM, and others. Yet as helpful as virtualization is in web development, using Vagrant is not always a bulletproof option.
Why Use Vagrant? Is It Worth It?
First and foremost, Vagrant provides much greater flexibility with a hypervisor-based immutable environment. This enables developers to build environments requiring features only available on other systems.
For instance, if you needed to build an application that supported a variety of OSes and kernels, you could easily create a number of virtual machines that could run tests making use of Vagrant. Similarly, if you wanted to replicate your client’s environment (OS, resources, etc.), combining VMs and Vagrant might also be particularly helpful.
Installing Vagrant on a Workstation
To get started with Vagrant, download the installer or suitable package from the official Vagrant Downloads page. The below instructions are for Vagrant v2.3.3 (the latest version as of December 2022).
Installing Vagrant on Windows
In order to install Vagrant on Windows, you first need to download the MSI package. Next, you can simply run the file and go through a regular installation process.
Note: The installer will automatically add ‘vagrant’ to your system path, so it will also be available in terminals. If Vagrant is not found, however, try logging out and back into the system, as this is a common occurrence on Windows.
Installing Vagrant on MacOS Catalina
Assuming that you already have a ‘brew’ package manager installed, to quickly set up Vagrant on Mac, you can run the installation with this one-liner command:
```bash
$> brew install vagrant
```
Alternatively, you can install Vagrant from binaries.
Installing Vagrant on Linux
For Linux systems, we will demonstrate the installation process for Vagrant on Ubuntu 20.04:
Install the key:
```bash
$> wget -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor | sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg
```
Add the repository:
```bash
$> echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
```
Pull updates and run the installation:
```bash
$> sudo apt update && sudo apt install vagrant
```
That’s all there is to it! Now all you need to do is open your terminal and type “vagrant.” It’s also worth pointing out, however, that if you run Vagrant on its own, you will be given a list with the available subcommands.
Main Problems You Can Have When Installing Vagrant
While Vagrant installation errors are fairly uncommon, the likelihood of this significantly increases if multiple hypervisors are installed on your machine. After all, hypervisors do not tend to allow the creation of virtual machines when more than one of them is already in use.
The following are a couple of quick solutions taken from Vagrant’s official documentation that can help you use Vagrant and VirtualBox when another hypervisor is present:
On Linux
When using another hypervisor, you may encounter the error below:
```bash
There was an error while executing `VBoxManage`, a CLI used by Vagrant for controlling VirtualBox. The command and stderr is shown below.
Command: ["startvm", <ID of the VM>, "--type", "headless"]
Stderr: VBoxManage: error: VT-x is being used by another hypervisor (VERR_VMX_IN_VMX_ROOT_MODE).
VBoxManage: error: VirtualBox can't operate in VMX root mode. Please disable the KVM kernel extension, recompile your kernel and reboot
(VERR_VMX_IN_VMX_ROOT_MODE)
VBoxManage: error: Details: code NS_ERROR_FAILURE (0x80004005), component ConsoleWrap, interface IConsole
```
In order to get VirtualBox to run correctly, any additional hypervisors will need to be added to your system’s deny list.
To do this, first discover what the hypervisor is called:
```bash
$> lsmod | grep kvm
kvm_intel 204800 6
kvm 593920 1 kvm_intel
irqbypass 16384 1 kvm
```
Next, simply use the “blacklist” command to add the hypervisor to your deny list.
```bash
echo 'blacklist kvm-intel' >> /etc/modprobe.d/blacklist.conf
```
Finally, restart your machine and use the “vagrant” command once again.
On Windows
On Windows, if you are trying to start a VirtualBox VM, you may be faced with a jarring blue screen.
In order to use VirtualBox as normal, you have to ensure Hyper-V is disabled. Use the following simple command to turn off the feature on Windows 10:
```PowerShell
$> Disable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V-All
```
On Windows 11, you can also use an elevated Powershell:
```PowerShell
$> bcdedit /set hypervisorlaunchtype off
```
Alternatively, you can disable Hyper-V from your Windows system settings.
- Right-click on the Windows button and choose “Apps and Features”
- Open Turn Windows Features on or off
- Uncheck Hyper-V and finally click on OK
Reboot your machine to apply these changes.
On MacOS
If you are met with the following error message when attempting to execute VBoxManage:
failed to open /dev/vboxnetctl: No such file or directory
Either reinstall Virtualbox fully or simply use the commands below:
sudo /Library/StartupItems/VirtualBox/VirtualBox restart
or
sudo /Library/StartupItems/VirtualBox/VirtualBox start
Note that in more recent versions, the file /Library/StartupItems/VirtualBox/VirtualBox
does not exist, so you will have to use the following command:
sudo launchctl load /Library/LaunchDaemons/org.virtualbox.startup.pl
MacOS Catalina: But It Works on My Machine
Another issue that can occur on Mac is that classic problem when you hear an application "works fine on my machine," and no amount of begging or weeping can make it do the same anywhere else. My team from SPG had also faced this issue before.
Imagine that you are currently deploying and testing local environments with Vagrant + VirtualBox. One of the software engineers in your team uses a customized version of VagrantBox, which so far is working well for them. They'll be known as Developer 1. On their workstation, VirtualBox v6.1.34, Vagrant 2.2, and Ubuntu are all running without a problem.
On the other hand, it is clear that Developer 2 needs to upgrade their local environment. They currently use a mix of MacOS, VirtualBox v6.1.24, and Vagrant 2.1.x on their own workstation.
As a result, at least at first glance, Developer 2's strategy appears to be straightforward:
- Step 1: Update VirtualBox and Vagrant versions (it is crucial to use unified versions)
- Step 2: Install all additional project components
Both operations are successfully completed, but the outcome is certainly unexpected — for some reason, the website that was installed on VirtualBox is now entirely unreachable. What the heck!?
When confronted with such a riddle, it is critical to brainstorm a solution and take sensible actions like the ones listed below:
- Examine the log files
- Verify that all VirtualBox services, such as PHP, Nginx and MySQL are operational
- Use curl to access the page
In this case, however, everything appears to be operating as it should, without any obvious problems inside the VirtualBox instance itself.
Your team then decides to change tack and determine if the VirtualBox host is reachable from the outside. After using a straightforward ping command, they are unprepared to discover that 100% of the packets are lost. Though this news is somewhat disconcerting, it might also be the cause of the whole shebang.
Following this thread, they eventually find out that HostOnly adapters must now receive a different range of IP addresses. This apparently began with the VirtualBox v6.1.30 update (a patch update, of all things):
192.168.55.х prior to the update
192.168.56.х. after the update.
There it is, clear as day, in the recently revised VirtualBox user manual:
“On Linux, Mac OS X, and Solaris, Oracle VM VirtualBox will only allow IP addresses in 192.168.56.0/21 range to be assigned to host-only adapters.”
Everyone on your team breathes a collective sigh of relief. The issue suddenly disappears after creating a new HostOnly adapter and changing the one in the VirtualBox instance. The critical web component of the solution is finally accessible, and Developer 2 is happy at last.
Vagrant Or...? Which Alternatives Exist? How Are They Better?
With so much excitement, you could be forgiven for looking for Vagrant alternatives. If you were to do so, however, you would probably come across a number of websites that compared the utility to containers (more often than not, Docker). But is this actually a valid comparison? As it turns out, it actually is, as long as you are aware of the difference between the two.
Many of the advantages Docker has over hypervisors can also be considered disadvantages — it just depends on what you need it for. So, let's start with a big one: load time. While containers can start up quickly because they are basically just a set of processes, this also means that it uses the host kernel. As a result, instead of relying on virtualized hardware, containers will use the host's hardware directly.
Hypervisors can also have dedicated resources in the form of memory, CPU time, drive space, and others. Containers, on the other hand, share resources with other processes on the host computer. Similarly, while Vagrant enables you to create an entire virtual machine — which will require more time and resources — Docker's containerized approach allows you to deploy faster with fewer resources.
So, with that in mind, just remember that this is not a competition. It is simply about your company’s needs.
Conclusion
While Vagrant is undoubtedly a formidable tool, it is also not immune to problems. If your company lacks the expertise or knowledge, a dedicated team of software developers might be able to help. After all, even when it only “works on my machine,” a solution exists for every problem.
Published at DZone with permission of Richard Haraldson. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments