Search code examples
windowsvagrantwindows-subsystem-for-linux

Vagrant with Virtualbox on WSL unable to access Windows environment


I have successfully followed steps on vagrant website to setup vagrant on WSL with VirtualBox including setting the windows path and all. but as soon as I try to run

sudo vagrant something up 

it throws the following error

Vagrant failed to initialize at a very early stage:

Vagrant is unable to use the VirtualBox provider from the Windows Subsystem for Linux without access to the Windows environment. Enabling this access must be done with caution and an understanding of the implications. For more information on enabling Windows access and using VirtualBox from the Windows Subsystem for Linux, please refer to the Vagrant documentation:

Host OS

Edition: Windows 10 Home Single Language Version: 20H2 OS Build: 19042.870

Ubuntu WSL Description: Ubuntu 20.04 LTS


Solution

  • This is caused when VAGRANT_WSL_ENABLE_WINDOWS_ACCESS environment variable is not set.

    1. First Export Environment Variables
      export VAGRANT_WSL_ENABLE_WINDOWS_ACCESS="1"
     export PATH="${PATH}:/mnt/c/Program Files/Oracle/VirtualBox"
    1. Run Vagrant using following commands
      vagrant up something 

    if you want to run it as sudo

     sudo -E vagrant up something
    

    "E" flag preserves the environment variables while using sudo. if this flag is not used the exported environment variables will not be avalaible.