When VirtualBox Vanishes After a Homebrew Upgrade and Vagrant Breaks

Vagrant could not detect VirtualBox!

* This page contains promotional content

Symptoms

I run several servers in Docker containers with Vagrant on my Mac, but the guest OS I had started with Vagrant stopped coming up, and neither vagrant ssh nor vagrant halt worked any more.

$ vagrant halt
The provider 'virtualbox' that was requested to back the machine
'default' is reporting that it isn't usable on this system. The
reason is shown below:

Vagrant could not detect VirtualBox! Make sure VirtualBox is properly installed.
Vagrant uses the `VBoxManage` binary that ships with VirtualBox, and requires
this to be available on the PATH. If VirtualBox is installed, please find the
`VBoxManage` binary and add it to the PATH environmental variable.

VirtualBox cannot be found? Did it disappear?

Why VirtualBox disappeared

My guess is that the cause was probably the updates I run with Homebrew from time to time.
When brew update;brew upgrade found an update for VirtualBox, the uninstall script ran, the installation could not complete properly, and so it was left gone.

Trying to reinstall virtualbox

$ brew reinstall virtualbox

...
(kernel) Kext org.virtualbox.kext.VBoxNetFlt did not stop (return code 0x5).
(kernel) Kext org.virtualbox.kext.VBoxNetFlt can't unload - module stop returned 0xdc008017.
Failed to unload org.virtualbox.kext.VBoxNetFlt - (libkern/kext) kext (kmod) start/stop routine failed.
An error occurred durning 'sudo /sbin/kextunload -m org.virtualbox.kext.VBoxNetFlt', there should be a message above. (rc=3)
unloading org.virtualbox.kext.VBoxDrv
(kernel) Can't remove kext org.virtualbox.kext.VBoxDrv; services failed to terminate - 0xdc008018.
Failed to unload org.virtualbox.kext.VBoxDrv - (libkern/kext) kext is in use or retained (cannot unload).

The reinstall fails as well.
It seems the installation cannot go through because VBox-related processes are still running.

$ ps aux|grep VBox
user             1053   4.9 16.9 10433224 5664640   ??  S    27 421  1198:10.06 /Applications/VirtualBox.app/Contents/MacOS/VBoxHeadless --comment vagrant_default_1617775585834_25189 --startvm 92f812c7-e002-4915-a44e-37f956c82adc --vrde config
user            86410   0.2  0.0  4399028    792 s000  R+   10:36AM   0:00.00 grep VBox
user              925   0.0  0.1  4876816  18988   ??  S    27 421   15:23.36 /Applications/VirtualBox.app/Contents/MacOS/VBoxSVC --auto-shutdown
user              923   0.0  0.0  4427532  11152   ??  S    27 421    2:34.08 /Applications/VirtualBox.app/Contents/MacOS/VBoxXPCOMIPCD

The reinstall was failing because VBOX-related processes were running, so kill the VBOX-related processes
In the case above that means kill 923 925 1053

After this I installed VirtualBox again, the installation finished without trouble, and vagrant up started the machine normally

Additional notes

One way or another, it feels like VirtualBox + Vagrant runs into trouble fairly often
Since there is a good chance the same symptom will show up whenever a VirtualBox update arrives in the future, it may be safer to pin virtualbox out of the way in homebrew.

Excluding a specified formula

brew pin formula-name

Undoing the exclusion (putting it back)

brew unpin formula-name

Listing the formulas that have been excluded

brew list --pinned