Passion's Log

Where code meets life

← Back to blog

Installing Python and Pip on Ubuntu

Python is a core foundational environment in modern development and AI systems, but version chaos and dependency conflicts are common. This article provides multiple installation and management solutions for stable deployment and development.

Linux #Linux#Python#Ubuntu#Development Environment#System Administration#Troubleshooting

Preface

Python is a core foundational environment in modern development and AI systems, but issues like version chaos, dependency conflicts, and irreproducible environments are very common in actual work.

Starting from engineering practice, this article provides multiple Python installation and management solutions, covering stable deployment, development environments, and advanced customization scenarios.

Through unified standards, you can build a reproducible, isolated, and maintainable Python development environment.


Prerequisites

Before performing any installation, ensure your system’s package list is up to date.

sudo apt update && sudo apt upgrade -y

Method 1: System Default Installation

Install the default version recommended by the system (most stable and fastest).

Install Python 3 and Pip

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

Verify Installation

python3 --version
pip3 --version

Method 2: Installing via PPA

Install the latest version or a specific older version via PPA (recommended for developers).

Install Software Source Management Tools

sudo apt install software-properties-common -y

Add Deadsnakes PPA Repository

sudo add-apt-repository ppa:deadsnakes/ppa

Update Ubuntu Source List

sudo apt update

Install Specific Python Version (e.g., 3.12)

sudo apt install python3.12 python3.12-venv python3.12-dev -y

Verify Installation

python3.12 --version

Install/Configure Pip for Specific Version

curl -sS https://bootstrap.pypa.io/get-pip.py | python3.12

Verify Pip for Specific Version

python3.12 -m pip --version

Method 3: Compiling from Source

Compile from source code (for absolute control or extreme performance).

Install System Dependencies Required for Compilation

sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev wget libbz2-dev -y

Download Specific Version Source Package (e.g., 3.12.2)

Visit Python Official FTP to find the version number you need.

https://www.python.org/ftp/python/
wget https://www.python.org/ftp/python/3.12.2/Python-3.12.2.tgz

Extract and Enter Directory

tar -xf Python-3.12.2.tgz && cd Python-3.12.2

Configure Compilation Options

./configure --enable-optimizations

Compile and Install

make -j $(nproc)
sudo make altinstall

Verify

python3.12 --version
python3.12 -m pip --version

Managing Multiple Versions

Option A: Changing Global Default Python Version (Use with Caution)

If you want to call your newly installed version (e.g., 3.12) directly when typing python3 in the terminal, you can use the update-alternatives tool.

(⚠️Warning: This may affect system-level scripts relying on older Python versions)

Add Version to Candidate List

# Assuming 3.10 is system default
# Newly installed is 3.12
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.12 2

Switch Version

The system will pop up a list; enter the corresponding number to switch.

sudo update-alternatives --config python3

Option B: Using Virtual Environments (Best Practice)

In modern enterprise development, changing the global default Python is strongly discouraged;

The best solution is to create an independent environment for each project.

Create Virtual Environment Using Your Specified Python Version

python3.12 -m venv my_project_env

Activate Environment

source my_project_env/bin/activate

Check Version and Test

At this point, python and pip within the environment point to 3.12 and are isolated from the system.

python --version
pip install <your-dependency-package>

Configuring Pip Mirrors for Faster Downloads

To solve the problem of slow or timed-out package downloads in China, it is recommended to configure a mirror source (using Tsinghua University mirror as an example).

Temporary Use

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple some-package

Global Configuration (Current User)

python3 -m pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

Comments

Back to top