杰瑞科技汇

Ubuntu 12.04如何安装Python?

Of course. Here is a comprehensive guide to using Python on Ubuntu 12.04, including its default state, how to upgrade, and the critical security warnings associated with this very old system.

Ubuntu 12.04如何安装Python?-图1
(图片来源网络,侵删)

Executive Summary / Strong Warning

Ubuntu 12.04 (Precise Pangolin) reached its End of Life (EOL) in May 2025. This means it no longer receives any security updates, bug fixes, or software package updates from the official Ubuntu repositories.

Running Ubuntu 12.04 on a machine connected to the internet is a significant security risk. It is highly recommended to upgrade to a supported LTS version like Ubuntu 20.04, 22.04, or 24.04.


The Default Python Installation on Ubuntu 12.04

When Ubuntu 12.04 was released, it came with Python 2.7 as the primary system Python. This was the long-term supported version of Python 2.

How to check your default Python version:

Ubuntu 12.04如何安装Python?-图2
(图片来源网络,侵删)

Open a terminal and run:

python --version

or

python -V

The output will likely be:

Python 2.7.3

This Python installation is managed by the system's package manager (apt). You should never use sudo pip to upgrade or modify this system Python, as it can break critical system tools that depend on it.

Ubuntu 12.04如何安装Python?-图3
(图片来源网络,侵删)

Checking for Other Python Installations

Ubuntu 12.04 might also have a separate python3 package available, though it would be an older version.

python3 --version

The output might be:

Python 3.2.3

This version is also extremely outdated and lacks modern features and security patches.

The Recommended Approach: Using python-is-python3 and venv (The Modern Way)

For any new development, you should not rely on the system Python. The best practice is to use a tool like pyenv to install a modern Python version and manage it in isolated environments called virtual environments.

This method avoids conflicts with system packages and gives you full control over your project's dependencies.

Step 1: Update System Packages (Even if EOL)

First, ensure your package lists are up-to-date. While you won't get security patches, this will ensure you can install the available software.

sudo apt-get update
sudo apt-get upgrade

Step 2: Install Prerequisites

You'll need to install some essential build tools and libraries.

sudo apt-get install -y make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \
libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

Step 3: Install pyenv

pyenv is a fantastic tool for managing multiple Python versions.

curl https://pyenv.run | bash

After the installation, you need to add pyenv to your shell's configuration file. For Bash (the default on Ubuntu), this is ~/.bashrc.

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc

Now, restart your terminal or run source ~/.bashrc to apply the changes.

Step 4: Install a Modern Python Version with pyenv

Let's install a recent, stable version of Python 3, for example, Python 3.9.

pyenv install 3.9.20

(You can see all available versions with pyenv install --list)

Step 5: Create and Use a Virtual Environment

Now, for your project, you can create a virtual environment using the modern Python you just installed.

# Create a directory for your project
mkdir my_project
cd my_project
# Create a virtual environment using the Python 3.9 version
pyenv local 3.9.20  # This sets the Python version for this directory
python -m venv my_project_env
# Activate the virtual environment
source my_project_env/bin/activate

Your terminal prompt will change to show the active environment, like (my_project_env) user@ubuntu:~/my_project$.

Now, when you check the Python version, it will be the modern one you installed:

(my_project_env) $ python --version
Python 3.9.20

You can now use pip to install packages for your project without needing sudo, and they will be contained within this virtual environment.

(my_project_env) $ pip install requests

When you're done, simply deactivate the environment:

(my_project_env) $ deactivate

The Older (But Still Common) Approach: virtualenv

Before Python 3.3, virtualenv was the standard tool. It works similarly.

Step 1: Install virtualenv

sudo apt-get install python-virtualenv

Step 2: Create and Use a Virtual Environment

# Create a virtual environment
virtualenv my_project_env
# Activate it
source my_project_env/bin/activate

Now, your pip commands will install packages into this isolated folder.


How to Install Python Packages

Inside an active virtual environment (venv or virtualenv):

This is the correct and safest way. No sudo is needed.

pip install <package_name>

Globally on the system (Strongly Discouraged):

This can break your system. Only do this if you absolutely know what you're doing and are prepared for potential issues.

sudo pip install <package_name>

Summary and Final Recommendation

Method Pros Cons Recommendation
System Python Pre-installed, no setup needed. Outdated (Python 2.7), risky to modify, breaks system tools. Avoid for any new work.
pyenv + venv Best Practice. Isolated, modern Python versions, full control, no sudo needed. Requires initial setup. Highly Recommended. The standard for modern Python development.
virtualenv Good for isolation, works on older systems. Requires separate installation, pyenv is often more convenient. A good alternative if pyenv is not an option.

Final, Strong Advice:

The most important step you can take is to migrate off Ubuntu 12.04. The security risks are too high. Back up your data and perform a clean installation of a modern, supported Ubuntu LTS version (e.g., 22.04 or 24.04). On a modern system, you will have a recent Python 3 available by default and a much safer, more stable computing environment.

分享:
扫描分享到社交APP
上一篇
下一篇