Skip to content

Instantly share code, notes, and snippets.

@kvietmeier
Last active June 22, 2024 17:13
Show Gist options
  • Save kvietmeier/3f296be759f201d7431cf3af4d53c2db to your computer and use it in GitHub Desktop.
Save kvietmeier/3f296be759f201d7431cf3af4d53c2db to your computer and use it in GitHub Desktop.
Setting up and using Vagrant on Windows 11

Using Vagrant with Windows 11 and VirtualBox

Vagrant is an open-source software product for building and maintaining portable virtual software development environments for hosted hypervisors on your workstation; VirtualBox, Hyper-V, Docker, VMware, libvirt/KVM. The application "Vagrant" is used to start and manage virtual machine instances and containers formatted and distributed as "boxes" which are the package format for Vagrant environments. They are prepackaged virtual machines and container images that can range from basic Linux images to fully installed and configured applications ready to start and use. With just a few simple commands you can startup any number of virtual machines/containers with networking, shared filesystems, authentication, and any post boot customizations you require.

It allows you to create complete development environments like Kubernetes clusters, Ceph clusters, or Docker containers withgout needing a cloud subscrtion or access to a lab.

I have examples from the simple to the complex in my GitHub repo



NOTE: I am porting this over from a Jive/Word doc and it isn't done yet.



Requirements for using Vagrant:

  • You will need to have a Hypervisor (Provider) installed, this document uses VirtualBox
  • Proxy Configuration: There are 2 installation tasks required if you are behind proxies:
    • Configure proxy environment variables
    • Install the Vagrant proxy plugin
  • A good IDE to edit Vagrantfiles (Visual Studio Code works great).
  • PuTTY or other SSH client (OpenSSH is ported to PowerShell).
  • Enough disk space for Vagrant Boxes – a 256GB or larger SSD is highly recommended.
  • For Windows - a modern console Windows Terminal

A note on Hypervisors for Windows - Hyper-V is still not a good option. There are too many challenges with the network stack, limited community support, and no support from Microsoft. Stick with VMware Workstation or VirtualBox.

Getting and installing the Vagrant software

Vagrant runs on Windows, OS X, and Linux - this document focuses on Vagrant for Windows.

Installation itself is straightforward, just use the defaults. The majority of the work is involved in getting your environment setup to use it effectively.

If you don’t want to use the download link and prefer a CLI based installation method Vagrant and VirtualBox can both be installed using winget (Recommended).

Example install using winget -
KV C:\Users\ksvietme\repos\gist> winget search vagrant
Name    Id                Version Source
-----------------------------------------
Vagrant Hashicorp.Vagrant 2.4.1   winget
KV C:\Users\ksvietme\repos> winget install Hashicorp.Vagrant
Found Vagrant [Hashicorp.Vagrant] Version 2.4.1
This application is licensed to you by its owner.
Microsoft is not responsible for, nor does it grant any licenses to, third-party packages.
Downloading https://releases.hashicorp.com/vagrant/2.4.1/vagrant_2.4.1_windows_amd64.msi
  ██████████████████████████████   258 MB /  258 MB
Successfully verified installer hash
Starting package install...
Successfully installed

KV C:\Users\ksvietme\repos>
KV C:\Users\karlv\repos> winget show Hashicorp.Vagrant
Found Vagrant [Hashicorp.Vagrant]
Version: 2.4.1
Publisher: HashiCorp
Publisher Url: https://www.hashicorp.com
Publisher Support Url: https://www.vagrantup.com/docs
Author: Mitchell Hashimoto
Description: Vagrant is a tool for building and distributing development environments.
Homepage: https://www.vagrantup.com
License: Business Source License 1.1
License Url: https://github.com/hashicorp/vagrant/blob/main/LICENSE
Privacy Url: https://www.hashicorp.com/privacy
Copyright: Copyright (c) 2024 HashiCorp, Inc.
Copyright Url: https://github.com/hashicorp/vagrant/blob/main/LICENSE
Documentation:
  Vagrant Documentation: https://developer.hashicorp.com/vagrant/docs
  Tutorials: https://developer.hashicorp.com/vagrant/tutorials
Tags:
  developing
  development
  virtualization
Installer:
  Installer Type: wix
  Installer Locale: en-US
  Installer Url: https://releases.hashicorp.com/vagrant/2.4.1/vagrant_2.4.1_windows_amd64.msi
  Installer SHA256: fc3806e071b7af2067eec5e8dfbe9f2dfe2c46139c5d682bb7defd7a2f9083e9
  Offline Distribution Supported: true


Setup and install VirtualBox

We cover this first before anything else because you will need VirtualBox setup properly to verify the Vagrant setup tasks. VirtualBox installation is straightforward and Vagrant will take care of most everything once it is setup but there are a few items to check off:

  1. Download VirtualBox - https://www.virtualbox.org/wiki/Downloads
  2. Download Extension Pack
  3. Install VirtualBox and Extension Packs
  4. Verify that vboxmanage is in the Windows path
  5. Install any required plugins

Note: Installing Virtualbox is outside the scope of this document – pick a method and run the appropriate installer.

Recommended - use winget

KV C:\Users\ksvietme\repos\gist> winget search virtualbox
Name                 Id                Version Source
------------------------------------------------------
Oracle VM VirtualBox Oracle.VirtualBox 7.0.18  winget

Set vboxmanage in $PATH so you can use the CLI from PowerShell terminals:

Setting the path is optional as you won't often need to run vboxmanage yourself, that is what Vagrant is for. But you may want to verify things are working or troubleshoot so it is handy to have it available without having to remember the path.

Path to vboxmanage:

C:\"Program Files"\Oracle\VirtualBox\vboxmanage
Set PATH env variable -
  • Add path temporarily

    PS C:\Users\ksvietme\Documents\Projects> $env:Path += ";C:\Program Files\Oracle\VirtualBox\"
  • Add it permanently (need to change a registry key)

    • View Current PATH:

      PS C:\> Get-Item Env:Path|fl
    • Save current PATH

      PS C:\> OrigPath=(Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name Path).Path
    • Append vboxmanage directory

      PS C:\ > Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH -Value ($OrigPath += ";C:\Program Files\Oracle\VirtualBox\") -Verbose -PassThru|fl

      (will need to reboot to use it)

  • Test the setting - there are 3 VMs running on this host and the default NAT network is configured:

    • List the VMs (vboxmanage list vms)

      PS C:\Users\ksvietme\Documents\Projects\vagrant> vboxmanage list vms
      "CentosAdminSystem" {4df1381a-2e3e-4999-b626-a3e88da0cca9}
      "Foobar1" {53ac0c8a-026b-4546-baba-04b72475878d}
      "centos" {14daf367-4f3b-404f-b90a-c65b21ec25ee}
      PS C:\Users\ksvietme\Documents\Projects\vagrant>
    • NAT Networks (vboxmanage natnetwork list)

      PS C:\Users\ksvietme\Documents\Projects\vagrant> vboxmanage natnetwork list
      NAT Networks:
      Name: NatNetwork
      Network: 10.0.2.0/24
      Gateway: 10.0.2.1
      IPv6: No
      Enabled: Yes
      1 network found
      PS C:\Users\ksvietme\Documents\Projects\vagrant>

Configuring Vagrant for Use

There are 4 things you will want to do in order make sure you have setup/configured your environment before starting:

  1. Create a "Project" directory for Vagrant project folders (for GitHub).
  2. Create/Update the global Vagrantfile.
  3. Setting/Enabling system proxies (if you are behind firewalls)
  4. Set any required environmmrnt variables - like changing the location of VAGRANT_HOME
  5. Install any needed/wanted plugins.

Project directories

Where you create your Project directory is entirely up to you and reflection of your own workflow/organization.
Though you may want to consider a few points:

  • Are you pushing your Vagrant environments up to GitHub?
  • A Vagrant "Environment" consists of a directory holding a Vagrantfile and any supporting files/folders.
  • You may want to reference a common set of provider scripts amd cloud-init files across environments.
  • Things like proxies can be set in a "global" Vagrantfile
  • You will probably want to keep them all under one common tree.

Example:

KV C:\Users\ksvietme\repos\vagrant> ll

    Directory:  C:\Users\ksvietme\repos\vagrant

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d--h--       10/15/2022  12:45 PM        1   .git
d-----       10/15/2022  12:45 PM        1   archive
d-----       10/15/2022  12:45 PM        1   azure
d-----       10/15/2022  12:45 PM        1   Hyper-V
d-----       10/15/2022  12:45 PM        1   scripts
d-----       10/15/2022  12:45 PM        1   virtualbox
-a----       10/15/2022  12:45 PM      997   .gitignore
-a----       10/15/2022  12:45 PM     4.21KB CONTRIBUTING.md
-a----       10/15/2022  12:45 PM     2.23KB PortsAndNetworks.txt
-a----       10/15/2022  12:45 PM     1.68KB README.md

Use a Global Vagrantfile

You can put a Vagrantfile in your home directory to set some global defaults like enabling corporate proxies, accessing SSH keys, and setting sane VM defaults.

File is in:

PS C:\Users\<username>\.vagrant.d>
KV C:\Users\karlv\.vagrant.d> ll
d-----        6/22/2024   9:00 AM        1   boxes
d-----        6/22/2024   9:00 AM        1   data
d-----        6/22/2024   9:00 AM        1   gems
d-----        6/22/2024   9:00 AM        1   insecure_private_keys
d-----        6/22/2024   9:00 AM        1   rgloader
d-----        6/22/2024   9:00 AM        1   tmp
-a----        6/22/2024   9:00 AM     1.64KB insecure_private_key
-a----        6/22/2024   9:00 AM        3   setup_version
-a----        6/22/2024   9:54 AM      895   Vagrantfile
Example contents
# -*- mode: ruby -*-
# vi: set ft=ruby :

# Alternate proxies
#config.proxy.http     = "http://proxy.foobar.com:911"
#config.proxy.https    = "https://proxy.foobar.com:912"

# Put global settings in here
Vagrant.configure("2") do |config|

  # Set some global variables like SSH Keys
  $adminvm_karlvkey = File.readlines("#{Dir.home}/repos/certs/adminvm_karlv_id_rsa.pub").first.strip
  $adminvm_rootkey = File.readlines("#{Dir.home}/repos/certs/adminvm_root_id_rsa.pub").first.strip
  $defaultvm_CPU="1"
  $defaultvm_MEM="512"

  # Proxies check to see if they are set
  if File.readlines("C:/Users/ksvietme/.setproxies").grep(/True/).any?
    config.proxy.http     = "http://proxy.foobar.com:911"
    config.proxy.https    = "https://proxy.foobar.com:912"
    config.proxy.no_proxy = "localhost,127.0.0.1,*.mylocaldomain.com,172.10.0.0/24,172.16.0.0/24"
  end
  
end  

Configure proxies if needed

Installing/configuring plugins



Need to add

  • Setting up plugins
  • Managing proxies
  • Downloading Vagrant boxes
  • Using Vagrant to start VMs
  • Vagrantfile syntax and customizing VMs with provider scripts (bash)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment