Vagrant with VirtualBox in WSL

Don’t do it – unless you are brave or really require it

Vagrant with WSL is a constant mix of Linux and Windows. I personally think, there are very good reasons to use Vagrant with WSL, which is why I still do it, but not all features work and especially not out of the box

Setup

Please also check the official Vagrant documentation

The official documentation suggests, that you should avoid having Vagrant on Windows and WSL installed at the same time or at least keep the same versions.

Please be aware, that the project you are working on must be located on a directory, which is accessible from Windows. This is caused by the fact, that Vagrant will mount the project directory into the VMs as shared folders, so VirtualBox needs access to it.

To run Vagrant on WSL, you will have to install the virtualbox_WSL2 plugin first.

By default Vagrant creates a NAT interface and a port-forwarding to it, to connect to the VMs on localhost. This is not accessible from within WSL. The plugin enables Vagrant to connect to the VMs using the IP of the Windows system instead of localhost.

1vagrant plugin install virtualbox_WSL2

Furthermore, it is required to allow access from WSL and to define the user home.

1export VAGRANT_WSL_ENABLE_WINDOWS_ACCESS="1"
2export VAGRANT_WSL_WINDOWS_ACCESS_USER_HOME_PATH="/mnt/c/checkouts/"

The user home path is required to e.g. skip permission checks on the SSH keys, but it's not used everywhere. So another step is required to actually allow Vagrant to set proper permissions on the generated keys:

1[automount]
2options = metadata,umask=22,fmask=11
3enabled = true

To apply these changes, you have to close all WSL sessions and run:

1wsl --shutdown

For more information about these settings, please refer to the official Microsoft documentation

There you go, the basic features of Vagrant are working.

Broken Features

What I noticed so far:

  1. Disk resizing It is not possible to resize disks due to path issues. I have put in a Pull request to fix this issue, but I am neither a Vagrant expert nor a Ruby developer, so I am not sure if this will ever be merged.

  2. Cloud-Init When using the Cloud-Init provider, Vagrant will build and mount an ISO image. On WSL this image will be located under /tmp/.iso and will then instruct VirtualBox (running on Windows) to mount /tmp/.iso