home
navigate_next
Blog
navigate_next
Tutorials

Installing Python Packages via UV leads to 3.75x increase in build performance

Installing Python Packages via UV leads to 3.75x increase in build performance
Michael Louis
Co-Founder & CEO

Astral, is a company that builds high-performance developer tools for the Python ecosystem. They’re best known for Ruff, an extremely fast Python linter and formatter.

On February 15th they announced, uv, an extremely fast Python package resolver and installer, written in Rust. After seeing this everywhere on X and having many requests from our user base to implement it, we thought we would test the performance ourselves and show how you can implement uv in Cerebrium!

In this short tutorial, we are going to be installing a bunch of common pip packages as well as some of the biggest on the PyPi index to test the performance vs normal pip installs. Hoping our Cerebrium user base can get significant speed ups!

Tutorial:

Pip:

To start let us test the normal pip install performances. To create an empty project, run the following command:


cerebrium init uv-test

We are going to test installing the following pip packages. You can define this in your cerebrium.toml file


[cerebrium.dependencies.pip]
transformers = "latest"
torch = "latest"
accelerate = "latest"
xformers = "latest"
tensorflow-cpu = "latest"

You update your main.py to look like this:


print('we made it!')

def predict(item, run_id, logger):
	return item

Lastly, run the following command to deploy your application:


cerebrium deploy

In our testing, this took our deployment process to about 5 minutes and 30 seconds to assign compute, install packages and create your environment.

UV:

If you look at the Github repository, they recommend many different ways of installing uv. We will install it using pip. One of the requirements of uv is that they require environment which can either be created or inherited from an existing env.

When running pip sync or pip install, uv will search for a virtual environment in the following order:

  • An activated virtual environment based on the VIRTUAL_ENV environment variable.
  • An activated Conda environment based on the CONDA_PREFIX environment variable.
  • A virtual environment at .venv in the current directory, or in the nearest parent directory.

Since Cerebrium runs user apps in their own environment, you don’t need to create a new environment and then activate it - it just works out the gate!

To implement this functionality, we will make use of Cerebrium shell commands functionality. You can update your cerebrium.toml as follows:


[cerebrium.build]
shell_commands = [
"pip install uv",
"uv pip install transformers",
"uv pip install torch",    
"uv pip install accelerate",    
"uv pip install xformers",    
"uv pip install tensorflow-cpu"
]

Please remember to clear the pip dependencies we defined under [cerebrium.dependencies.pip] in the cerebrium toml.

Now run the following to run a clean deployment:


cerebrium deploy —force-rebuild

We do a force rebuild to make sure that we don’t reuse any of the package cache from the previous deployment.

In our testing, this took our deployment process to about 1 minutes and 28 seconds to assign compute, install packages and create your environment - a 58% improvement!

Conclusion

The use of UV over the traditional pip installation method has shown significant improvements in the build process. The tutorial demonstrates that UV, by leveraging the shell commands functionality in Cerebrium, managed to reduce the build time by 3.75x. This is a great increase in performance and especially more so for applications with numerous dependencies. We recommend replacing pip with uv for any team with multiple large Python dependencies that also care about developer iteration speed.

arrow_back
Back to blog