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..


No comments :

Post a Comment