-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall-wsl.ps1
More file actions
225 lines (180 loc) · 7.73 KB
/
install-wsl.ps1
File metadata and controls
225 lines (180 loc) · 7.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# WSL2 Installation Helper for Windows
# Run this from Windows PowerShell to set up WSL2 and configure .wslconfig
#Requires -RunAsAdministrator
param(
[switch]$Force,
[switch]$SkipWSLInstall,
[switch]$SkipDistro
)
$ErrorActionPreference = "Stop"
function Write-ColorOutput {
param(
[string]$Message,
[string]$Color = "White"
)
Write-Host $Message -ForegroundColor $Color
}
function Write-Section {
param([string]$Title)
Write-Host ""
Write-Host "═══════════════════════════════════════════════════════════════" -ForegroundColor Cyan
Write-Host " $Title" -ForegroundColor Cyan
Write-Host "═══════════════════════════════════════════════════════════════" -ForegroundColor Cyan
Write-Host ""
}
function Test-WSLInstalled {
try {
$null = wsl --status 2>&1
return $true
}
catch {
return $false
}
}
function Enable-WSLFeatures {
Write-Section "Enabling WSL Features"
Write-ColorOutput "Enabling WSL feature..." -ForegroundColor Yellow
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
Write-ColorOutput "Enabling Virtual Machine Platform..." -ForegroundColor Yellow
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
Write-ColorOutput "WSL features enabled successfully!" -ForegroundColor Green
}
function Set-WSLDefaultVersion {
Write-Section "Setting WSL 2 as Default"
wsl --set-default-version 2
Write-ColorOutput "WSL 2 set as default version." -ForegroundColor Green
}
function Install-Ubuntu24 {
Write-Section "Installing Ubuntu 24.04 LTS"
Write-ColorOutput "Checking for Ubuntu 24.04 installation..." -ForegroundColor Yellow
$wslList = wsl --list --verbose 2>&1
if ($wslList -match "Ubuntu-24\.04") {
Write-ColorOutput "Ubuntu 24.04 is already installed." -ForegroundColor Green
return
}
Write-ColorOutput "Installing Ubuntu 24.04 LTS..." -ForegroundColor Yellow
Write-ColorOutput "This will open the Microsoft Store. Please complete the installation there." -ForegroundColor White
Start-Process "ms-windows-store://pdp/?productid=9PN20MSR04DW"
Write-ColorOutput "Press Enter after you've completed the Ubuntu installation in the Store..." -ForegroundColor Yellow
Read-Host
}
function Copy-WSLConfig {
Write-Section "Configuring .wslconfig"
$scriptDir = Split-Path -Parent $PSScriptRoot
$wslconfigSource = Join-Path $scriptDir "config\wslconfig"
$wslconfigDest = "$env:USERPROFILE\.wslconfig"
if ((Test-Path $wslconfigDest) -and !$Force) {
Write-ColorOutput ".wslconfig already exists at $wslconfigDest" -ForegroundColor Yellow
$response = Read-Host "Overwrite? (y/N)"
if ($response -ne 'y' -and $response -ne 'Y') {
Write-ColorOutput "Skipping .wslconfig configuration." -ForegroundColor Yellow
return
}
}
if (Test-Path $wslconfigSource) {
Copy-Item $wslconfigSource $wslconfigDest -Force
Write-ColorOutput ".wslconfig installed to $wslconfigDest" -ForegroundColor Green
Write-ColorOutput "Restart WSL for changes to take effect: wsl --shutdown" -ForegroundColor Yellow
}
else {
Write-ColorOutput "Warning: config/wslconfig not found at $wslconfigSource" -ForegroundColor Red
}
}
function Get-SystemMemory {
$cs = Get-CimInstance -ClassName Win32_ComputerSystem
return [math]::Round($cs.TotalPhysicalMemory / 1GB)
}
function Get-SystemProcessors {
return [Environment]::ProcessorCount
}
function Optimize-WSLConfig {
Write-Section "Optimizing .wslconfig for Your System"
$memoryGB = Get-SystemMemory
$processors = Get-SystemProcessors
Write-ColorOutput "System Memory: $memoryGB GB" -ForegroundColor Cyan
Write-ColorOutput "Logical Processors: $processors" -ForegroundColor Cyan
# Calculate optimal settings
$wslMemory = if ($memoryGB -ge 32) { "16GB" }
elseif ($memoryGB -ge 16) { "12GB" }
elseif ($memoryGB -ge 8) { "6GB" }
else { "4GB" }
$wslProcessors = if ($processors -ge 16) { 8 }
elseif ($processors -ge 8) { 4 }
else { 2 }
$wslSwap = if ($memoryGB -ge 32) { "4GB" }
elseif ($memoryGB -ge 16) { "3GB" }
else { "2GB" }
Write-ColorOutput "Recommended settings:" -ForegroundColor Yellow
Write-ColorOutput " memory: $wslMemory" -ForegroundColor White
Write-ColorOutput " processors: $wslProcessors" -ForegroundColor White
Write-ColorOutput " swap: $wslSwap" -ForegroundColor White
$scriptDir = Split-Path -Parent $PSScriptRoot
$wslconfigDest = "$env:USERPROFILE\.wslconfig"
# Create optimized config
$configContent = @"
[wsl2]
memory=$wslMemory
processors=$wslProcessors
swap=$wslSwap
localhostForwarding=true
[experimental]
autoMemoryReclaim=gradual
sparseVhd=true
"@
Set-Content -Path $wslconfigDest -Value $configContent -Force
Write-ColorOutput "Optimized .wslconfig created!" -ForegroundColor Green
}
function Show-CompletionMessage {
Write-Section "Setup Complete!"
Write-ColorOutput "Next steps:" -ForegroundColor Green
Write-Host "1. Restart WSL to apply .wslconfig changes:"
Write-Host " wsl --shutdown"
Write-Host " wsl"
Write-Host ""
Write-Host "2. From inside WSL2 Ubuntu, navigate to the project and run:"
Write-Host " cd ~/wsl2-migration # or wherever you cloned it"
Write-Host " ./bootstrap.sh"
Write-Host ""
}
# Main execution
function Main {
Write-Host ""
Write-Host "╔═══════════════════════════════════════════════════════════════╗" -ForegroundColor Cyan
Write-Host "║ ║" -ForegroundColor Cyan
Write-Host "║ WSL2 Installation Helper (Windows Side) ║" -ForegroundColor Cyan
Write-Host "║ ║" -ForegroundColor Cyan
Write-Host "╚═══════════════════════════════════════════════════════════════╝" -ForegroundColor Cyan
Write-Host ""
# Check if running as Administrator
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if (!$isAdmin) {
Write-ColorOutput "This script requires Administrator privileges." -ForegroundColor Red
Write-ColorOutput "Please right-click PowerShell and select 'Run as Administrator'." -ForegroundColor Yellow
exit 1
}
# Enable WSL features if not already installed
if (!$SkipWSLInstall) {
if (Test-WSLInstalled) {
Write-ColorOutput "WSL is already installed." -ForegroundColor Green
}
else {
Enable-WSLFeatures
Write-ColorOutput "Please restart your computer, then run this script again with -SkipWSLInstall" -ForegroundColor Yellow
exit 0
fi
Set-WSLDefaultVersion
}
# Install Ubuntu distro
if (!$SkipDistro) {
Install-Ubuntu24
}
# Configure .wslconfig
if ($Force) {
Optimize-WSLConfig
}
else {
Copy-WSLConfig
}
Show-CompletionMessage
}
Main