Install Python to the Windows Subsystem for Linux (WSL) using Ubuntu 22.04

Updated: June 8th, 2023, 01:41:34 IST
Published: June 7th, 2023
Install Python to the Windows Subsystem for Linux (WSL) using Ubuntu 22.04
Title: Install Python to the Windows Subsystem for Linux (WSL) using Ubuntu 22.04

The Windows Subsystem for Linux (WSL) is a new feature that lets developers use a Linux operating system within Windows. It provides access to a Linux distribution, like Ubuntu, directly on your Windows machine without needing to use a virtual machine. This makes it easier and faster for developers to work with Linux tools and software while still using their familiar Windows environment. Overall, it greatly improves the experience of using both Windows and Linux together.

Some restrictions and consideration for the Windows Subsystem for Linux (WSL)

  • Windows 10 Requirement: WSL is only available on Windows 10. Older versions of Windows don't support this feature.
  • No Graphical User Interface (GUI): WSL focuses on the command-line interface and lacks a full graphical desktop environment. It may not be suitable for GUI-dependent workflows.
  • Command-Line Environment: WSL is optimized for running Linux commands and tools from the Windows command prompt or integrated terminals like Visual Studio Code.
  • Linux Distribution Availability: WSL allows you to install and run various Linux distributions such as Ubuntu, Debian, and Fedora. Official documentation provides instructions for installing different distributions.
  • File System Integration: WSL integrates with the Windows file system, enabling access to files and directories from both Windows and Linux environments.

Running WSL on Windows 11: Install the Windows Subsystem for Linux on Windows 11

Yes, Windows 11 supports the Windows Subsystem for Linux (WSL). In reality, Windows 11 has an enhanced version of WSL known as WSL 2, which provides increased speed and compatibility with Linux programs.

WSL 2 in Windows 11 allows you to run a full Linux kernel alongside the Windows kernel, providing for a more seamless integration of Windows and Linux. This improved version of WSL supports more Linux distributions and performs better while performing Linux commands and running Linux applications.

If you have Windows 11, you may use WSL 2 by following the official Microsoft guidelines for installing and configuring WSL on Windows 11.

So, whether you are using Windows 10 or Windows 11, you can leverage the benefits of the Windows Subsystem for Linux to run Linux tools and applications within the Windows environment.

 

You can access your windows files at /mnt/c/Users/<user-name> from the WSL. You might want to consider adding export WINHOME="/mnt/c/Users/<user-name>" in your bash profile to facilitate access.

 
 

Install Python

To install Python on the Windows Subsystem for Linux (WSL) using Ubuntu 22.04, you can follow these steps:

1. Open the Ubuntu 22.04 WSL:

  • Open the Windows Start menu and search for "Ubuntu 22.04".
  • Click on the Ubuntu 22.04 app to launch the WSL.

2. Update the system:

Before installing Python, it's a good practice to update the system packages. Run the following commands in the WSL terminal:

sudo apt update && upgrade
 

3. Install Python

What's the default Python version in Ubuntu 22.04?

Currently, Ubuntu 22.04 comes with Python 3.10 pre-installed.

 

How to Check if Python3 is Installed?

Python3 comes by default with Ubuntu 22.04. To check the current version of "python3", run the following command in the termnal:

python3

OR

python3 --version
 

However, if you want to install a different Python version, you can use the deadsnakes PPA. Run the following commands to add the PPA and install Python 3.10:

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

After the installation is complete, you can verify the Python version by running:

python3.10 --version

Executing this command will display the installed Python version, confirming that Python 3.10 is successfully installed on your system.

When you type python3 in the terminal and press Enter, it typically opens the Python interpreter, where you can interactively write and execute Python code. After entering the python3 command, you will see the Python prompt, which looks like this:

python3

Python 3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
 

4. Set up Python environment (optional):

If you want to manage multiple Python versions or create virtual environments, you can use tools like pyenv or venv. Here's an example of installing pyenv:

Install prerequisites:

sudo apt install make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
 

Install pyenv:

curl https://pyenv.run | bash
 

Add pyenv to the PATH by adding the following lines to the end of your ~/.bashrc or ~/.bash_profile file:

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"

Restart your terminal or run source ~/.bashrc or source ~/.bash_profile to apply the changes.

Install Python versions using pyenv:

pyenv install 3.9.7
pyenv install 3.10.1

Set a global Python version or create a virtual environment:

pyenv global 3.10.1  # Set global Python version
pyenv virtualenv 3.10.1 myenv  # Create a virtual environment
pyenv activate myenv  # Activate the virtual environment

These steps are optional but can be useful if you want more control over your Python environment.

That's it! You have successfully installed Python on the WSL using Ubuntu 22.04. You can now use Python and its packages within your WSL environment.

Create a virtual environment in Python using venv

The venv module in Python allows you to create lightweight "virtual environments." Each virtual environment is like a separate space where you can install Python packages independently from your main Python installation. It isolates the packages in the virtual environment, so only the ones you install there are available.

When you activate a virtual environment, tools like pip automatically install packages into that environment without needing any extra instructions. This means you can easily manage different sets of packages for different projects without them conflicting with each other.

Creating Virtual environment

To create a virtual environment, you can use the venv command. Simply open your command prompt or terminal and run the following command:

python -m venv /path/to/new/virtual/environment

Replace /path/to/new/virtual/environment with the desired location where you want to create the virtual environment.

This command will create a new virtual environment at the specified location. You can choose any directory or path on your system. Once created, you can activate the virtual environment to start using it for your Python projects.

To activate a virtual environment, you need to execute the appropriate activation command based on your operating system. Here are the commands for different platforms:

For Windows:

\path\to\new\virtual\environment\Scripts\activate
 

For macOS and Linux:

source /path/to/new/virtual/environment/bin/activate
 

Replace \path\to\new\virtual\environment or /path/to/new/virtual/environment with the actual path where you created your virtual environment.

After running the activation command, you will notice that your command prompt or terminal prompt changes, indicating that you are now working within the virtual environment. Any packages you install or commands you run will be specific to that environment.

Remember, it's important to activate the virtual environment before working on your project to ensure that you are using the correct set of dependencies and configurations.