How Do I Upgrade VirtualBox from the Command Line
How to upgrade VirtualBox via the Command Line in macOS - 5 steps!

This is a quick guide on how to upgrade (install) VirtualBox from the command line.   It will involve mounting a .dmg, installing a .pkg, and finally upgrading (installing) the Extension Pack.  

Prerequisites

This guide assumes you have already downloaded the required files found on https://www.virtualbox.org/wiki/Downloads and have sudo privileges.

You also need the names or UUIDS of the running VMs. You can get that by using the VBoxManage list runningvms command.  Remember if you use the VM name and it has spaces, you must encapsulate it in quotes.  Example: VBoxManage showvminfo "My VM"



1.  Shutdown all running VMs and VirtualBox Application

Shutting down VMs is as simple as logging in and powering down the VM.  However, since this is "Terminal only" solution, we need to look at the savestate function in VBoxManage.

VBoxManage controlvm &lt;vmname|UUID&gt; savestate


Issue that command for each VM by name or UUID and it will automatically save the "position" of the running VM and then close it.  This is equivalent to selecting the Close item in Machine Menu of the VirtualBox GUI. 

Note:  This is not like "pulling the plug"  This will save the machine state so that when you power back on, it will pick up right where it left off. 

Next, we need to make sure the VirtualBox GUI (management console) isn't running.  To kill the VirtualBox GUI, just issue the command:

killall VirtualBox




2.  Mount the VirtualBox Installation .dmg1

hdiutil attach ~/Downloads/VirtualBox-Version.dmg


You will see several "Checksumming" messages and when finished, you will see the disks and mount point that the image was attached to similar to below (these device identifiers and names will differ from yours):

/dev/disk5              GUID_partition_scheme
/dev/disk5s1            Apple_HFS                       /Volumes/VirtualBox


The important piece is /Volumes/VirtualBox.  This is where the installer will be located.



3. Install the .pkg2

Next, we're going to run the installer to install the .pkg file. Located on the volume we just mounted.

sudo installer -pkg /Volumes/VirtualBox/VirtualBox.pkg -target /


If you get the following message, go back to step 1

installer: Error - The installer has detected running virtual machines. Please shut down all running VirtualBox machines and then restart the installation


If all goes well, you should see the following message:

installer: Package name is Oracle VM VirtualBox
installer: Upgrading at base path /
installer: The upgrade was successful.  


You can verify that the new version was installed by issuing the command:

VBoxManage --version




4. Install the Extension Pack

To upgrade the Extension Pack, you need to remove the old one and install the new one.  You can use the VirtualBoxManage extpack uninstall [--force] { name } and VBoxManage extpack install {tarball} commands, but luckily, Oracle provided us with a --replace option to do it in one step:

sudo VBoxManage extpack install --replace ~/Downloads/Oracle_VM_VirtualBox_Extension_Pack-version.vbox-extpack


There will be some license terms, simply enter "Y" to accept.  If successful, you will see the following message:

Successfully installed "Oracle VM VirtualBox Extension Pack".




5. Restart your VMs and Eject Your Volume

Success!  You're all done.  Restart your VMs as needed

VBoxManage startvm &lt;vmname|UUID&gt; [--type headless|GUI|seperate]


Most of my VMs are headless boxes so I issue the command with the --type headless option

VBoxManage startvm "FreeBSD" --type headless


Don't forget to eject the installation media volume.  You won't want unsuspecting users coming in and finding attached volumes!

hdiutil eject /Volumes/VirtualBox




References and further reading

1Is there a command to install a dmg

2Installing .pkg with terminal ?

VBManage Reference Manual