Setting Up a Python Environment on Ubuntu via WSL for UltraLinux

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

  1. 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.
  2. 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.
  3. Set WSL2 as Default:
    • In PowerShell, run:
      wsl --set-default-version 2
      
    • Confirm Ubuntu is using WSL2:
      wsl -l -v
      

Step 2: Update Ubuntu and Install Python

  1. Update Ubuntu:
    • Open the Ubuntu terminal and update the package list:
      sudo apt update && sudo apt upgrade -y
      
  2. 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
      
  3. Install pip:
    • Ensure pip, the Python package manager, is installed:
      sudo apt install python3-pip -y
      
    • Verify pip installation:
      pip3 --version
      
  4. Install Development Tools:
    • Install essential tools for a robust Python environment:
      sudo apt install -y build-essential libssl-dev libffi-dev python3-dev
      

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.

  1. Install venv:
    • Ensure the venv module is available:
      sudo apt install python3-venv -y
      
  2. Create a Project Directory:
    • Create a directory for your Python projects:
      mkdir ~/python_projects
      cd ~/python_projects
      
  3. Create a Virtual Environment:
    • Create a virtual environment named ultralinux_env:
      python3 -m venv ultralinux_env
      
  4. 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)).
  5. Install Packages in the Virtual Environment:
    • With the environment activated, install packages using pip. For example, to install the requests library:
      pip install requests
      
  6. Deactivate the Virtual Environment:
    • When done, deactivate the environment:
      deactivate
      

Step 4: Set Up Visual Studio Code (Optional)

For a more productive development experience, consider using Visual Studio Code (VS Code) with WSL.

  1. 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.
  2. 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 .
      
  3. 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

  1. 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()
      
  2. 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).

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.

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.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top