How to use Vagrant with HyperV on Windows
First thing you need to know about using HyperV on Windows is you need to have at least Windows 10 Aniversary Update or higher. While its possible to run HyerV on earlier versions of windows like Windows 8, this guide makes use of some network features only available in Windows 10.
Also if you enable HyperV it will prevent other hypervisors like VMware and VirtualBox from working too.
Installing Hyper V
To install Hyper V it is as simple as opening an Elevated PowerShell window and running the following command:
Enable-WindowsOptionalFeature -FeatureName Microsoft-Hyper-V-All –Online
Setting UP Virtual Networks
To use Vagrant with Hyper V you need to setup VMSwithces for it to use. The other problem you will run into is HyperV doesn't have DHCP out of the box either.
So to work around this we will setup a NAT VMSwitch and we will install a small DHCP service on your host which will do DHCP for it.
Run the following commands from an Elevated PowerShell prompt.
New-VMSwitch -SwitchName "NAT" -SwitchType Internal New-NetIPAddress -IPAddress 192.168.10.1 -PrefixLength 24 -InterfaceAlias "vEthernet (NAT)" New-NetNat -Name NAT -InternalIPInterfaceAddressPrefix 192.168.10.0/24
Now you have a Virtual network setup, next step is to get DHCP running on it to assign IP Addreses.
Setting up DHCP
Download the latest version of DHCP Server from http://www.dhcpserver.de/cms/download/ and unzip it to where you want to keep it on your hard drive (e.g. c:\tools\dhcpserver).
Open the folder you unzipped and double click on DHCPWiz.exe.
Click Next to progress to next screen.
Select the network card named "vEthernet (NAT)".
Enter the IP pool as 192.168.10.2 - 100
Enter 255.255.255.0 for the subnet mask.
Click Add under DNS Server.
Enter the IP address of your DNS server. If you are unsure put 220.127.116.11 which is google's DNS.
Then click ok
Now under Gateways select 0.0.0.0 and click Edit.
Enter 192.168.10.1 and press Ok.
Click Write Ini File and then Next.
Click Admin and Accept the UAC prompt.
Click Install and then Start when it becomes available.
Click Configure and then Exit.
Now you have DHCP configured and running.
Enable PowerShell Remoting
PowerShell remoting is an important part of interacting with Windows virtual machine in vagrant. For this functionality to work properly run the following commands:
Enable-PSRemoting -Force -SkipNetworkProfileCheck Enable-WSManCredSSP -Role Server –Force Enable-WSManCredSSP -Role Client -DelegateComputer * -Force Start-service winrm Set-Item "wsman:\localhost\client\trustedhosts" -Value "*" -Force
Now to install vagrant itself. You can do this by downloading the installer from here or running the following chocolatey command:
choco install vagrant -yes
Close and reopen any powershell windows you have open at this point.
Note: By default vagrant will download large files and store configuration in a folder called .vagrant.d in your user profile. If you want to move this to another location you can do so with the
VAGRANT_HOME environment variable.
Set default Provider
Last step is to set the
hyperv. Doing this will cause vagrant to use VMware as its default provider. If you don't you might have vagrant try and download virtual box which is not what you want.
Testing Vagrant works
In a blank directory that you want to create your VM in run the following commands:
vagrant init hashicorp/precise vagrant up vagrant ssh
You should now have an SSH session into a virtual machine. To clean up use the following command: