Saturday, September 20, 2025

WSL: Install Azure OpenAI SDK

 I have a pristine Ubuntu (Ubuntu-24.04) distro running on WSL (see: WSL: Keeping a Pristine (Greenfield) Version of Your Distro). The blog presents the steps required to install Azure OpenAI SDK on this distro.

First, ensure packages are up to date:

sudo apt update

Ensure Python is installed:

python3 --version

The standard distro for Ubuntu contains Python by default as shown by the message returned by python3 --version:

Python 3.12.3

Ubuntu 24.04 blocks installing Python packages system-wide with pip to protect system tools. This means using pip system-wide to install packages will fail with an “externally-managed-environment” error. The correct way is to use a virtual environment (venv), which is a private sandbox for Python and its packages.

To enable venv on Ubuntu, install the package:

sudo apt install -y python3-venv

To make use of venv, create and activate a virtual environment where the source command activates the virtual environment:

python3 -m venv ~/.venvs/azure
source ~/.venvs/azure/bin/activate

Within venv, pip can be upgraded:

pip install --upgrade pip

The azure-ai-openai package isn’t publicly available on PyPI, so install the OpenAI Python SDK:

pip install -U openai

The -U option above ensures the version installed is upgrade to the latest.” → “The -U option above ensures the package is upgraded to the latest version.

To test the install run python from inside the distro:

python3

Inside Python, paste the following to ensure access to the Azure OpenAI package:

from openai import AzureOpenAI
print("Import worked:", AzureOpenAI)

Once it runs successfully inside Python, exit Python using exit() or Ctrl-D.

Every time you access Azure OpenAI on this distro instance, the venv must be activated using the following:

source ~/.venvs/azure/bin/activate

To simplify, add the above command to ~/.bashrc..


WSL: Keeping a Pristine (Greenfield) Version of Your Distro

WSL has a limitation. Once you install a distro using wsl --install, you cannot just install a second copy of the same distro. The way to get around this is to install the latest distro, such as Ubuntu 24.04, and then make a backup of the pristine distro so that it can be duplicated later via import. It seems like something that should just be an option in wsl --install, but alas, it is not. 

In this article I will walk you through the steps required to create this pristine copy of a distro. I will also show you how to name your distro in WSL something besides Ubuntu or Ubuntu 24.04. 

To demonstrate a new hence pristine instance of a distro must be installed on WSL. This topic was covered in a previous blog WSL: Always Specify the Distro Version When Installing. Moving forward I will not be doing any work on this distro. See the following demarcated n boldface (courtesy of running wsl -l -v):

  NAME                    STATE           VERSION
* docker-desktop          Stopped         2
  Ubuntu-24.04            Stopped         2
  docker-desktop-data     Stopped         2

For example, on the Ubuntu-24.04 distro I will not install any Azure-related packages or Azure AI packages. Ubuntu-24.04 is my pristine/clean/greenfield distro, and any project-specific work will be done by making a copy.

Once a new distro is installed, I like updating the distro to use the latest packages by running:

sudo apt update
sudo apt upgrade -y

Once the packages are installed, clean up any unused packages by running:

sudo apt autoremove -y

On my personal machines I like disabling the sudo prompt, which was covered in this is blog, Ubuntu/Linux: Disable sudo Password Prompt. This means that each copy of the distro instance will also have sudo disabled.

Once the pristine/clean/greenfield distro is patched, make a backup of it by following the steps in WSL: Backing Up and Deleting a Distro, and obviously do not delete the current distro.

When I start a new project, such as my Azure AI Foundry work, I will create a new distro named for the project. It will be a suitable name like AzAiUbuntu-24.04, and I will follow the steps here to import an instance of the distro with the aforementioned name (see WSL: Restoring a Distro from a Backup).

To create (import) a project-specific distro I would use these parameters:

  • Distro AzZiUbuntu-24.04Host00
  • InstallLocation: C:\Users\jann\AppData\Local\wsl\AzAiUbuntu-24.04
  • FileName: C:\Backups\20250921Ubuntu-24.04.tar

Finding the default WSL distro folder path, C:\Users\jann\AppData\Local\wsl, was discussed in WSL: Backing Up and Deleting a Distro.

Using the aforementioned parameters, I can create a per-project WSL distro instance without modifying my pristine master copy:

wsl --import AzAiUbuntu-24.04 'C:\Users\jann\AppData\Local\wsl\AzAiUbuntu-24.04' 'C:\Backups\20250921Ubuntu-24.04.tar'

After the project specific distro instance is created (imported) the output from wsl -l -v is as follows where our newly imported/created VM is demarcated in bold:

  NAME                    STATE           VERSION
* docker-desktop          Stopped         2
  AzAiUbuntu-24.04        Stopped         2
  Ubuntu-24.04            Stopped         2
  docker-desktop-data     Stopped         2


WSL: Restoring a Distro from a Backup

This article discusses restoring a WSL distro from a backup TAR file. In a previous entry, the backup process for a WSL distro was presented: WSL: Backing Up and Deleting a Distro. Restoring from a backup is a bit more complicated because the wsl --import command requires that the "install location" be specified, but knowing where the default path used by WSL to install virtual disks (*.vhdx files) is important.

Microsoft documentation, Import any Linux distribution to use with WSL, presents the full wsl --import command as follows:

wsl.exe --import <Distro> <InstallLocation> <FileName> [Options]
Options:
    --version <Version>
    --vhd

To see the location where virtual disks are stored, run the following from a PowerShell console window:

$regKeyPath = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss'
Get-ChildItem $regKeyPath |
  Get-ItemProperty |
  Select-Object DistributionName, BasePath

An example of the output from the above command is as follows:

DistributionName    BasePath
----------------    --------
docker-desktop      \\?\C:\Users\jann\AppData\Local\Docker\wsl\distro
docker-desktop-data \\?\C:\Users\jann\AppData\Local\Docker\wsl\data
Ubuntu-24.04        C:\Users\jann\AppData\Local\wsl\{e3757a3a-1cb5-4208-b9c2-5f6f7490cda8}

From the above output, the default location used by WSL is:

C:\Users\jann\AppData\Local\wsl

Notice the GUID in the path, meaning WSL creates a random folder name — a GUID (e.g., {e3757a3a-1cb5-4208-b9c2-5f6f7490cda8}) — for the distro instance.

C:\Users\jann\AppData\Local\wsl\{e3757a3a-1cb5-4208-b9c2-5f6f7490cda8}

For the sake of example, the following InstallLocation could be used when importing an instance of Ubuntu-24.04:

C:\Users\jann\AppData\Local\wsl\Ubuntu-24.04Host00

The distro name must be unique so run the following comment to see the current distros installed.

wsl -l -v

An example of the output generated by wsl -l -v is as follows:

  NAME                   STATE           VERSION
* docker-desktop         Stopped         2
  docker-desktop-data    Stopped         2
  Ubuntu-24.04           Running         2

From the list of names, choosing Ubuntu-24.04 as the name for the imported distro would be invalid.

For our example, the following parameters will be used:

  • Distro Ubuntu-24.04Host00
  • InstallLocation: C:\Users\jann\AppData\Local\wsl\Ubuntu-24.04Host00
  • FileName: C:\Backups\20250921Ubuntu-24.04.tar

In the blog, WSL: Backing Up and Deleting a Distro, a backup TAR file named 20250921Ubuntu-22.04.tar was created. The steps described in the blog were used to create a backup of a clean Ubuntu-24.04 WSL distro and the full back up file was placed at:

C:\Backups\20250921Ubuntu-24.04.tar

The full command to import the back up is as follows:

wsl --import Ubuntu-24.04Host0 'C:\Users\jann\AppData\Local\wsl\Ubuntu-24.04Host00' 'C:\Backups\20250921Ubuntu-24.04.tar'

The import took tens of seconds compared to the export, which took minutes. Once complete, wsl --import displays status:

The operation completed successfully.

After import, always verify by running, wsl -l -v, which shows Ubuntu-24.04Host0 was created:

  NAME                   STATE           VERSION
* docker-desktop         Stopped         2
  Ubuntu-24.04Host0      Stopped         2
  docker-desktop-data    Stopped         2
  Ubuntu-24.04           Stopped         2

Friday, September 19, 2025

WSL: Backing Up and Deleting a Distro

Before deleting a WSL distro and , say, upgrading to a new version**,** it is often wise to back up the existing distro. This blog covers the steps to perform this task.

Step 1: List Installed Distros

From a PowerShell console run the following:

wsl -l -v

This will show you the name, version, and state of each distro. One of my WSL instances shows the following when wsl -l -v is run:

  NAME                   STATE           VERSION
* docker-desktop         Stopped         2
  Ubuntu-22.04           Stopped         2
  docker-desktop-data    Stopped         2
  Ubuntu-24.04           Running         2

For this example, the distro Ubuntu-22.04 will be backed up and then deleted.

Step 2: Export the Distro (Backup)

The wsl command’s --export option allows a backup of a distro to be saved to a file in TAR format. The command is as follows for my distro, Ubuntu-22.04:

wsl --export Ubuntu-22.04 C:\Backups\20250921Ubuntu-22.04.tar

Once initiated, the export operation will display a message such as the following:

Export in progress, this may take a few minutes.

Once completed, the export operation will display a message including the size of the exported TAR file:

Export in progress, this may take a few minutes. (1167 MB)

The backup TAR file can be imported later using the wsl command. The exporting process can take several minutes, unlike deleting a distro which is instantaneous.

Step 3: Delete the Distro

The distro is backed up, so it can safely be deleted as follows with wsl:

wsl --unregister Ubuntu-22.04

Although the command-line option is --unregister, it completely deletes the distro from WSL. No warning prompt will be given. The command just runs nearly instantly:

PS C:\blogs\wslscratchpad> wsl --unregister Ubuntu-22.04
Unregistering.
The operation completed successfully.



WSL: Always Specify the Distro Version When Installing

The distros on my WSL courtesy of running wsl -l -v from PowerShell are as follows:

  NAME                   STATE           VERSION
* Ubuntu                 Running         2 
  docker-desktop         Stopped         2 
  docker-desktop-data    Stopped         2

I know from experience that the named Ubuntu is actually: Ubuntu 22.04 LTS (Jammy Jellyfish). It would be helpful if the distro name actually contained information such as the version 22.04.

When I installed a few years ago, I used the following command, where the name Ubuntu means “install the latest distro”:

wsl --install -d Ubuntu

The problem with this generic/latest approach is that the distro is simply named Ubuntu. 

It makes more sense to install the distro by specifying the version, such as:

wsl --install -d Ubuntu-22.04
wsl --install -d Ubuntu-24.04

Running wsl -l -v again from PowerShell shows the distros with their version numbers:

  NAME                   STATE           VERSION
* docker-desktop         Stopped         2
  Ubuntu-22.04           Stopped         2
  docker-desktop-data    Stopped         2
  Ubuntu-24.04           Running         2


QED: Quod Erat Demonstrandum


Visual Studio: Finding Using Statements Added to Source Code by Visual Studio

In a previous post (Visual Studio: Disabling Add Using Statements on Paste), I showed that Visual Studio has a feature that adds using statements to C# files even when a project takes advantage of project-level global usings. And yes, this feature does turn itself back on (see below -- it's back):



Periodically, in a large project, I like searching for these superfluous using statements. The way to do this is to use a regular expression. From Visual Studio, select Find in Files, which is under Edit → Find and Replace → Find in Files (CTRL+SHIFT+F). In the search textbox, paste the following regular expression:

(?m)^using\s+

Make sure you select the "Use regular expressions" checkbox in the "Find and Replace" dialog (see below):


File types can be *.* or *.cs. Click on "Find All" and purge away.

Below is ChatGPT's breakdown of the regular expression,  (?m)^using\s+ :


Thursday, September 18, 2025

Windows 11: Updating WSL to the Latest Verson

In this vlog I will show you how to update your WSL (Windows Subsystem for Linux) on a Windows host.

I found myself using a backup laptop for a project. I ran the following to check the WSL version from a PowerShell terminal window running as administrator:

wsl --version

The output was as follows:

WSL version: 2.3.26.0
Kernel version: 5.15.167.4-1
WSLg version: 1.0.65
MSRDC version: 1.2.5620
Direct3D version: 1.611.1-81528511
DXCore version: 10.0.26100.1-240331-1435.ge-release
Windows version: 10.0.26100.6584

ChatGPT Pointed out my WSL was released November 2024: 

WSL 2.3.26 with kernel 5.15.167.4

The latest version is WSL 2.5.10 , released August 2025.

I ran the following from the PowerShell terminal:

wsl --update

Which installed WSL 2.6.1, so ChatGPT was incorrect saying 2.5.10 was the latest.

Out of caution I ran:

wsl --shutdown

The double-checked with:

wsl --version 

This showed I was up to date with WSL 2.6.1:

WSL version: 2.6.1.0
Kernel version: 6.6.87.2-1
WSLg version: 1.0.66
MSRDC version: 1.2.6353
Direct3D version: 1.611.1-81528511
DXCore version: 10.0.26100.1-240331-1435.ge-release
Windows version: 10.0.26100.6584