Monday, February 16, 2026

Install Python on Ubuntu

 

Python 3 is
pre-installed on all modern Ubuntu versions
. You should not remove or change the system's default Python version as it can break core system tools. For most use cases, you just need to install additional modules like pip and venv or a specific Python version alongside the system's default.
 
Using the Pre-installed Python 3
 
Open your terminal (Ctrl + Alt + T) and follow these steps:
  1. Update package lists:
    bash
    sudo apt update
    
  2. Verify Python 3 is installed:
    bash
    python3 --version
    
    This command will display the installed version (e.g., Python 3.12.x on Ubuntu 24.04).
  3. Install pip (Python package manager): This allows you to install other Python packages from the Python Package Index (PyPI).
    bash
    sudo apt install python3-pip
    
  4. Install venv (virtual environment module): This is crucial for creating isolated environments for your projects to manage dependencies without affecting the system Python.
    bash
    sudo apt install python3-venv
    
  5. Create and activate a virtual environment:
    • Create an environment in your project directory:
      bash
      python3 -m venv my_project_env
      
    • Activate the environment:
      bash
      source my_project_env/bin/activate
      
    Your terminal prompt will change to indicate you are in the virtual environment. You can now use python and pip commands within this environment (instead of python3 and pip3).
 
Installing a Specific or Newer Python Version
 
 If you need a different version not available in the default repositories, the recommended way is to use the deadsnakes PPA (Personal Package Archive).
  1. Add the deadsnakes PPA:
    bash
    sudo add-apt-repository ppa:deadsnakes/ppa
    sudo apt update
    
  2. Install the desired version (e.g., Python 3.14):
    bash
    sudo apt install python3.14
    
  3. Verify the installation:
    bash
    python3.14 --version
    
This version is installed alongside the system Python and can be accessed with its specific command (python3.14). Always use virtual environments when working with these separate installations.

No comments:

Blog Archive

Followers