vinci rufus

Switch to UV as your Python Package Manager

2024-10-05
3 minutes

After having tried UV as the package manager for Python, I now regret having wasted so much of my life waiting for pip installs to complete. If you’re tired of slow package installations and want to supercharge your Python development workflow, it’s time to switch to UV.

What is UV and How is it Different?

UV (pronounced “you-vee”) is a revolutionary Python package manager and resolver written in Rust. It’s designed to be a drop-in replacement for pip, offering significant improvements in speed and efficiency. Here’s what sets UV apart:

  1. Lightning-fast installations: UV can install packages up to 10-100 times faster than pip, thanks to its parallel downloading and installation capabilities.

  2. Rust-powered performance: Built with Rust, UV leverages the language’s speed and memory safety to provide a robust and efficient package management experience.

  3. Compatibility: UV aims to be fully compatible with pip, making it easy to integrate into existing projects without breaking your workflow.

  4. Improved dependency resolution: UV uses a state-of-the-art resolver that can handle complex dependency trees more efficiently than pip.

  5. Built-in virtual environment management: UV includes features for creating and managing virtual environments, simplifying your development setup.

How to Install UV

Getting started with UV is straightforward. Here’s how you can install it:

  1. Ensure you have Python 3.7 or later installed on your system.

  2. Open a terminal and run the following command:

    pip install uv
    
  3. Once installed, you can start using UV by replacing pip commands with uv pip. For example:

    uv pip install requests
    uv pip install -r reqiurements.txt
    
  4. To create a new virtual environment with UV:

    uv venv /path/to/new/venv
    

Using UV within Docker

UV can significantly speed up your Docker builds by reducing the time spent on package installation. Here’s an example of how to use UV in a Dockerfile:

FROM ghcr.io/astral-sh/uv:0.4.18 as uv

# Choose your python version here
FROM python:3.10.1-slim-buster

# Create a virtual environment with uv inside the container
RUN --mount=from=uv,source=/uv,target=./uv \
    ./uv venv /opt/venv

# We need to set this environment variable so that uv knows where
# the virtual environment is to install packages
ENV VIRTUAL_ENV=/opt/venv

# Make sure that the virtual environment is in the PATH so
# we can use the binaries of packages that we install such as pip
# without needing to activate the virtual environment explicitly
ENV PATH="/opt/venv/bin:$PATH"

# Set the working directory inside the container
WORKDIR /app

# Copy the requirements file into the container
COPY requirements.txt .

# Install the packages with uv using --mount=type=cache to cache the downloaded packages
RUN --mount=type=cache,target=/root/.cache/uv \
    --mount=from=uv,source=/uv,target=./uv \
    ./uv pip install  -r requirements.txt

COPY . .

EXPOSE 8000
# Set the entry point command to run the Python app
CMD ["python", "your-python-file.py"]