Enabling VirtualBox Guest Additions in Vagrant
The VirtualBox Guest Additions are a set of drivers and applications to be deployed on a virtual machine to have better performance and enable features such as folder sharing. While it's possible to include the Guest Additions directly in the box, not all the boxes you'll find have it, and even when they do, they can be outdated very quickly.
The solution is to automatically deploy the VirtualBox Guest Additions on demand, through a plugin.
Note
The downside to using this plugin is that the Vagrant box may now take longer to boot, as it may need to download and install the right guest additions for the box.
Getting ready
To step through this recipe, you will need the following:
- A working Vagrant installation
- A working VirtualBox installation
- An internet connection
- The Vagrantfile from the previous recipe
How to do it…
Follow these steps to enable VirtualBox Guest Additions in Vagrant:
- Install the
vagrant-vbguest
plugin:$ vagrant plugin install vagrant-vbguest Installing the 'vagrant-vbguest' plugin. This can take a few minutes... Installed the plugin 'vagrant-vbguest (0.13.0)'!
- Confirm that the plugin is installed:
$ vagrant plugin list vagrant-vbguest (0.13.0)
- Start Vagrant and see that the VirtualBox Guest Additions are installed:
$ vagrant up […] Installing Virtualbox Guest Additions 5.0.26 […] Building the VirtualBox Guest Additions kernel modules ...done. Doing non-kernel setup of the Guest Additions …done.
- Now, maybe you don't want to do this every time you start you Vagrant box, because it takes time and bandwidth or because the minor difference between your host VirtualBox version and the one already installed in the Vagrant box isn't a problem for you. In this case, you can simply tell Vagrant to disable the auto-update feature right from the Vagrantfile:
config.vbguest.auto_update = false
- An even better way to keep your code compatible with people without this plugin is to use this plugin configuration only if the plugin is found by Vagrant itself:
if Vagrant.has_plugin?("vagrant-vbguest") then config.vbguest.auto_update = false end
- The full Vagrantfile now looks like this:
Vagrant.configure("2") do |config| config.vm.box = "ubuntu/xenial64" if Vagrant.has_plugin?("vagrant-vbguest") then config.vbguest.auto_update = false end end
How it works…
Vagrant plugins are automatically installed from the vendor's website, and made available globally on your system for all other Vagrant environments you'll run. Once the virtual machine is ready, the plugin will detect the operating system, decide if the Guest Additions need to be installed or not, and if they do, install the necessary tools to do that (compilers, kernel headers, and libraries), and finally download and install the corresponding Guest Additions.
There's more…
Using Vagrant plugins also extends what you can do with the Vagrant CLI. In the case of the VirtualBox Guest Addition plugin, you can do a lot of things such as status checks, manage the installation, and much more:
$ vagrant vbguest --status [default] GuestAdditions 5.0.26 running --- OK.
The plugin can later be called through Vagrant directly; here it's triggering the Guest Additions installation in the virtual machine:
$ vagrant vbguest --do install