Passion's Log

Where code meets life

← Back to blog

Complete Guide to Installing Python and Pip on Ubuntu

Python is a core foundational environment for modern development and AI systems. This article provides multiple Python installation and management solutions from an engineering practice perspective, covering stable deployment, development environments, and advanced customization scenarios to help you build a reproducible, isolated, and maintainable Python development environment.

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

Introduction

Python is a core foundational environment for modern development and AI systems, but issues such as version confusion, dependency conflicts, and unreproducible environments are very common in actual work.

Starting from engineering practices, 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.


Preparation Before Installation

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

sudo apt update && sudo apt upgrade -y

Method 1

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

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 Version of Python (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

Install via source code compilation (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 the 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

How to Manage Multiple Versions

Method A

Change the 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 the 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

Method B

Use Virtual Environments (Best Practice).

In modern enterprise-level 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 Version of Python

python3.12 -m venv my_project_env

Activate Environment

source my_project_env/bin/activate

Check Version and Test

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

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

Pip Domestic Acceleration Configuration

To solve the problem of slow or timed-out package downloads domestically, it is recommended to configure a mirror source (using Tsinghua 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