Skip to content

Instantly share code, notes, and snippets.

@pyhedgehog
Created July 1, 2026 12:59
Show Gist options
  • Select an option

  • Save pyhedgehog/00e57609e04e31aaf733e9d1277762d2 to your computer and use it in GitHub Desktop.

Select an option

Save pyhedgehog/00e57609e04e31aaf733e9d1277762d2 to your computer and use it in GitHub Desktop.
Script to install latest Alpine release into WSL2.

installwslalpine.cmd

Script to install latest Alpine release into WSL2.

Usage

  1. Install WSL2.
  2. Copy to directory where you want distro to live (where will live ext4.vhdx file).
  3. (Optional) Create init.sh to be run inside distro after creation (i.e. before user creation).
  4. (Optional) Create user.sh to be run inside distro after user creation.
  5. Run installwslalpine.cmd.

Drawbacks

There are issues ignored in this script:

  1. No hash check of downloaded release.
  2. Only some easiest sanity checks (curl.exe, clash of distro name, good username).
@echo off
set WSL_UTF8=1
rem echo %%~dp0=%~dp0
call :target "%~dp0"
rem echo target=%target%
call :targetname "%target%"
rem echo targetname=%targetname%
call :where curl.exe
if "%where%"=="" goto fail_curl
for /f "usebackq" %%i in (`echo %targetname%^|findstr /x /r "^[a-z_][a-z0-9_]*$"`) do set check_targetname=%%i
if not "%targetname%"=="%check_targetname%" goto fail_targetname
for /f "usebackq" %%i in (`wsl -l -q^|findstr /x "%targetname%"`) do goto fail_distroclash
set filename=
echo Looking for latest release...
curl -so "%target%\latest-releases.yaml" https://dl-cdn.alpinelinux.org/alpine/latest-stable/releases/x86_64/latest-releases.yaml
if errorlevel 1 goto fail_download
for /f "usebackq tokens=2" %%i in (`findstr /r ".*minirootfs.*\.tar\.gz" "%target%\latest-releases.yaml"`) do set filename=%%i&goto found_release
:fail_release
echo Error parsing latest-releases.yaml
goto :EOF
:fail_curl
echo Can't find "curl.exe".
goto :EOF
:fail_targetname
echo Can't create WSL distribution "%targetname%". Invalid identifier.
goto :EOF
:fail_distroclash
@echo on
wsl -l -v
@echo off
echo Can't create WSL distribution "%targetname%". Already exists.
echo To delete use: wslconfig /u alpinecosmic
echo NB: This will remove ext4.vhdx (i.e. all userspace data).
goto :EOF
:fail_download
echo Error downloading "https://dl-cdn.alpinelinux.org/alpine/latest-stable/releases/x86_64/latest-releases.yaml". Either you have connectivity problem, or have no curl.exe installed in PATH.
goto :EOF
:found_release
rem echo Downloading release %filename%...
curl -# -o "%target%\%filename%" https://dl-cdn.alpinelinux.org/alpine/latest-stable/releases/x86_64/%filename%
echo Ready to install WSL distribution "%targetname%" in directory "%target%". Press Ctrl-C (and answer Y) to abort.
pause
echo Installing...
@echo on
wsl -l -v
wsl --import %targetname% %target% %target%\%filename% --version 2
wsl -l -v
@echo off
for /f "usebackq" %%i in (`wsl -l -q^|findstr /x "%targetname%"`) do goto found_distro
echo Seems failed to install WSL distribution "%targetname%".
goto :EOF
:found_distro
@echo on
wsl -d %targetname% -u root apk add sudo
wsl -d %targetname% -u root sh -c "echo '%wheel ALL=(ALL:ALL) NOPASSWD: ALL'>/etc/sudoers.d/99_wheel"
@echo off
if not exist %target%\init.sh goto skip_initsh
set saveCD=%CD%
@echo on
cd %target%
wsl -d %targetname% sh -x init.sh
@echo off
cd %saveCD%
:skip_initsh
for /f "usebackq" %%i in (`echo %username%^|findstr /x /r "^[a-z_][a-z0-9_-]*$"`) do set check_username=%%i
if "%username%"=="%check_username%" goto ok_username
@echo on
@echo off
echo Username "%username%" doesn't like an identifier. Skipping creation of user inside WSL distribution %targetname%.
goto :EOF
:ok_username
@echo on
wsl -d %targetname% adduser -D %username%
wsl -d %targetname% adduser %username% wheel
wsl --manage %targetname% --set-default-user %username%
wsl -l -v
@echo off
if not exist %target%\user.sh goto skip_usersh
set saveCD=%CD%
@echo on
cd %target%
wsl -d %targetname% sh -x user.sh
@echo off
cd %saveCD%
:skip_usersh
goto :EOF
:target
set saveCD=%CD%
cd %1
set target=%CD%
cd %saveCD%
goto :EOF
:targetname
set targetname=%~n1
goto :EOF
:where
set where=%~$PATH:1
rem for /f "usebackq" %%i in (`where "%1"`) do set where=%%i&goto got_where
rem :got_where
goto :EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment