|
| 1 | +# Custom Base Images |
| 2 | + |
| 3 | +This guide explains how to create custom base images for the Vagrant WSL2 provider. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The Vagrant WSL2 provider uses a caching system to speed up VM creation. When you first run `vagrant up`, the provider: |
| 8 | + |
| 9 | +1. Downloads/installs a clean WSL2 distribution (e.g., Ubuntu-24.04) |
| 10 | +2. Exports it to a cache directory: `~/.vagrant.d/wsl2-cache/` |
| 11 | +3. Creates project-specific VMs from this cached image |
| 12 | + |
| 13 | +This ensures every `vagrant up` starts with a clean, reproducible environment. |
| 14 | + |
| 15 | +## Why Create Custom Base Images? |
| 16 | + |
| 17 | +You might want to create a custom base image if you: |
| 18 | + |
| 19 | +- Need pre-installed packages in all your VMs (e.g., Docker, specific tools) |
| 20 | +- Want to apply custom configurations before VM creation |
| 21 | +- Need a specific distribution version not available via `wsl --install` |
| 22 | +- Want to optimize VM startup time by pre-installing common dependencies |
| 23 | + |
| 24 | +## Creating a Custom Base Image |
| 25 | + |
| 26 | +### Method 1: Manual Creation (Recommended) |
| 27 | + |
| 28 | +**Step 1: Create and customize a WSL distribution** |
| 29 | + |
| 30 | +```powershell |
| 31 | +# Install a fresh distribution |
| 32 | +wsl --install Ubuntu-24.04 --no-launch |
| 33 | +
|
| 34 | +# Launch it and install your custom packages |
| 35 | +wsl -d Ubuntu-24.04 |
| 36 | +
|
| 37 | +# Inside the distribution: |
| 38 | +sudo apt update |
| 39 | +sudo apt install -y build-essential git curl |
| 40 | +# ... install whatever you need ... |
| 41 | +
|
| 42 | +# Exit the distribution |
| 43 | +exit |
| 44 | +``` |
| 45 | + |
| 46 | +**Step 2: Export to cache directory** |
| 47 | + |
| 48 | +```powershell |
| 49 | +# Create cache directory if it doesn't exist |
| 50 | +New-Item -ItemType Directory -Force -Path "$env:USERPROFILE\.vagrant.d\wsl2-cache" |
| 51 | +
|
| 52 | +# Export your customized distribution |
| 53 | +wsl --export Ubuntu-24.04 "$env:USERPROFILE\.vagrant.d\wsl2-cache\Ubuntu-24.04-vagrant-base.tar" |
| 54 | +``` |
| 55 | + |
| 56 | +**Step 3: Clean up the original (optional)** |
| 57 | + |
| 58 | +```powershell |
| 59 | +# Remove the original distribution to save space |
| 60 | +wsl --unregister Ubuntu-24.04 |
| 61 | +``` |
| 62 | + |
| 63 | +**Step 4: Use in Vagrantfile** |
| 64 | + |
| 65 | +```ruby |
| 66 | +Vagrant.configure("2") do |config| |
| 67 | + config.vm.box = "Ubuntu-24.04" |
| 68 | + |
| 69 | + config.vm.provider "wsl2" do |wsl| |
| 70 | + # Vagrant will use your custom cached image! |
| 71 | + end |
| 72 | +end |
| 73 | +``` |
| 74 | + |
| 75 | +### Method 2: Using an Existing Distribution |
| 76 | + |
| 77 | +If you already have a WSL distribution that you've customized and want to use as a base: |
| 78 | + |
| 79 | +```powershell |
| 80 | +# Export your existing distribution to the cache |
| 81 | +wsl --export MyCustomDistro "$env:USERPROFILE\.vagrant.d\wsl2-cache\MyCustomDistro-vagrant-base.tar" |
| 82 | +``` |
| 83 | + |
| 84 | +Then in your Vagrantfile: |
| 85 | + |
| 86 | +```ruby |
| 87 | +Vagrant.configure("2") do |config| |
| 88 | + config.vm.box = "MyCustomDistro" |
| 89 | + |
| 90 | + config.vm.provider "wsl2" do |wsl| |
| 91 | + # Will use your custom cached image |
| 92 | + end |
| 93 | +end |
| 94 | +``` |
| 95 | + |
| 96 | +## Important Considerations |
| 97 | + |
| 98 | +### Cache Naming Convention |
| 99 | + |
| 100 | +The cache file **must** follow this naming pattern: |
| 101 | + |
| 102 | +``` |
| 103 | +<distribution-name>-vagrant-base.tar |
| 104 | +``` |
| 105 | + |
| 106 | +For example: |
| 107 | +- `Ubuntu-24.04-vagrant-base.tar` for box name `Ubuntu-24.04` |
| 108 | +- `Debian-vagrant-base.tar` for box name `Debian` |
| 109 | +- `MyCustomDistro-vagrant-base.tar` for box name `MyCustomDistro` |
| 110 | + |
| 111 | +### Keeping Images Clean |
| 112 | + |
| 113 | +**Best Practice**: Don't include user-specific data or temporary files in your base image. |
| 114 | + |
| 115 | +**What to avoid**: |
| 116 | +- User home directory modifications |
| 117 | +- Running services or daemons |
| 118 | +- Temporary files in `/tmp` |
| 119 | +- User-specific SSH keys or credentials |
| 120 | +- Project-specific code or data |
| 121 | + |
| 122 | +**What's okay**: |
| 123 | +- System-wide package installations |
| 124 | +- System configuration files |
| 125 | +- Pre-compiled tools or binaries |
| 126 | +- System-wide environment variables |
| 127 | + |
| 128 | +### Automatic Backup for Legacy Distributions |
| 129 | + |
| 130 | +If you have a legacy distribution (e.g., Ubuntu-20.04) already installed locally, the provider will automatically: |
| 131 | + |
| 132 | +1. Backup your existing distribution |
| 133 | +2. Install a fresh clean version |
| 134 | +3. Export it to cache |
| 135 | +4. Restore your original distribution |
| 136 | + |
| 137 | +This ensures you don't lose your existing work while still getting a clean cache. |
| 138 | + |
| 139 | +## Cache Location |
| 140 | + |
| 141 | +All cached base images are stored in: |
| 142 | + |
| 143 | +``` |
| 144 | +Windows: C:\Users\<username>\.vagrant.d\wsl2-cache\ |
| 145 | +``` |
| 146 | + |
| 147 | +You can manually manage these files: |
| 148 | + |
| 149 | +```powershell |
| 150 | +# List cached images |
| 151 | +Get-ChildItem "$env:USERPROFILE\.vagrant.d\wsl2-cache" |
| 152 | +
|
| 153 | +# Remove a specific cache |
| 154 | +Remove-Item "$env:USERPROFILE\.vagrant.d\wsl2-cache\Ubuntu-24.04-vagrant-base.tar" |
| 155 | +
|
| 156 | +# Remove all caches |
| 157 | +Remove-Item "$env:USERPROFILE\.vagrant.d\wsl2-cache\*" |
| 158 | +``` |
| 159 | + |
| 160 | +## Updating Cache |
| 161 | + |
| 162 | +To update a cached image with new packages or configurations: |
| 163 | + |
| 164 | +1. Delete the existing cache file |
| 165 | +2. Run `vagrant up` - the provider will recreate it |
| 166 | + |
| 167 | +OR manually: |
| 168 | + |
| 169 | +```powershell |
| 170 | +# Delete old cache |
| 171 | +Remove-Item "$env:USERPROFILE\.vagrant.d\wsl2-cache\Ubuntu-24.04-vagrant-base.tar" |
| 172 | +
|
| 173 | +# Create new custom image using Method 1 above |
| 174 | +# ... |
| 175 | +``` |
| 176 | + |
| 177 | +## Examples |
| 178 | + |
| 179 | +### Example 1: Docker Pre-installed |
| 180 | + |
| 181 | +```powershell |
| 182 | +# Install Ubuntu |
| 183 | +wsl --install Ubuntu-24.04 --no-launch |
| 184 | +wsl -d Ubuntu-24.04 |
| 185 | +
|
| 186 | +# Inside Ubuntu, install Docker |
| 187 | +curl -fsSL https://get.docker.com -o get-docker.sh |
| 188 | +sudo sh get-docker.sh |
| 189 | +exit |
| 190 | +
|
| 191 | +# Export to cache |
| 192 | +wsl --export Ubuntu-24.04 "$env:USERPROFILE\.vagrant.d\wsl2-cache\Ubuntu-24.04-vagrant-base.tar" |
| 193 | +
|
| 194 | +# Clean up |
| 195 | +wsl --unregister Ubuntu-24.04 |
| 196 | +``` |
| 197 | + |
| 198 | +### Example 2: Development Tools Pre-installed |
| 199 | + |
| 200 | +```powershell |
| 201 | +wsl --install Debian --no-launch |
| 202 | +wsl -d Debian |
| 203 | +
|
| 204 | +# Install development tools |
| 205 | +sudo apt update |
| 206 | +sudo apt install -y build-essential cmake ninja-build git |
| 207 | +exit |
| 208 | +
|
| 209 | +# Export to cache |
| 210 | +wsl --export Debian "$env:USERPROFILE\.vagrant.d\wsl2-cache\Debian-vagrant-base.tar" |
| 211 | +wsl --unregister Debian |
| 212 | +``` |
| 213 | + |
| 214 | +## Troubleshooting |
| 215 | + |
| 216 | +### Cache Not Being Used |
| 217 | + |
| 218 | +If Vagrant isn't using your custom cache: |
| 219 | + |
| 220 | +1. **Check the filename**: Must be `<box-name>-vagrant-base.tar` |
| 221 | +2. **Check the location**: Must be in `~/.vagrant.d/wsl2-cache/` |
| 222 | +3. **Check the box name**: In Vagrantfile, `config.vm.box` must match the cache filename |
| 223 | + |
| 224 | +### Large Cache Files |
| 225 | + |
| 226 | +If your cache files are very large: |
| 227 | + |
| 228 | +- Avoid including package caches: `sudo apt clean` |
| 229 | +- Remove temporary files before export |
| 230 | +- Consider using VHDX compression (future feature) |
| 231 | + |
| 232 | +### Permission Issues |
| 233 | + |
| 234 | +If you get permission errors when exporting: |
| 235 | + |
| 236 | +```powershell |
| 237 | +# Run PowerShell as Administrator |
| 238 | +wsl --export DistroName "path\to\cache.tar" |
| 239 | +``` |
| 240 | + |
| 241 | +## See Also |
| 242 | + |
| 243 | +- [WSL2 Command Cheat Sheet](wsl2-command-cheat-sheet.md) |
| 244 | +- [Vagrant Documentation](https://www.vagrantup.com/docs) |
| 245 | +- [WSL Documentation](https://learn.microsoft.com/en-us/windows/wsl/) |
0 commit comments