This guide provides step-by-step instructions for setting up a Python development environment on Ubuntu using the Windows Subsystem for Linux (WSL) on a Windows system, tailored for users interested in the UltraLinux project (https://ultralinux.org/). The UltraLinux project focuses on the port of Linux to SPARC processors, but this tutorial is designed for those using WSL to explore Python development in a Linux-like environment on Windows.
Prerequisites
- A Windows 10 or 11 system with WSL2 enabled.
- Basic familiarity with terminal commands.
- Internet access to download packages.
Step 1: Enable and Install WSL2
- Enable WSL2:
- Open PowerShell as Administrator and run:
wsl --install
- This command enables WSL and installs Ubuntu by default. If prompted, restart your computer.
- Alternatively, enable WSL manually via Settings:
- Go to Settings > Apps > Optional Features.
- Select Windows Subsystem for Linux, click OK, and restart.
- Open PowerShell as Administrator and run:
- Install Ubuntu:
- Open the Microsoft Store, search for “Ubuntu” (e.g., Ubuntu 22.04 LTS), and click Install.
- Once installed, launch Ubuntu from the Start menu. Set up a username and password when prompted.
- Set WSL2 as Default:
- In PowerShell, run:
wsl --set-default-version 2
- Confirm Ubuntu is using WSL2:
wsl -l -v
- In PowerShell, run:
Step 2: Update Ubuntu and Install Python
- Update Ubuntu:
- Open the Ubuntu terminal and update the package list:
sudo apt update && sudo apt upgrade -y
- Open the Ubuntu terminal and update the package list:
- Check Python Version:
- Ubuntu typically comes with Python 3 pre-installed. Verify with:
python3 --version
- If Python 3 is not installed or you need a specific version, install it:
sudo apt install python3 -y
- Ubuntu typically comes with Python 3 pre-installed. Verify with:
- Install pip:
- Ensure
pip
, the Python package manager, is installed:sudo apt install python3-pip -y
- Verify pip installation:
pip3 --version
- Ensure
- Install Development Tools:
- Install essential tools for a robust Python environment:
sudo apt install -y build-essential libssl-dev libffi-dev python3-dev
- Install essential tools for a robust Python environment:
Step 3: Set Up a Python Virtual Environment
Virtual environments isolate project dependencies, preventing conflicts with the system Python installation. This is especially useful for UltraLinux-related projects where specific package versions may be needed.
- Install
venv
:- Ensure the
venv
module is available:sudo apt install python3-venv -y
- Ensure the
- Create a Project Directory:
- Create a directory for your Python projects:
mkdir ~/python_projects cd ~/python_projects
- Create a directory for your Python projects:
- Create a Virtual Environment:
- Create a virtual environment named
ultralinux_env
:python3 -m venv ultralinux_env
- Create a virtual environment named
- Activate the Virtual Environment:
- Activate the environment:
source ultralinux_env/bin/activate
- Your terminal prompt should change to indicate the active environment (e.g.,
(ultralinux_env)
).
- Activate the environment:
- Install Packages in the Virtual Environment:
- With the environment activated, install packages using
pip
. For example, to install therequests
library:pip install requests
- With the environment activated, install packages using
- Deactivate the Virtual Environment:
- When done, deactivate the environment:
deactivate
- When done, deactivate the environment:
Step 4: Set Up Visual Studio Code (Optional)
For a more productive development experience, consider using Visual Studio Code (VS Code) with WSL.
- Install VS Code:
- Download and install VS Code from the official website or Microsoft Store.
- Install the Remote – WSL extension in VS Code:
- Open VS Code, go to Extensions (Ctrl+Shift+X), search for “Remote – WSL,” and install it.
- Open Your Project in VS Code:
- Navigate to your project directory in the Ubuntu terminal:
cd ~/python_projects
- Open VS Code in the current directory:
code .
- Navigate to your project directory in the Ubuntu terminal:
- Install Python Extension:
- In VS Code, install the Python extension from the Extensions marketplace for enhanced Python support (e.g., linting, debugging).
Step 5: Write and Run a Test Script
- Create a Test Script:
- In your project directory, create a file named
test_ultralinux.py
:nano test_ultralinux.py
- Add the following code:
import requests def test_ultralinux(): print("Testing UltraLinux environment setup") try: response = requests.get('https://ultralinux.org/') print(f"UltraLinux website status: {response.status_code}") except requests.exceptions.RequestException as e: print(f"Error accessing UltraLinux website: {e}") if __name__ == "__main__": test_ultralinux()
- In your project directory, create a file named
- Run the Script:
- Ensure the virtual environment is activated:
source ultralinux_env/bin/activate
- Run the script:
python3 test_ultralinux.py
- The script checks the status of the UltraLinux website, printing the HTTP status code (e.g., 200 for success).
- Ensure the virtual environment is activated:
Step 6: Additional Tips for UltraLinux Users
- SPARC Development: If you’re working on SPARC-related projects for UltraLinux, ensure your Python scripts are compatible with the target architecture. Test scripts in a virtual environment to avoid dependency conflicts.
- Mailing Lists: For UltraLinux-specific questions, check the mailing lists on https://ultralinux.org/ for community support.
- Version Management: If you need multiple Python versions, consider using
pyenv
:- Install
pyenv
:curl https://pyenv.run | bash
- Follow the setup instructions to add
pyenv
to your shell and install specific Python versions.
- Install
Conclusion
By following these steps, you’ve set up a Python development environment on Ubuntu via WSL, ready for UltraLinux-related projects or general Python development. This setup provides an isolated, flexible environment to explore Python programming while leveraging the Linux-like capabilities of WSL. For further details on UltraLinux, visit https://ultralinux.org/ and explore its resources, such as the FAQ and mailing lists.