Skip to content

Instantly share code, notes, and snippets.

@dot-mike
Forked from travelhawk/README.md
Last active January 20, 2025 21:49
Show Gist options
  • Save dot-mike/cb35f9a4b02c4180f8d76b28bf6affec to your computer and use it in GitHub Desktop.
Save dot-mike/cb35f9a4b02c4180f8d76b28bf6affec to your computer and use it in GitHub Desktop.
Use vagrant and ansible on Windows (WSL2)

Use vagrant and ansible on Windows (WSL2)

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.

Requirements

  • Windows 11
  • Virtualbox
  • WSL2

Install Vagrant on WSL

Install Virtualbox

Install Virtualbox and the Extension Pack from here: (https://www.virtualbox.org/wiki/Downloads)

Install Vagrant

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""

Additional Issues

  • 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"
    

VERR_PATH_NOT_FOUND when using vagrant up

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

Docker Desktop and Virtualbox

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.

Disable Hypervisor

bcdedit /set hypervisorlaunchtype off

Enable Hypervisor

bcdedit /set hypervisorlaunchtype auto

Resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment