Skip to content

Instantly share code, notes, and snippets.

@t-mat
Created April 27, 2026 02:58
Show Gist options
  • Select an option

  • Save t-mat/6e00387fd74afaefeb6b7288498cf7f2 to your computer and use it in GitHub Desktop.

Select an option

Save t-mat/6e00387fd74afaefeb6b7288498cf7f2 to your computer and use it in GitHub Desktop.
Windows 11 batch file which automatically downloads, imports, invokes Ubuntu 26.04
@echo off
rem Windows 11 batch file which automatically downloads, imports, invokes Ubuntu 26.04
setlocal enabledelayedexpansion
set "UBUNTU_VER=26.04"
set "UBUNTU_COD=resolute"
set "UBUNTU_IMG=ubuntu-!UBUNTU_VER!-wsl-amd64.wsl"
set "UBUNTU_URL=https://releases.ubuntu.com/!UBUNTU_COD!/!UBUNTU_IMG!"
set "UBUNTU_NAM=public-documents-ubuntu-!UBUNTU_VER!"
set "UBUNTU_DIR=%PUBLIC%\Documents\!UBUNTU_NAM!"
rem Required WSL version (for new tar-based .wsl format)
set "REQ_VER=2.4.10"
rem Check WSL
where wsl.exe >nul 2>&1 || ( echo WSL is not installed. & exit /b 1 )
rem Fast path: skip version check if instance is already registered
rem Check existing WSL instance and launch
powershell -NoProfile -Command "[Console]::OutputEncoding=[System.Text.Encoding]::Unicode; if (((wsl.exe -l -q) | %% {$_.Trim()}) -contains '%UBUNTU_NAM%') { exit 0 } else { exit 1 }"
if not errorlevel 1 (
wsl -d !UBUNTU_NAM! %*
exit /b !errorlevel!
)
rem Check WSL 1
wsl.exe --version >nul 2>&1 || ( echo WSL is too old. Run "wsl --update". & exit /b 1 )
rem Check WSL 2 - version number
powershell -NoProfile -Command "[Console]::OutputEncoding=[System.Text.Encoding]::Unicode; $o=(wsl.exe --version) -join [char]10; if($o -match '(\d+)\.(\d+)\.(\d+)'){$c=[Version]($Matches[1]+'.'+$Matches[2]+'.'+$Matches[3]); if($c -lt [Version]'%REQ_VER%'){exit 1} else {exit 0}} else {exit 3}"
if errorlevel 3 ( echo Failed to parse WSL version. & exit /b 1 )
if errorlevel 1 ( echo WSL is older than !REQ_VER!. Run "wsl --update". & exit /b 1 )
rem mkdir
if not exist "!UBUNTU_DIR!" (
mkdir "!UBUNTU_DIR!" || ( echo Failed to create !UBUNTU_DIR!. & exit /b 1 )
)
rem Download
if not exist "!UBUNTU_DIR!\!UBUNTU_IMG!" (
echo Downloading !UBUNTU_URL! ...
curl.exe -fL -# -o "!UBUNTU_DIR!\!UBUNTU_IMG!" "!UBUNTU_URL!"
if errorlevel 1 (
echo Failed to download !UBUNTU_URL!.
del /q "!UBUNTU_DIR!\!UBUNTU_IMG!" 2>nul
exit /b 1
)
)
rem Import
echo Importing !UBUNTU_IMG! to !UBUNTU_NAM! ...
wsl --import "!UBUNTU_NAM!" "!UBUNTU_DIR!" "!UBUNTU_DIR!\!UBUNTU_IMG!"
if errorlevel 1 (
echo Failed to import !UBUNTU_IMG!.
exit /b 1
)
rem Launch
wsl -d !UBUNTU_NAM! %*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment