archive2025

Moving from Conda to uv: a default venv instead of base

I replaced Miniconda with uv for my everyday Python work. Why I switched, how I set up a global default venv to take the place of conda base, a small zsh setup for switching venvs, and the traps when a project has its own .venv.

12 min read#dev#python#uv

For years I had Miniconda on every machine. conda base was my default Python, and I made a named environment for each bigger task. I already used uv to install packages in CI, and this month I removed conda from my laptop and used uv for everything.

Why I left conda

These are the problems I had with conda as a Python developer. Most of them are not about speed.

  • Packages that are not on conda channels. Many libraries are only on PyPI, or the conda package is several versions behind. Then you run pip install inside the conda environment, and now two package managers own the same site-packages. A later conda install or conda update does not know what pip did and can break it. With uv there is only PyPI, so uv pip install finds the same packages that pip finds.
  • Slow solves. Even with the libmamba solver, a conda install in an old environment can take a long time or end in a conflict message that is hard to read. uv resolves and installs in a few seconds in my environments.
  • The big libraries moved to PyPI. NumPy, pandas, scikit-learn and PyTorch all ship binary wheels on PyPI, and the PyTorch wheels for Linux include the CUDA libraries. PyTorch also stopped publishing to its own Anaconda channel with version 2.6 (January 2025). For my work, conda no longer brings packages I cannot get from pip.
  • A polluted base. Everything I tried went into base. After a year nobody knew why a package was there, and fixing base is risky because conda itself lives in it. A uv venv is just a directory. If it breaks, I delete it and make a new one in seconds.
  • Shell startup and conda init. The block that conda init writes into .zshrc runs conda’s shell hook at every start, and that made new terminals slow. My uv setup below only sources one activate script.
  • Reproducing an environment. environment.yml lists packages, not exact versions, unless you add a separate tool such as conda-lock. In a uv project, uv.lock is created and used by default.
  • Python versions. With conda, the Python version is a package inside each environment. uv installs standalone Python builds with uv python install 3.12 and shares them between all venvs.
  • Cheap environments. uv keeps one global cache and links packages from it into each venv. A second venv with the same packages takes little extra disk space and is created almost at once, so I make venvs more freely than I made conda environments.
  • License. The Anaconda defaults channel needs a paid license in larger organizations. conda-forge does not, but you have to configure it. PyPI has no such question.

Conda is still the better tool when you need non-Python binaries that are not available as wheels, for example some GIS libraries, R, or a specific CUDA toolkit for building code. I do not need those on my laptop.

Moving the old environments

First I wrote down what I had installed on purpose. --from-history lists only the packages that I asked for, not every dependency:

conda env list
conda env export -n base --from-history
conda env export -n ai --from-history
conda list -n ai | grep pypi

The last command matters. --from-history only knows what conda installed, so everything I had added with pip install was missing from its output. conda list marks those packages with the channel pypi. That list also has their dependencies, so I only took the names I recognized.

I did not copy the version numbers. Old conda pins often conflict with each other on PyPI. I installed the packages without versions, ran my code, and pinned only what broke.

Then I installed uv and a Python version:

curl -LsSf https://astral.sh/uv/install.sh | sh
uv python install 3.12

For each conda environment I made a venv under ~/.venvs and installed the packages from the list:

uv venv ~/.venvs/default --python 3.12
uv pip install --python ~/.venvs/default ipython requests pandas matplotlib

When conda only had an old version or no package at all, this step was the easy part. The package was simply on PyPI.

Last, I removed conda from the shell, and then deleted the Miniconda directory:

conda init --reverse zsh
conda config --set auto_activate_base false

Do this before you use uv every day. uv pip install looks for an active environment in this order: VIRTUAL_ENV (an activated venv), then CONDA_PREFIX (an activated conda environment), then a .venv in the current or a parent directory. While conda still activates base in every shell, a uv pip install without an active venv goes into conda base, which is the mix of two package managers I wanted to leave.

uv does not install into a Python that is not a virtual environment unless you pass --system. The uv docs say --system is for CI and containers. On my laptop I never use it, so the Python from Homebrew or the OS stays clean.

Some names map one to one:

Conda uv (with the zsh functions below)
conda create -n ai python=3.12 mkvenv ai 3.12
conda activate ai venv ai
conda deactivate deactivate
conda env list venv
conda env remove -n ai rmvenv ai
conda install numpy uv pip install numpy
conda list uv pip list
conda env export > environment.yml uv pip freeze > requirements.txt

If you use Jupyter with several environments, register each venv as a kernel, the same as with conda:

venv ai
uv pip install ipykernel
python -m ipykernel install --user --name ai
jupyter kernelspec remove old-conda-env   # remove kernels of deleted conda environments

I do not install JupyterLab itself into every venv. It runs as a tool (see below), and each venv only needs ipykernel.

A default venv instead of base

~/.venvs/default takes the place of conda base. It holds the tools I use outside projects: IPython, requests, pandas for a quick look at a CSV. Every new shell activates it, so python and uv pip install use it without extra flags.

Other venvs live next to it in ~/.venvs. This is the part of my ~/.zshrc for uv:

# ---- uv ----
export VENVS_HOME="$HOME/.venvs"

# Activate the default venv in new shells. Skip it if a venv is already active,
# for example in a terminal that the editor opened with the project venv.
if [[ -z "$VIRTUAL_ENV" && -f "$VENVS_HOME/default/bin/activate" ]]; then
  source "$VENVS_HOME/default/bin/activate"
fi

# Shell completion for uv and uvx (needs compinit earlier in this file)
eval "$(uv generate-shell-completion zsh)"
eval "$(uvx --generate-shell-completion zsh)"

# venv          list the venvs in ~/.venvs, "*" marks the active one
# venv NAME     activate ~/.venvs/NAME (use `deactivate` to leave it)
venv() {
  if [[ -z "$1" ]]; then
    local d
    for d in "$VENVS_HOME"/*(N/); do
      if [[ "$d" == "$VIRTUAL_ENV" ]]; then print "* ${d:t}"; else print "  ${d:t}"; fi
    done
    return 0
  fi
  if [[ -f "$VENVS_HOME/$1/bin/activate" ]]; then
    source "$VENVS_HOME/$1/bin/activate"
  else
    print -u2 "venv '$1' not found. Run 'venv' to list them or 'mkvenv $1' to create it."
    return 1
  fi
}

# mkvenv NAME [PYTHON]   create ~/.venvs/NAME with uv and activate it
mkvenv() {
  if [[ -z "$1" ]]; then print -u2 "usage: mkvenv NAME [python-version]"; return 1; fi
  if [[ -e "$VENVS_HOME/$1" ]]; then print -u2 "venv '$1' already exists"; return 1; fi
  local -a py
  [[ -n "$2" ]] && py=(--python "$2")
  uv venv "$VENVS_HOME/$1" "${py[@]}" && venv "$1"
}

# rmvenv NAME   delete ~/.venvs/NAME (not the active one)
rmvenv() {
  if [[ -z "$1" ]]; then print -u2 "usage: rmvenv NAME"; return 1; fi
  if [[ ! -d "$VENVS_HOME/$1" ]]; then print -u2 "venv '$1' not found"; return 1; fi
  if [[ "$VENVS_HOME/$1" == "$VIRTUAL_ENV" ]]; then print -u2 "run 'deactivate' first"; return 1; fi
  rm -rf -- "$VENVS_HOME/$1"
}

# Tab completion of venv names
_venv_names() { compadd -- "$VENVS_HOME"/*(N/:t) }
compdef _venv_names venv rmvenv

Some notes on it:

  • *(N/) is a zsh glob for directories only. N makes it return nothing instead of an error when ~/.venvs is empty, and :t keeps only the last part of the path.
  • venv ai works while another venv is active. The activate script of a venv first deactivates the old one.
  • mkvenv without a version lets uv choose the Python, the same as a plain uv venv.
  • compdef needs compinit to run earlier in .zshrc, like the uv completion lines.

Projects have their own .venv

For a project I do not use ~/.venvs. uv init and uv add create .venv in the project directory, and uv.lock next to pyproject.toml. Inside a project, I start every command with uv:

uv add httpx
uv run python -m my_tool
uv run pytest

This matters because the default venv is still active in the shell. Two things surprised me:

  • Plain python and pytest are from the default venv, not from the project. uv run finds the project and uses its .venv. It prints a warning that the active venv does not match the project and is ignored. That warning is expected here.
  • uv pip install goes into the active venv. It does not look for the project first. In a project directory with ~/.venvs/default active, uv pip install httpx installs into the default venv. Use uv add, which also writes the dependency into pyproject.toml.

If you want python to mean the project venv for a while, source .venv/bin/activate works, and venv default brings you back.

For a project that had an environment.yml, I moved the packages into pyproject.toml:

uv init --bare --python 3.12     # only writes pyproject.toml
uv python pin 3.12               # writes .python-version
uv add numpy pandas httpx        # the names from environment.yml, including the pip: section

Pass --python to uv init. Without it, requires-python is set from the newest Python on the machine, and pinning an older version later fails. The channels: part of environment.yml has no equivalent; uv uses PyPI. I kept environment.yml in the repository until the uv setup worked on every machine.

Then the usual rules for a uv project:

  • Commit pyproject.toml, uv.lock and .python-version. Put .venv/ in .gitignore.
  • To check that the lock file is complete, delete .venv and run uv sync, then run the tests with uv run.
  • In CI, use uv sync --locked. It fails when uv.lock does not match pyproject.toml, instead of resolving again.
  • For notebooks in a project: uv add --dev ipykernel, then uv run ipython kernel install --user --name=my-project.

PyTorch from the PyTorch index

The PyTorch wheels on PyPI for Linux are built for one CUDA version and pull in several GB of CUDA libraries. On a Linux machine without a GPU, or for another CUDA version, PyTorch has its own package index. With conda this was the -c pytorch channel. In uv it is an index in pyproject.toml. This is a CPU-only setup for a small Linux server:

[tool.uv.sources]
torch = [{ index = "pytorch-cpu", marker = "sys_platform == 'linux'" }]

[[tool.uv.index]]
name = "pytorch-cpu"
url = "https://download.pytorch.org/whl/cpu"
explicit = true

explicit = true means only packages listed under tool.uv.sources come from this index. Everything else, such as numpy, still comes from PyPI. Without it, uv may take other packages from the PyTorch index too. The marker applies the index only on Linux; macOS gets the normal wheels from PyPI, which have no CUDA anyway. For a specific CUDA version, use an index such as https://download.pytorch.org/whl/cu118 instead. Add torchvision to tool.uv.sources in the same way if you use it.

In a venv without a project, the same works on the command line:

uv pip install torch --index-url https://download.pytorch.org/whl/cpu

One-file scripts

This was the best surprise. Many of my tools are one Python file: a script that calls an API, renames files, or checks a device on the network. A venv for one file is too much, and installing its packages into the default venv slowly fills it up again, the same problem I had with conda base.

uv supports inline script metadata (PEP 723). The dependencies are written into the script itself:

uv init --script check_hosts.py --python 3.12
uv add --script check_hosts.py httpx rich

The top of the file now looks like this:

# /// script
# requires-python = ">=3.12"
# dependencies = [
#     "httpx>=0.28.1",
#     "rich>=13.9.4",
# ]
# ///

uv run check_hosts.py reads this block, builds a cached environment with those packages and runs the script. The default venv is not touched. The next run reuses the cached environment, so it starts quickly.

With a shebang line, the script runs like any other command. --script tells uv to read the file as a script even if the name has no .py at the end:

#!/usr/bin/env -S uv run --script
chmod +x check_hosts.py
./check_hosts.py

I can copy the file to another machine with uv installed, and it runs there without any setup.

Some more things I use with scripts:

  • Keep dependencies in the block, even if it is empty. A script with inline metadata ignores the project around it, so it behaves the same in any directory.
  • Lock a script that must keep working: uv lock --script check_hosts.py writes check_hosts.py.lock next to it. Later uv run --script and uv add --script use and update that lock. Without a lock, each new cache can pick newer versions.
  • Try a package once without changing the file: uv run --with rich check_hosts.py.

Tools and housekeeping

Command-line tools that I installed into conda base now live outside all venvs:

uvx ruff check .                 # run once in a temporary environment
uv tool install ruff             # install for all shells
uv tool install jupyterlab       # Jupyter itself; each venv only needs ipykernel
uv tool upgrade --all

uv tool install gives each tool its own environment, so tools do not break each other or the default venv. This replaced pipx for me.

A few commands for uv itself:

  • uv self update updates uv when it was installed with the install script. With Homebrew, use brew upgrade uv.
  • uv cache prune removes unused entries from the cache, and uv cache clean removes all of it. Do not delete files in the cache directory by hand. uv cache dir shows where it is.
  • uv python list shows the Python versions that uv found or installed.