How to Install Python 3 on Ubuntu 22.04 or 24.04: Complete Step-by-Step Guide



How to Install Python 3 on Ubuntu 22.04 or 24.04: Complete Step-by-Step Guide

⚡ Quick Answer

The fastest way to install Python 3 on Ubuntu: Run sudo apt update && sudo apt install python3 python3-pip python3-venv in your terminal. This installs Python 3, pip package manager, and virtual environment support in one command.

Python has become the cornerstone of modern development, powering everything from web applications and data science projects to machine learning algorithms and automation scripts. Whether you’re a developer starting your first Django project, a data scientist preparing to work with NumPy and pandas, or a system administrator looking to automate routine tasks, having Python properly installed on your Ubuntu system is essential.

This comprehensive guide will walk you through every aspect of installing Python 3 on Ubuntu 22.04 and 24.04, including package management with pip, virtual environment setup, and troubleshooting common issues. By the end of this tutorial, you’ll have a fully functional Python development environment ready for any project.

1. Why Python 3 Matters in 2025

Python 3 represents the current and future of Python development. With Python 2 officially reaching end-of-life in January 2020, Python 3 has become the only supported version, offering significant improvements in performance, security, and functionality.

💡 Key Advantages of Python 3

  • Enhanced Performance: Improved memory management and faster execution
  • Better Unicode Support: Native Unicode handling eliminates encoding issues
  • Modern Syntax: Cleaner, more readable code with advanced features
  • Rich Ecosystem: Access to cutting-edge libraries and frameworks
  • Active Development: Regular updates with new features and security patches

Whether you’re building web applications with Django or Flask, analyzing data with pandas and NumPy, or developing machine learning models with TensorFlow and PyTorch, Python 3 provides the foundation you need for modern development.

2. Prerequisites and System Requirements

Before beginning the installation process, ensure your system meets these requirements and you have the necessary access permissions.

📋 Prerequisites Checklist

  • ☑️ Ubuntu 22.04 LTS (Jammy Jellyfish) or Ubuntu 24.04 LTS (Noble Numbat)
  • ☑️ Root or sudo privileges for system-level installations
  • ☑️ Active internet connection for downloading packages
  • ☑️ Terminal or SSH access to your Ubuntu system
  • ☑️ At least 500MB free disk space for Python and common packages

Most Ubuntu installations come with Python 3 pre-installed, but the version may not be the latest, and essential tools like pip might be missing. This guide ensures you have the complete Python development environment with all necessary components.

3. Update Your System Packages

Starting with an updated system ensures you’ll install the latest available versions of Python and related packages. Package updates also include important security patches and bug fixes.

Step 1: Update package lists and upgrade existing packages

sudo apt update && sudo apt upgrade -y

This command performs two essential operations:

  • sudo apt update refreshes the package index from Ubuntu’s repositories
  • sudo apt upgrade -y installs available updates for all installed packages

💡 Pro Tip: Understanding apt vs apt-get

While apt-get still works, apt is the modern, user-friendly command with better output formatting and progress indicators. Both commands achieve the same result, but apt provides a more pleasant user experience.

4. Verify Existing Python Installation

Ubuntu typically includes Python 3 by default, but it’s important to check which version is installed and whether it meets your project requirements.

Step 2: Check current Python 3 version

python3 --version

You should see output similar to:

# Expected output:
Python 3.10.12  # Ubuntu 22.04 default
# or
Python 3.12.3   # Ubuntu 24.04 default

If you see “command not found” or need a newer version, continue with the installation steps below.

📊 Python Version by Ubuntu Release

  • Ubuntu 22.04 LTS: Python 3.10.x (default)
  • Ubuntu 24.04 LTS: Python 3.12.x (default)

These versions are well-maintained and receive security updates throughout the LTS lifecycle.

5. Install Python 3 on Ubuntu

If Python 3 isn’t installed or you need to ensure all components are present, use the apt package manager to install the complete Python 3 environment.

Step 3: Install Python 3 and essential components

sudo apt install python3 python3-pip python3-venv python3-dev -y

This comprehensive command installs:

  • python3 – The Python 3 interpreter
  • python3-pip – Package installer for Python
  • python3-venv – Virtual environment support
  • python3-dev – Development headers for compiling Python extensions
Step 4: Verify the installation was successful

python3 --version
which python3

The output should confirm your Python installation:

# Expected output:
Python 3.10.12
/usr/bin/python3

6. Install and Configure pip Package Manager

pip (Pip Installs Packages) is Python’s standard package manager, essential for installing libraries from the Python Package Index (PyPI). Modern Python installations should include pip, but it’s worth verifying and updating.

Step 5: Verify pip installation and check version

pip3 --version

Step 6: Upgrade pip to the latest version

python3 -m pip install --upgrade pip

⚠️ Important: Why Use python3 -m pip

Using python3 -m pip instead of just pip3 ensures you’re using the pip associated with your specific Python installation. This prevents version conflicts and ensures packages install to the correct location.

Understanding pip and Its Importance

pip connects you to the vast Python ecosystem, containing over 400,000 packages covering every conceivable use case. From web frameworks like Django and Flask to data science libraries like pandas and scikit-learn, pip makes these tools instantly accessible.

🌐 The Python Package Index (PyPI)

PyPI hosts millions of Python packages contributed by developers worldwide. Popular categories include:

  • Web Development: Django, Flask, FastAPI
  • Data Science: NumPy, pandas, matplotlib
  • Machine Learning: scikit-learn, TensorFlow, PyTorch
  • Automation: Selenium, requests, BeautifulSoup

7. Essential pip Commands Every Developer Should Know

Mastering these fundamental pip commands will make you efficient at managing Python packages and dependencies.

Basic Package Operations

Install a single package:

pip3 install package-name

# Example: Install the popular requests library
pip3 install requests

Install multiple packages simultaneously:

pip3 install numpy pandas matplotlib

# For data science essentials
pip3 install jupyter numpy pandas matplotlib seaborn

Install a specific version:

# Install exact version
pip3 install django==4.2.0

# Install minimum version
pip3 install requests>=2.28.0

# Install version range
pip3 install flask>=2.0.0,<3.0.0

Package Management Commands

# List all installed packages
pip3 list

# Show detailed package information
pip3 show package-name

# List outdated packages
pip3 list --outdated

# Upgrade a package
pip3 install --upgrade package-name

# Uninstall a package
pip3 uninstall package-name

# Install from requirements file
pip3 install -r requirements.txt

# Generate requirements file
pip3 freeze > requirements.txt

💡 Pro Tip: Managing Dependencies with requirements.txt

Always maintain a requirements.txt file for your projects. This file lists all dependencies with specific versions, enabling consistent installations across different environments:

# Generate requirements for current environment
pip3 freeze > requirements.txt

# Install exact dependencies in new environment
pip3 install -r requirements.txt

8. Set Python 3 as the System Default (Optional)

By default, Ubuntu may require you to type python3 instead of just python. If you prefer using python to invoke Python 3, you can configure this system-wide.

⚠️ Caution: Changing System Defaults

Modifying system Python defaults can affect system tools that expect specific Python versions. Only proceed if you understand the implications and primarily work with Python 3.

Step 7: Configure Python 3 as default using alternatives

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 1

Step 8: Verify the change

python --version
# Should output: Python 3.x.x

🔄 Alternative Approach: Shell Aliases

A safer alternative is creating a shell alias that only affects your user account:

# Add to ~/.bashrc or ~/.zshrc
echo 'alias python=python3' >> ~/.bashrc
echo 'alias pip=pip3' >> ~/.bashrc

# Reload shell configuration
source ~/.bashrc

9. Python Virtual Environments: Best Practices

Virtual environments are isolated Python environments that allow different projects to have separate dependencies. This prevents conflicts when projects require different versions of the same library.

Why Virtual Environments Matter

Imagine you're working on two projects: Project A requires Django 3.2, while Project B needs Django 4.2. Without virtual environments, you'd face constant conflicts. Virtual environments solve this by creating isolated spaces for each project's dependencies.

Step 9: Ensure venv module is installed

sudo apt install python3-venv -y

Creating and Managing Virtual Environments

Step 10: Create a new virtual environment

# Create virtual environment named 'myproject'
python3 -m venv myproject

# Create with specific Python version
python3.10 -m venv myproject-py310

Step 11: Activate the virtual environment

# Linux/macOS activation
source myproject/bin/activate

# You'll see the environment name in your prompt:
# (myproject) user@hostname:~$

Step 12: Install packages in the virtual environment

# Install packages (only affects this virtual environment)
pip install django requests pandas

# Verify installation location
which pip
# Output: /path/to/myproject/bin/pip

Step 13: Deactivate the virtual environment

# Return to system Python environment
deactivate

# Prompt returns to normal:
# user@hostname:~$

Pro Tip: Virtual Environment Best Practices

  • Project Structure: Create virtual environments inside or alongside your project directories
  • Naming Convention: Use descriptive names like myapp-env or project-venv
  • Requirements Management: Always generate requirements.txt files: pip freeze > requirements.txt
  • Version Control: Never commit virtual environment folders to Git - add them to .gitignore

Advanced Virtual Environment Management

For developers working with multiple projects, consider using tools that simplify virtual environment management:

# Install virtualenvwrapper for easier management
pip3 install virtualenvwrapper

# Or use pipenv for automatic dependency management
pip3 install pipenv

10. Troubleshooting Common Installation Issues

Even with careful installation, you might encounter issues. Here are solutions to the most common problems developers face when setting up Python on Ubuntu.

Issue 1: "Command 'python3' not found"

Error Message:

bash: python3: command not found

Cause: Python 3 isn't installed or isn't in the system PATH.

Solution:

# Install Python 3
sudo apt update
sudo apt install python3 python3-pip

# Verify installation
python3 --version
which python3

Issue 2: "pip: command not found"

Error Message:

bash: pip: command not found or bash: pip3: command not found

Cause: pip package manager isn't installed or is corrupted.

Solution:

# Install pip for Python 3
sudo apt install python3-pip

# Alternative: Install using get-pip.py
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3 get-pip.py --user

# Verify installation
pip3 --version

Issue 3: Permission Denied Errors

Error Message:

ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied

Cause: Attempting to install packages globally without proper permissions.

Solutions (in order of preference):

# Option 1: Use virtual environment (recommended)
python3 -m venv myenv
source myenv/bin/activate
pip install package-name

# Option 2: Install for current user only
pip3 install --user package-name

# Option 3: Use sudo (not recommended for development)
sudo pip3 install package-name

Why Avoid sudo with pip?

Installing packages with sudo can:

  • Conflict with system package manager
  • Create security vulnerabilities
  • Make package management difficult
  • Affect system stability

Virtual environments provide better isolation and security.

Issue 4: "ModuleNotFoundError" After Installation

Error Message:

ModuleNotFoundError: No module named 'package_name'

Cause: Package installed in different Python environment than where you're trying to use it.

Diagnostic Steps:

# Check which Python you're using
which python3
python3 -c "import sys; print(sys.executable)"

# Check which pip you're using
which pip3
pip3 --version

# List installed packages
pip3 list | grep package-name

# Check Python path
python3 -c "import sys; print('\n'.join(sys.path))"

Issue 5: SSL Certificate Errors

Error Message:

SSL: CERTIFICATE_VERIFY_FAILED

Solution:

# Update certificates
sudo apt update
sudo apt install ca-certificates

# Update pip and try again
python3 -m pip install --upgrade pip
pip3 install package-name

11. Installing Popular Python Packages

With Python and pip properly configured, you can install packages for various development scenarios. Here are curated package collections for common use cases.

Web Development Stack

# Django web framework
pip3 install django

# Flask lightweight framework
pip3 install flask flask-sqlalchemy flask-migrate

# FastAPI for modern APIs
pip3 install fastapi uvicorn

# Web scraping tools
pip3 install requests beautifulsoup4 selenium

Data Science and Analytics

# Essential data science stack
pip3 install numpy pandas matplotlib seaborn

# Jupyter notebooks for interactive development
pip3 install jupyter jupyterlab

# Machine learning libraries
pip3 install scikit-learn

# Statistical computing
pip3 install scipy statsmodels

Development Tools and Utilities

# Code formatting and linting
pip3 install black flake8 pylint

# Testing frameworks
pip3 install pytest pytest-cov

# Environment management
pip3 install python-dotenv

# Database connectors
pip3 install psycopg2-binary pymongo redis

Pro Tip: Creating Development Environment Templates

Create requirements.txt templates for different project types:

# Create web-dev-requirements.txt
echo "django>=4.2.0
djangorestframework
celery
redis
psycopg2-binary" > web-dev-requirements.txt

# Create data-science-requirements.txt  
echo "numpy
pandas
matplotlib
seaborn
jupyter
scikit-learn" > data-science-requirements.txt

# Use templates for new projects
pip3 install -r web-dev-requirements.txt

12. System Cleanup and Maintenance

Regular maintenance keeps your Python environment clean and efficient. Here's how to manage your installation over time.

Removing Unused Packages

# List installed packages
pip3 list

# Uninstall specific packages
pip3 uninstall package-name

# Uninstall multiple packages
pip3 uninstall package1 package2 package3 -y

# Clean pip cache
pip3 cache purge

Removing Old Python Versions

Caution: System Dependencies

Some system tools depend on specific Python versions. Only remove Python versions you're certain aren't needed by system components.

# Check what Python packages are installed
dpkg -l | grep python

# Remove specific Python version (example: Python 3.8)
sudo apt remove python3.8 python3.8-dev python3.8-venv

# Remove unused dependencies
sudo apt autoremove

13. Frequently Asked Questions

How do I check which Python packages are installed?

Answer: Use pip3 list to see all installed packages with their versions. For detailed information about a specific package, use pip3 show package-name.

# List all packages
pip3 list

# Show package details
pip3 show requests

# List packages in specific format
pip3 freeze

How do I install a specific version of a Python package?

Answer: Use the == operator followed by the version number. You can also specify version ranges using >=, <=, or !=.

# Install exact version
pip3 install django==4.2.0

# Install minimum version
pip3 install requests>=2.28.0

# Install version range
pip3 install flask>=2.0.0,<3.0.0

# Install pre-release versions
pip3 install --pre package-name

Why should I use virtual environments?

Answer: Virtual environments isolate project dependencies, preventing conflicts between different projects that require different versions of the same library. They also make it easier to deploy applications and maintain clean development environments.

Virtual Environment Benefits:

  • Dependency Isolation: Each project has its own package versions
  • Clean System: Avoid cluttering global Python installation
  • Reproducibility: Exact dependency versions for consistent deployments
  • Security: Minimize conflicts with system packages

Can I have multiple Python versions installed simultaneously?

Answer: Yes, Ubuntu can have multiple Python versions installed. Use specific version commands like python3.10 or python3.12 to invoke particular versions.

# Install additional Python versions
sudo apt install python3.10 python3.11 python3.12

# Create virtual environments with specific versions
python3.10 -m venv myenv-py310
python3.12 -m venv myenv-py312

# Check available Python versions
ls /usr/bin/python*

How do I update pip to the latest version?

Answer: Use python3 -m pip install --upgrade pip to ensure you're updating the pip associated with your Python installation.

# Update pip
python3 -m pip install --upgrade pip

# Check pip version
pip3 --version

# Update pip in virtual environment
source myenv/bin/activate
pip install --upgrade pip

What's the difference between pip and pip3?

Answer: pip3 specifically refers to Python 3's package installer, while pip might refer to Python 2 or 3 depending on your system configuration. Always use pip3 to be explicit about Python 3.

How do I fix "externally-managed-environment" errors?

Answer: This error occurs on newer Ubuntu versions that prevent global package installation. Use virtual environments or the --user flag for user-local installations.

# Recommended: Use virtual environment
python3 -m venv myenv
source myenv/bin/activate
pip install package-name

# Alternative: Install for user only
pip3 install --user package-name

Conclusion: Your Python Development Environment is Ready

You've successfully installed Python 3 and pip on Ubuntu 22.04 or 24.04, creating a robust foundation for any Python project. The installation includes essential components like virtual environment support and package management tools, giving you everything needed for professional development.

Key achievements from this guide:

  • Complete Python 3 installation with latest features and security updates
  • pip package manager for accessing the vast Python ecosystem
  • Virtual environment capabilities for project isolation
  • Troubleshooting knowledge for common installation issues
  • Best practices for maintaining a clean development environment

Start with virtual environments for your first project - they're essential for maintaining clean, professional development workflows. As you become more comfortable with Python, explore advanced tools like poetry for dependency management or pyenv for managing multiple Python versions.

Next Steps for Python Mastery

  • Learn the Basics: Complete the official Python tutorial
  • Explore Frameworks: Try Django for web development or pandas for data analysis
  • Join the Community: Visit PyPI to discover useful packages
  • Practice Coding: Build projects and contribute to open source

Your Python journey begins now. Whether you're building web applications, analyzing data, or automating tasks, you have the tools and knowledge to succeed. Happy coding!



L

Written by Logic Encoder

Professional crypto analyst and trading expert

Leave a Reply

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