Making Vagrant and Virtualbox installed on Windows as a provider is not well documented across the documentations. This document outlines how to set this up.
- Windows 11
- Virtualbox
- WSL2
Install Virtualbox and the Extension Pack from here: (https://www.virtualbox.org/wiki/Downloads)
Enter WSL and install the latest version of Vagrant using the following code
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
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
sudo apt update && sudo apt install vagrant
Important step to get SSH to work is to install the Virtualbox_WSL2 plugin so that we can SSH from WSL to the virtual machine.
vagrant plugin install virtualbox_WSL2
You must also enable WSL2 support by adding the following lines to the ~/.bashrc
. Note: Replace YOUR_USERNAME with the Windows username.
### .bashrc
# Setup Vagrant variables
export VAGRANT_WSL_ENABLE_WINDOWS_ACCESS="1"
export VAGRANT_WSL_WINDOWS_ACCESS_USER_HOME_PATH="/mnt/c/Users/YOUR_USERNAME/"
export PATH="$PATH:/mnt/c/Program Files/VirtualBox""
- You might need to allow your firewall for Virtualbox for private and public networks
- You might need to update your Powershell version on the host
- If there are error message that you cannot access cmd.exe from WSL2, add the following to your .profile:
PATH="/mnt/c/Windows/System32:$PATH"
- If you experience an issue with an outdated powershell version, there are some things to try
- Install the newest powershell version
- Check the
/opt/vagrant/embedded/gems/gems/vagrant-2.3.7/lib/vagrant/util/powershell.rb
- Add the powershell version you are using to the
PATH
variable
PATH="$PATH:/mnt/c/Program Files/PowerShell/7-preview"
I received the following when starting up the VM
There was an error while executing `VBoxManage`, a CLI used by Vagrant
for controlling VirtualBox. The command and stderr is shown below.
Command: ["startvm", "81868749-e1d9-4d05-92d0-0f2d66c1e402", "--type", "headless"]
Stderr: VBoxManage.exe: error: RawFile#0 failed to create the raw output file /dev/null (VERR_PATH_NOT_FOUND)
VBoxManage.exe: error: Details: code E_FAIL (0x80004005), component ConsoleWrap, interface IConsole
It seems that this is due to the activated Serial Port. After deactivating it via an option for the Virtualbox provider it worked.
Addvb.customize [ "modifyvm", :id, "--uartmode1", "disconnected" ]
to your Vagrantfile
like below.
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/jammy64"
config.vm.provider "virtualbox" do |vb|
vb.customize [ "modifyvm", :id, "--uartmode1", "disconnected" ]
end
If you use Docker Desktop and Virtualbox together, Virtualbox will be very slow (turtle mode). The turtle mode can be prevented when disabling the hypervisor. For Docker it needs to be enabled though otherwise Docker doesn't work.
bcdedit /set hypervisorlaunchtype off
bcdedit /set hypervisorlaunchtype auto
- https://gist.github.com/travelhawk/b9c1b6b7a9cd35cf3898fcb33cdae72d
- https://developer.hashicorp.com/vagrant/docs/other/wsl
- https://thedatabaseme.de/2022/02/20/vagrant-up-running-vagrant-under-wsl2/
- https://learn.microsoft.com/en-us/windows/wsl/networking
- https://blog.thenets.org/how-to-run-vagrant-on-wsl-2/
- https://stackoverflow.com/questions/45773825/vagrant-with-virtualbox-on-wsl-verr-path-not-found