Skip to content

Instantly share code, notes, and snippets.

@1eedaegon
Last active June 29, 2022 06:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save 1eedaegon/2535436ade80b28e4d97f517c6434638 to your computer and use it in GitHub Desktop.
Save 1eedaegon/2535436ade80b28e4d97f517c6434638 to your computer and use it in GitHub Desktop.
Enable hyper-v on windows 11 home and Use vagrant

How to use vagrant with hyper-v on Windows 11 home

  • Windows 11
  • Hyper-v
  • Vagrant

1. Install & enable hyper-v

Make and execute batch file install-and-enable-hyper-v.bat (Administrator)

pushd
%~dp0
dir /b %SystemRoot%\servicing\Packages\*Hyper-V*.mum >hv.txt
for /f %%i in ('findstr /i . hv.txt 2^>nul') do dism /online /norestart /add-package:%SystemRoot%\servicing\Packages\%%i
del hv.txt
Dism /online /enable-feature /featurename:Microsoft-Hyper-V -All /LimitAccess /ALL
pause
    1. Open the Settings app and go to Apps > Optional features.
    1. Scroll to the bottom and click More Windows features.
    1. Find Hyper-V on the list and enable it. You may have to restart afterwards.
    1. You should now be able to use Hyper-V to create and manage virtual machines.

2. Add default vagrant box(hyper-v)

If got error, remove folder: ./vagrant

> vagrant box add hashicorp/bionic64 --provider hyperv

==> box: Loading metadata for box 'hashicorp/bionic64'
    box: URL: https://vagrantcloud.com/hashicorp/bionic64
==> box: Adding box 'hashicorp/bionic64' (v1.0.282) for provider: hyperv
    box: Downloading: https://vagrantcloud.com/hashicorp/boxes/bionic64/versions/1.0.282/providers/hyperv.box                                           ==> box: Box download is resuming from prior download progress                  box:                                                                    ==> box: Successfully added box 'hashicorp/bionic64' (v1.0.282) for 'hyperv'!

> vagrant box list

hashicorp/bionic64 (hyperv, 1.0.282)

3. Vagrant up with auto setup

> vagrant up --provider hyperv

Bringing machine 'default' up with 'hyperv' provider...
==> default: Verifying Hyper-V is enabled...
==> default: Verifying Hyper-V is accessible...
==> default: Importing a Hyper-V instance
    default: Creating and registering the VM...
    default: Successfully imported VM
    default: Please choose a switch to attach to your Hyper-V instance.
    default: If none of these are appropriate, please open the Hyper-V manager
    default: to create a new virtual switch.
    default:
    default: 1) Default Switch
    default: 2) WSL
    default:
    default: What switch would you like to use? 1
    default: Configuring the VM...
    default: Setting VM Enhanced session transport type to disabled/default (VMBus)
==> default: Starting the machine...
==> default: Waiting for the machine to report its IP address...
    default: Timeout: 120 seconds
    default: IP: 172.20.122.195
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 172.20.122.195:22
    default: SSH username: vagrant
    default: SSH auth method: private key
    default:
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default:
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Preparing SMB shared folders...
    default: You will be asked for the username and password to use for the SMB
    default: folders shortly. Please use the proper username/password of your
    default: account.
    default:
SMB account process...

4. Check vm setup

> vagrant status

Current machine states:

default                   running (hyperv)

5. Connect to VM

> vagrant ssh

Welcome to Ubuntu 18.04.3 LTS (GNU/Linux 4.15.0-58-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Mon Oct 11 10:13:42 UTC 2021

  System load:  0.0               Processes:           88
  Usage of /:   2.4% of 61.31GB   Users logged in:     0
  Memory usage: 14%               IP address for eth0: 172.20.122.195
  Swap usage:   0%

 * Super-optimized for small spaces - read how we shrank the memory
   footprint of MicroK8s to make it the smallest full K8s around.

   https://ubuntu.com/blog/microk8s-memory-optimisation

0 packages can be updated.
0 updates are security updates.


vagrant@ubuntu-18:~$

etc. Windows10 Vagrantfile

# Vagrantfile
Vagrant.configure("2") do |config|
  config.vm.box = "gusztavvargadr/windows-10"
  config.vm.synced_folder "./shared", "/vagrant", type: "smb",
    smb_password: "admin", smb_username: "admin@admin.com"
end

ref

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