Installing Python in WSL (Windows Subsystem for Linux)

Installing Python in WSL (Windows Subsystem for Linux)

Ready to start coding with Python on your Windows machine? Using the Windows Subsystem for Linux (WSL) gives you the best of both worlds — the convenience of Windows and the power of a Linux environment. Let’s walk through how to set it all up.

What is WSL and Why Use It?

WSL lets you run a Linux distribution right inside Windows without the overhead of a virtual machine. This is perfect for Python development because many production servers run on Linux, and having a matching environment locally helps avoid “it works on my machine” issues. Tools like pip, venv, and many Python libraries often work more smoothly in Linux-like environments.

Prerequisites

Before you begin, make sure you’re running a supported version of Windows 10 or Windows 11. You’ll also need administrator access to enable WSL and install a Linux distribution.

Step-by-Step Installation Guide

Enable WSL

First, you need to enable the Windows Subsystem for Linux feature. Open PowerShell as an administrator and run:

wsl --install

This command enables the required optional components and installs Ubuntu by default. If you want a different distribution, you can specify it, for example:

wsl --install -d Debian

After the installation, restart your computer if prompted.

Set Up Your Linux Distribution

Once your system reboots, launch your installed Linux distribution from the Start Menu. You’ll be asked to create a username and password. This account will be your default user for the distribution and will have sudo privileges.

Update Package Lists

It’s always a good idea to update your package lists after a fresh install. In your WSL terminal, run:

sudo apt update && sudo apt upgrade -y

This ensures you have the latest versions of all pre-installed packages.

Install Python

Most Linux distributions come with Python pre-installed. To check if Python is already available, run:

python3 --version

You might see something like Python 3.10.12. If it’s not installed or you want a different version, you can install it using apt. For example, to install Python 3.11:

sudo apt install python3.11

You can also install pip, the Python package installer, if it isn’t already included:

sudo apt install python3-pip

Verify Your Installation

Confirm that everything is working correctly:

python3 --version
pip3 --version

Both commands should output version numbers without errors.

Setting Up a Virtual Environment

It’s a best practice to use virtual environments for your Python projects to manage dependencies. Here’s how to create one:

python3 -m venv my_project_env
source my_project_env/bin/activate

Your terminal prompt should now show the name of the activated environment. To deactivate it later, just run deactivate.

Common Python Versions in Linux Distributions

Different Linux distributions ship with different default Python versions. Here’s a quick reference:

Distribution Default Python Version
Ubuntu 22.04 LTS Python 3.10
Debian 11 Python 3.9
Fedora 38 Python 3.11

Note: You can always install additional versions using deadsnakes PPA (for Ubuntu) or compile from source.

Useful Tips and Tricks

  • Accessing Windows Files: Your Windows drives are automatically mounted under /mnt/ (e.g., /mnt/c/ for your C: drive). You can navigate to them directly from WSL.
  • Installing Multiple Python Versions: Use update-alternatives to manage multiple Python installations.
  • Using VS Code: Install the Remote - WSL extension in VS Code to develop directly in your WSL environment.

Installing a Specific Python Version

If you need a version not available in the default repositories, you can use a third-party repository like deadsnakes for Ubuntu:

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.12

Setting Default Python Version

To avoid typing python3 or pip3 every time, you can set up aliases in your ~/.bashrc file:

echo "alias python=python3" >> ~/.bashrc
echo "alias pip=pip3" >> ~/.bashrc
source ~/.bashrc

Now you can use python and pip directly.

Troubleshooting Common Issues

Sometimes things don’t go as planned. Here are a few common problems and their solutions:

  • WSL Not Installing: Ensure virtualization is enabled in your BIOS/UEFI settings.
  • Permission Denied Errors: Remember to use sudo for system-wide installs.
  • Pip Not Found: Install it using sudo apt install python3-pip.

If you encounter version conflicts or missing modules, double-check your installation steps and consider using a virtual environment.

With WSL and Python correctly installed, you’re all set to start building amazing projects in a robust development environment. Happy coding