TL;DR
Last week I was helping a colleague set up an environment. He typed
pip install requestson 24.04 and got a screenful of red text back. His first thought was that pip was broken, so he reinstalled it — same error. It's not broken; Ubuntu has just kicked you off its system Python's turf. And the moment you need a version newer than the one that ships with the system, apt really can't help you anymore.
About 8 minutes
01 You can't escape PEP 668
Ubuntu 24.04 ships python3 3.12, and the system itself needs it, so you're not allowed to write into it. Type pip install outside a virtualenv and you slam into the PEP 668 wall with externally-managed-environment. --user doesn't help either — packages in your home directory still get imported by the system interpreter.
The official way out comes in three flavors: system-level libraries via apt install python3-xxx, project dependencies in a virtualenv, and CLI tools via pipx. Those three cover most people, but the moment you need a different interpreter version — say your project wants 3.13 and the system only has 3.12 — the apt route starts to feel awkward.
The four tools below are exactly what solves that.
02 pyenv: interpreter management, plain and simple
pyenv is the oldest option in this space, and its scope is narrow: it only manages interpreter versions, nothing else.
Its approach is to shove a shim directory at the very front of your PATH. When you type python, what actually gets hit is the shim, which then looks at the rules for the current directory and forwards the request to the matching interpreter. The real /usr/bin/python3 doesn't get a single hair touched.
Installing it takes two steps, and the first one is easy to skip and the easiest to blow up on:
sudo apt install -y build-essential libssl-dev zlib1g-dev libbz2-dev \
libreadline-dev libsqlite3-dev curl libncursesw5-dev xz-utils \
tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
curl -fsSL https://pyenv.run | bash
This needs explaining, because it's pyenv's biggest hurdle. pyenv installs Python by compiling from source. Miss any one of those dependencies above and the build ends with an error you won't understand. A decent chunk of the questions in pyenv's own issue tracker point right back here.
Once it's installed, add these three lines to ~/.bashrc and open a new terminal:
export PYENV_ROOT="$HOME/.pyenv"
[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"
eval"$(pyenv init -)"
Day to day it's three commands:
pyenv install 3.13.13 # compile and install
pyenv versions # see what's installed
pyenv local 3.13.13 # applies to the current directory only
pyenv local writes a .python-version file into the current directory, and from then on, any time you cd in, the version switches automatically. Other tools later copied this convention — more on that below.
A compile takes anywhere from a few minutes to a dozen or so, depending on the machine. That's the price you pay.
One thing to get out of the way up front: pyenv only manages interpreters, not dependency isolation. After you switch to 3.13 with it, you still need to do the honest python -m venv and pip install inside your project. It's not a replacement for pip and venv.
03 uv: the least-hassle path right now
uv is by Astral, the team behind Ruff, and it's written in Rust. What it's going after is much bigger than pyenv — the official homepage puts it as "a single tool to replace pip, pip-tools, pipx, poetry, pyenv, twine, virtualenv, and more."
Installing on Ubuntu is one line, no Rust and no Python required:
curl -LsSf https://astral.sh/uv/install.sh | sh
Switching versions gets squeezed down to a few words:
uv python install 3.11 3.12 3.13 # three at once, done in seconds
uv python list # see what's available and where it's installed
uv python pin 3.13 # writes .python-version
The fundamental difference from pyenv is how it installs. Python upstream doesn't publish directly distributable binaries, so uv uses python-build-standalone, a set of prebuilt distributions Astral maintains. Download, unpack, done — no compiling. That whole long list of build dependencies from pyenv's step is entirely unnecessary with uv, and it finishes in seconds.
Worth mentioning: uv honors pyenv's .python-version convention too — uv python pin writes the same file. So the two can coexist on one machine; you don't have to pick one.
A few other nice touches. By default it only installs versioned executables like python3.13 and won't hijack your python3; if you want python3 pointed at it, you have to explicitly add --default. Any Python already on your system it detects and uses directly, no config needed. It can also install other implementations like PyPy: uv python install pypy@3.10.
Last thing, so you're not caught off guard the first time: uv works fine without installing Python yourself. By default it downloads missing versions on demand, so a single uv venv can trigger a download. There's a switch to turn that behavior off if you want.
04 deadsnakes: install an official version the system doesn't have
The first two tools put Python in your home directory. deadsnakes takes the other road: it packages Python as debs and installs it into the system via apt.
Its use case is narrow but very real. Your project just needs 3.13, you don't want to understand shims and PATH, you just want apt install python3.13 and to be done.
sudo apt install -y software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install -y python3.13 python3.13-venv python3.13-dev
Three properties you need to know about in advance.
First, it doesn't provide the default Python for any release. It won't give you 3.10 on 22.04 or 3.12 on 24.04, because upstream Ubuntu already provides those. You can only use it to fill in versions the system doesn't ship. That's a reasonable design, but it also means anyone hoping to "manage all versions uniformly with deadsnakes" will be disappointed.
Second, all you get is the interpreter itself. Want a virtualenv? Install python3.13-venv separately. Need to compile C extensions? Install python3.13-dev separately.
Third, and most important, the maintainers explicitly state they don't guarantee timely security updates. The wording is that they "cannot guarantee" prompt updates when security issues arise, and they advise you to accept the risk in security-sensitive environments. This PPA is community-maintained, not an official Ubuntu repository.
Another easy trap: third-party Python modules in the apt repos are built against the ABI of Ubuntu's default interpreter. The interpreter deadsnakes gives you doesn't match, so compiled extension modules basically can't be reused — pure-Python packages are fine though.
And after installing, remember that python3 won't be redirected: what you get is a versioned command, python3.13.
05 conda (Miniforge)
Let's clarify the positioning first, since it often gets lumped into the "Python version manager" bucket. Strictly speaking conda isn't a version manager, it's a cross-language environment manager. It's listed here because a lot of people's real motivation for using it isn't managing Python at all — it's managing non-Python binary dependencies like CUDA and MKL. Those are painful to wrangle in pip's world, or outright impossible to install.
What's recommended nowadays is conda-forge's Miniforge rather than Anaconda. Miniforge configures only the conda-forge channel, so there's none of the old defaults-vs-conda-forge fighting, and it bundles mamba, making dependency resolution much faster.
curl -L -O "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-$(uname)-$(uname -m).sh"
bash Miniforge3-$(uname)-$(uname -m).sh
conda config --set auto_activate_base false# don't auto-activate base
conda create -n ml python=3.13 pytorch torchvision -c
conda-forgeconda activate ml
Here's a detail a lot of people get wrong. The Miniforge base environment comes with its own Python version (currently 3.14), but the base environment's version doesn't affect the environments you create. You can still do conda create -n py313 python=3.13. Base is just a management tool — don't work in it.
The cost is clear too. A conda environment is a complete standalone directory, easily several GB apiece. If you're just doing a small web project, that's using a sledgehammer on a walnut, and the few seconds of dependency resolution aren't worth it either.
06 Don't forget compiling from source as a fallback
This is rarely the first choice, but some situations leave you no way around it — a company's internally customized Python, for instance, or testing a patch release that upstream just published and nobody has packaged yet.
Ubuntu officially allows you to compile, but there's one hard rule:
./configure --enable-optimizations
make -j $(nproc)
sudo make altinstall # note: altinstall, not install
You must use make altinstall. Using make install overwrites /usr/bin/python3, and apt, desktop updates, and all sorts of system tooling may fail to start as a result. altinstall produces a binary with a versioned name like python3.14 that doesn't grab the main command, so it's safe.
This rule gets repeated in every reliable document about installing Python, and the repetition itself is evidence of how important it is.
07 Five tools, one comparison table
Time to wrap up. These tools don't conflict; each anchors a different kind of need.
| Tool | Installs to | How you switch versions | Best for | Main limitation |
|---|---|---|---|---|
| System apt | /usr |
Follows the system release | The built-in version is enough | Can't change versions, and it's protected by PEP 668 |
| pyenv | ~/.pyenv |
Compile from source + shim | Needing precise control over many versions, liking modularity | Slow compiles, fails if dependencies are incomplete, no dependency isolation |
| uv | ~/.local |
cache | Downloading prebuilt binaries | New projects, multi-platform, speed |
| deadsnakes | /usr |
(deb) | apt install | Just wanting one convenient official version |
| conda/Miniforge | ~/miniforge3 |
Separate environments | Data science, needing non-Python deps like CUDA | Big footprint, overkill for pure-Python projects |
If you can only remember one sentence: for a new project, try uv first; if you need long-term precise control over multiple versions and don't want to install new stuff, use pyenv; if you're dealing with CUDA, go Miniforge.
One thing I really want to flag. None of these four tools should be seen as "replacing the system Python." You can install 3.13 on 24.04 and run your projects on 3.13, but always leave /usr/bin/python3 pointing at 3.12. That's not being conservative, it's leaving yourself a way back. On the day apt won't open, you'll be glad you left it alone.
导读
上周帮同事配环境,他在 24.04 上敲了一句
pip install requests,终端甩回来一屏红字。他第一反应是 pip 坏了,重装了一遍,还是同样的错。这不是坏了,是 Ubuntu 把你请出系统 Python 的地盘了。而一旦你要装的版本比系统自带的更新,光靠 apt 就真的没辙了。
约 8 分钟
01逃不掉的 PEP 668
Ubuntu 24.04 自带的 python3 是 3.12,系统自己要用它,所以不让你往里写东西。你在虚拟环境外敲 pip install,会撞上 PEP 668 这堵墙,报 externally-managed-environment。--user 也没用,家目录里的包照样会被系统解释器导入。
官方给的出路有三条:系统级的库用 apt install python3-xxx,项目依赖建虚拟环境,命令行工具用 pipx。三条路对大多数人够用,但只要你需要换解释器版本,比如项目要 3.13 而系统只有 3.12,apt 这条线就开始别扭了。
下面四个工具,解决的就是这件事。
02pyenv,老老实实管解释器
pyenv 是这个圈子里年头最久的方案,它的定位很单一:只管解释器版本,别的一概不管。
它的做法是往 PATH 最前面塞一个 shim 目录。你敲 python 的时候,实际命中的是这个 shim,shim 再去看当前目录的规则,把请求转给对应版本的解释器。系统真正的 /usr/bin/python3 一根汗毛都没动。
装它分两步,第一步容易被跳过,也最容易翻车:
sudo apt install -y build-essential libssl-dev zlib1g-dev libbz2-dev \
libreadline-dev libsqlite3-dev curl libncursesw5-dev xz-utils \
tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
curl -fsSL https://pyenv.run | bash
这里必须解释清楚,因为它是 pyenv 最大的坎。pyenv 装 Python 的方式是从源码编译。上面这串依赖少装任何一个,编译就会以一个你看不懂的错误收场。pyenv 自己的 issue 列表里,相当一部分提问都指向这里。
装好之后,把这三行加进 ~/.bashrc,重开一个终端:
export PYENV_ROOT="$HOME/.pyenv"
[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"
eval"$(pyenv init -)"
日常就三个命令:
pyenv install 3.13.13 # 编译并安装
pyenv versions # 看装了哪些
pyenv local 3.13.13 # 只在当前目录生效
pyenv local 会在当前目录写一个 .python-version 文件,之后只要 cd 进来,版本自动切。这个约定后来被别的工具抄走了,后面会再提到。
编译一次要几分钟到十几分钟不等,取决于机器。这是它换来的代价。
还有一点必须提前说明:pyenv 只管解释器,不管依赖隔离。你用它切成 3.13 之后,项目里还是要老老实实 python -m venv 建虚拟环境、用 pip 装包。它不是 pip 和 venv 的替代品。
03uv,现在最省事的那条路
uv 是 Astral 做的,就是写 Ruff 那个团队,用 Rust 写的。它想干的事比 pyenv 大得多,官方首页的原话是「一个工具替换 pip、pip-tools、pipx、poetry、pyenv、twine、virtualenv 等等」。
装在 Ubuntu 上只要一行,不需要 Rust 也不需要 Python:
curl -LsSf https://astral.sh/uv/install.sh | sh
换版本这件事被压成了几个词:
uv python install 3.11 3.12 3.13 # 一次装三个,秒级完成
uv python list # 看有哪些、装在哪
uv python pin 3.13 # 写 .python-version
它跟 pyenv 最本质的差别在安装方式。Python 官方不发布可直接分发的二进制包,所以 uv 用的是 Astral 自己做的一套预编译发行版 python-build-standalone。下载解包就完事,不编译。前面 pyenv 那一长串构建依赖,在 uv 这里整个不需要,几秒完成。
顺手说,pyenv 那个 .python-version 约定 uv 也认,uv python pin 写的是同一个文件。所以两个工具可以在同一台机器上共存,不用二选一。
uv 还有几个顺手的地方。它默认只装带版本号的可执行文件,比如 python3.13,不会抢你的 python3;想让 python3 指过来,得显式加 --default。你系统里已经有的 Python,它会自动检测直接用,不用配置。它还能装 PyPy 这类别的实现,写法是 uv python install pypy@3.10。
最后说清楚它的自动行为,否则第一次用会愣一下。不装 Python 也能用 uv。它默认会在需要的时候自己下载缺失的版本,所以 uv venv 一条命令就可能触发下载。想把这个行为关掉也是有开关的。
04deadsnakes,给系统装个官方没有的版本
前面两个工具都是把 Python 装在你自己的家目录。deadsnakes 走的是另一条路:它把 Python 打包成 deb,走 apt 装进系统。
它的适用场景很窄但很实在。你的项目就是需要 3.13,你也不想理解 shim 和 PATH 那一套,只想 apt install python3.13 然后结束。
sudo apt install -y software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install -y python3.13 python3.13-venv python3.13-dev
它有三个必须提前知道的性质。
第一,它不支持各版本的默认 Python。22.04 上它不提供 3.10,24.04 上不提供 3.12,因为上游 Ubuntu 已经提供了。你只能拿它补到系统没带的那些版本。这个设计挺合理,但也意味着想到「用 deadsnakes 统一管理所有版本」就会失望。
第二,它给的只是解释器本体。想做虚拟环境还得另装 python3.13-venv,要编译 C 扩展得另装 python3.13-dev。
第三,也是最关键的一条,官方明确声明不保证安全更新的及时性。原文写的是,出安全问题时「无法保证及时更新」,并提醒在安全敏感环境里自行承担风险。这个 PPA 是社区维护的,不是 Ubuntu 官方仓库。
还有一点容易踩:apt 仓库里的第三方 Python 模块,是按 Ubuntu 默认解释器的 ABI 编的。deadsnakes 装进来的解释器跟它们对不上,编译型扩展模块基本不能复用,纯 Python 的包倒是没事。
装完了也要记住,python3 不会因此改指向,你得到的是一个带版本号的命令 python3.13。
05conda(Miniforge)
先说清定位,因为它常被混进「Python 版本管理器」这一类。conda 严格说不是版本管理器,是跨语言的环境管理器。它被列进来,是因为一大批人用它的真实动机不是管 Python,而是管 CUDA、MKL 那些非 Python 的二进制依赖。这些东西在 pip 的世界里很难伺候,甚至装不了。
现在更推荐的是 conda-forge 官方的 Miniforge,而不是 Anaconda。Miniforge 只配 conda-forge 一个频道,没有 defaults 和 conda-forge 打架的老毛病,而且内置了 mamba,依赖解析快得多。
curl -L -O "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-$(uname)-$(uname -m).sh"
bash Miniforge3-$(uname)-$(uname -m).sh
conda config --set auto_activate_base false# 别让 base 自动激活
conda create -n ml python=3.13 pytorch torchvision -c
conda-forgeconda activate ml
有个细节很多人误会。Miniforge 基础环境自带一个 Python 版本(当前是 3.14),但基础环境的版本不影响你建的环境。你照样可以 conda create -n py313 python=3.13。基础环境只是个管理工具,不要在里面干活。
代价也很明确。conda 的环境是一个完整独立的目录,一个环境动辄几个 GB。你要是只是想做个小 Web 项目,用它是杀鸡用牛刀,依赖解析那几秒的等待也不划算。
06别忘了源码编译这条退路
这条路很少是首选,但绕不开某些场景,比如某个公司内部定制的 Python,或者你要测一个上游刚发、各家都还没打包的补丁版本。
Ubuntu 官方允许你编译,但有一条硬性规矩:
./configure --enable-optimizations
make -j $(nproc)
sudo make altinstall # 注意是 altinstall,不是 install
必须用 make altinstall。用 make install 会覆盖 /usr/bin/python3,apt、桌面更新、各种系统工具都可能因此起不来。altinstall 装出来的二进制叫 python3.14 这种带版本号的名字,不会抢主命令,所以是安全的。
这条规矩在任何一篇讲 Python 安装的可靠文档里都会重复一遍,重复本身就是它重要的证据。
07五个工具,一张对照表
写到这里可以收口了。这些工具不冲突,各自锚定一类需求。
| 工具 | 装在哪 | 换版本方式 | 最适合 | 主要局限 |
|---|---|---|---|---|
| 系统 apt | /usr |
跟系统版本走 | 系统自带版本够用 | 换不了版本,且受 PEP 668 保护 |
| pyenv | ~/.pyenv |
源码编译 + shim | 需要精确控制多个版本、喜欢模块化 | 编译慢,依赖装不全就失败,不带依赖隔离 |
| uv | ~/.local |
缓存 | 下载预编译二进制 | 新项目、多平台、追求速度 |
| deadsnakes | /usr |
(deb) | apt 安装 | 只想要一个官方便捷版本 |
| conda/Miniforge | ~/miniforge3 |
独立环境 | 数据科学、需要 CUDA 这类非 Python 依赖 | 体积大,纯 Python 项目属于过度设计 |
要是只能记一句话:给新项目配环境,先试 uv;需要长期精确控制多个版本、又不想装新东西,用 pyenv;要跟 CUDA 打交道,上 Miniforge。
有一点我特别想提醒。这四个工具都不要去想「替换系统 Python」。你可以在 24.04 上装一个 3.13,让项目跑在 3.13 上,但 /usr/bin/python3 永远让它指向 3.12。这不是保守,是给自己留退路。真到了那天 apt 打不开的时候,你会感谢自己没动它。
Translated by AI from 简体中文Original (简体中文)
快银 VPN,一键连接全球节点,线路稳定、延迟低。支持 Windows、macOS 等多平台,TUN 全局模式覆盖全部应用流量。无需复杂配置,下载即用。
4 replies
Sort order