zaro

How to install Python on Raspberry Pi using terminal?

Published in Python Installation 2 mins read

Installing Python on a Raspberry Pi using the terminal is a straightforward process, often involving package management tools like apt. This guide outlines the general steps, assuming you're using a recent version of Raspberry Pi OS which typically comes with Python pre-installed. However, this provides instructions on building and installing a specific version from source.

Checking for Existing Python Installations

First, check if Python is already installed. Open a terminal and type:

python3 --version

or

python --version

This will display the Python version if it's installed. If Python 3 is installed, you likely don't need to do anything further for basic usage. However, if you want to build and install a specific version, continue to the next steps.

Building and Installing Python (Specific Version)

This assumes you want to install a specific Python version from source. This is less common for basic use, but useful if you need a particular version. This example builds and installs Python 3.10. You'll need to download the source code first (not covered here). Let's assume the source is extracted to /path/to/Python-3.10.x.

  1. Navigate to the Python source directory:

    cd /path/to/Python-3.10.x
  2. Configure the build:

    ./configure --enable-optimizations
    • --enable-optimizations: Enables profile guided optimization (PGO) which can improve performance. This step is optional.
  3. Build Python:

    make -j $(nproc)
    • make: Starts the build process.
    • -j $(nproc): Speeds up the build by using all available CPU cores.
  4. Install Python:

    sudo make altinstall
    • sudo: Requires administrator privileges.
    • make altinstall: Installs Python without overwriting the system's default Python installation. This is crucial to avoid breaking system tools. It installs it as python3.10. Using simply make install is highly discouraged.

Verify the Installation

After the installation, verify it by running:

python3.10 --version

This should display the version you just installed (e.g., Python 3.10.x). This confirms the installation was successful. If you encounter errors, double-check each step and ensure you have the necessary dependencies installed (e.g., build tools, zlib, etc.).