Sign up Log in
n/ubuntu · 26d ago · 350 views
u/BearPawMaster
n/ubuntuu/BearPaw · 26d ago

Ubuntu Multi-Version Python Management Tools Compared: Five Options Like pyenv, uv and conda

TL;DR

Last week I was helping a colleague set up an environment. He typed pip install requests on 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.

Translated by AI from 简体中文

7
4
Log into join the discussion.
QuicksilverVPN

快银 VPN,一键连接全球节点,线路稳定、延迟低。支持 Windows、macOS 等多平台,TUN 全局模式覆盖全部应用流量。无需复杂配置,下载即用。

umami.dnsl.top Learn more

4 replies

Sort order
  1. tufei 26d ago

    Very detailed write-up, thanks for sharing, OP.

    Translated by AI from 简体中文

    2
  2. huhuhuhuhuhu_huhuhu Gold 25d ago

    Very detailed, nice, 👍 if you need to set up multiple Python versions, this one guide is all you need

    Translated by AI from 简体中文

    1
  3. 550162729 Silver 25d ago

    The mise tool.

    Translated by AI from 简体中文

    1
  4. te_xu Silver 25d ago

    Nice tool, let me review it.
    Thanks OP

    Translated by AI from 简体中文

    1