Using a Python venv to run different versions of CMake

03Jun25

Sometimes I need an older or newer version of CMake to the one installed by the system package manager on whatever I’m using, and I’ve found using a Python venv provides an easy way to do that. It’s all facilitated by the fact that CMake is a PyPI package [1].

For example, my Kubuntu desktop presently has CMake 4.0.2, as I installed it as a snap, which is very up to date. But (for now) anything that I try to build that includes cJSON fails, because it specifies CMake 3(.0) and CMake 4 is only compatible back to 3.5 [2].

First I need to create and activate a venv[3,4]:

uv venv cmake3.31.6
source cmake3.31.6/bin/activate

NB not all of the patch releases seem to make it to PyPI, so 3.31.7 isn’t there

Then install CMake and verify that I have the expected version:

uv pip install cmake==3.31.6
cmake --version

I can then go off and do my build :) The venv helpfully adds (cmake3.31.6) to my prompt, so I know what’s going on.

The example above is for an older CMake, but I’ve also used the same approach to get a newer CMake.

Notes

[1] Simon Willison has an excellent blog post “Bundling binary tools in Python wheels” about how Zig is packaged on PyPI.
[2] I opened an issue on this before finding that there’s a PR already open :/
[3] I have a habit of keeping my venvs in ~/python/venvs/ but I get that this isn’t necessarily part of Python orthodoxy.
[4] I’m using uv here for creating the venv and installing into it rather than the old school `python -m venv` and `pip install`.



No Responses Yet to “Using a Python venv to run different versions of CMake”

  1. Leave a Comment

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.