Installing Python and Keras on Ubuntu via WSL for UltraLinux

This guide provides step-by-step instructions for installing Python and the Keras deep learning framework on Ubuntu using the Windows Subsystem for Linux (WSL) on a Windows system, tailored for users interested in the UltraLinux project (https://ultralinux.org/). While UltraLinux focuses on Linux for SPARC processors, this tutorial enables you to set up a Keras development environment in a Linux-like setup on Windows using WSL.

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 enables WSL and installs Ubuntu by default. Restart your computer if prompted.
    • Alternatively, enable WSL manually:
      • Go to Settings > Apps > Optional Features.
      • Select Windows Subsystem for Linux, click OK, and restart.
  2. Install Ubuntu:
    • From the Microsoft Store, search for “Ubuntu” (e.g., Ubuntu 22.04 LTS) and click Install.
    • Launch Ubuntu from the Start menu and set up a username and password when prompted.
  3. Set WSL2 as Default:
    • In PowerShell, run:
      wsl --set-default-version 2
      
    • Verify 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. Install Python:
    • Ubuntu typically includes Python 3. Verify with:
      python3 --version
      
    • If not installed or you need a specific version, install it:
      sudo apt install python3 -y
      
  3. Install pip:
    • Install pip, the Python package manager:
      sudo apt install python3-pip -y
      
    • Verify pip:
      pip3 --version
      
  4. Install Development Tools:
    • Install essential tools for Python and Keras:
      sudo apt install -y build-essential libssl-dev libffi-dev python3-dev
      

Step 3: Set Up a Python Virtual Environment

Using a virtual environment ensures Keras and its dependencies don’t conflict with system packages, which is useful for UltraLinux-related projects.

  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 Keras projects:
      mkdir ~/keras_projects
      cd ~/keras_projects
      
  3. Create a Virtual Environment:
    • Create a virtual environment named keras_env:
      python3 -m venv keras_env
      
  4. Activate the Virtual Environment:
    • Activate the environment:
      source keras_env/bin/activate
      
    • Your terminal prompt should change to indicate the active environment (e.g., (keras_env)).

Step 4: Install Keras and Dependencies

  1. Install TensorFlow:
    • Keras is included in TensorFlow as tf.keras. Install TensorFlow, which includes Keras:
      pip install tensorflow
      
    • This installs TensorFlow and Keras compatible with your Python version. Verify with:
      python -c "import tensorflow as tf; print(tf.keras.__version__)"
      
  2. Install Additional Dependencies:
    • For numerical computations and data processing, install NumPy and other useful libraries:
      pip install numpy pandas matplotlib
      

Step 5: Set Up Visual Studio Code (Optional)

For a better development experience, use 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, go to Extensions (Ctrl+Shift+X), search for “Remote – WSL,” and install.
  2. Open Your Project in VS Code:
    • Navigate to your project directory:
      cd ~/keras_projects
      
    • Open VS Code:
      code .
      
  3. Install Python Extension:
    • Install the Python extension in VS Code for features like linting and debugging.

Step 6: Write and Run a Test Keras Script

  1. Create a Test Script:
    • In your project directory, create a file named test_keras.py:
      nano test_keras.py
      
    • Add the following code to test Keras with a simple neural network:
      import tensorflow as tf
      from tensorflow import keras
      import numpy as np
      
      def test_keras():
          print("Testing Keras setup on UltraLinux environment")
          # Create a simple dataset
          x_train = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
          y_train = np.array([0, 1, 1, 0])
      
          # Build a simple neural network
          model = keras.Sequential([
              keras.layers.Dense(units=4, activation='relu', input_shape=(2,)),
              keras.layers.Dense(units=1, activation='sigmoid')
          ])
      
          # Compile the model
          model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
      
          # Train the model
          model.fit(x_train, y_train, epochs=100, verbose=0)
          print("Model training complete")
      
          # Test the model
          predictions = model.predict(x_train)
          print("Predictions:", predictions.flatten())
      
      if __name__ == "__main__":
          test_keras()
      
  2. Run the Script:
    • Ensure the virtual environment is activated:
      source keras_env/bin/activate
      
    • Run the script:
      python3 test_keras.py
      
    • The script trains a simple neural network on a toy dataset (XOR problem) and prints predictions.

Step 7: Additional Tips for UltraLinux Users

  • SPARC Compatibility: If you’re targeting SPARC hardware for UltraLinux, note that TensorFlow/Keras may not be fully optimized for SPARC. Test scripts in WSL first, and consult UltraLinux mailing lists (https://ultralinux.org/) for SPARC-specific advice.
  • GPU Support: WSL2 supports GPU acceleration for TensorFlow if you have an NVIDIA GPU and CUDA-enabled drivers. Install the necessary drivers and follow TensorFlow’s GPU setup guide for WSL.
  • Version Management: To manage multiple Python versions, use pyenv:
    • Install pyenv:
      curl https://pyenv.run | bash
      
    • Follow setup instructions to install specific Python versions compatible with TensorFlow/Keras.
  • Community Support: Visit https://ultralinux.org/ for UltraLinux-specific resources, such as the FAQ or mailing lists, to troubleshoot issues or connect with the community.

Conclusion

You’ve now set up a Python environment with Keras on Ubuntu via WSL, ready for deep learning projects or UltraLinux-related development. This setup provides an isolated environment for experimenting with Keras while leveraging WSL’s Linux-like capabilities. For more on UltraLinux, explore https://ultralinux.org/ for SPARC-specific resources and community support.

Leave a Comment

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

Scroll to Top