Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript
8.8 KB
Raw Blame
[user]
I’m trying to install `pycairo` in a virtual environment on Ubuntu, but `pip install pycairo` fails. Can you help me diagnose it?

[assistant]
Sure. Please paste the complete command and error output, starting with the command you ran. Also include your Ubuntu version, Python version, and whether you’re using a virtual environment.

[user]
Here’s the command and the relevant output:

```text
$ python3 -m venv .venv
$ . .venv/bin/activate
$ python --version
Python 3.12.3
$ python -m pip install pycairo
Collecting pycairo
  Downloading pycairo-1.26.0.tar.gz (346 kB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Building wheels for collected packages: pycairo
  Building wheel for pycairo (pyproject.toml) ... error
  error: subprocess-exited-with-error

  × Building wheel for pycairo (pyproject.toml) did not run successfully.
  │ exit code: 1
  ╰─> [18 lines of output]
      meson setup ... 
      Run-time dependency cairo found: NO (tried pkgconfig and cmake)
      ../cairo/meson.build:31:10: ERROR: Dependency "cairo" not found, tried pkgconfig and cmake
      A full log can be found at /tmp/pip-install-abc123/pycairo_.../meson-logs/meson-log.txt
  error: metadata-generation-failed
```

I’m on Ubuntu 24.04.

[assistant]
The Python package is compiling a native extension and cannot find the system Cairo development library. The `cairo` runtime dependency line is the key part.

On Ubuntu, install the compiler tools, Python headers, pkg-config, and Cairo development package:

```bash
sudo apt update
sudo apt install build-essential python3-dev pkg-config libcairo2-dev
```

Then, with the virtual environment still activated, retry:

```bash
python -m pip install --upgrade pip
python -m pip install pycairo
```

If it still fails, paste the new output, especially any line beginning with `ERROR:`.

[user]
Before I install anything, can I check which of those packages are already present? I’d prefer not to add unnecessary packages.

[assistant]
Yes. You can query their installation status without changing anything:

```bash
dpkg-query -W -f='${Package}\t${Status}\t${Version}\n' \
  build-essential python3-dev pkg-config libcairo2-dev
```

A package is installed if its status ends with:

```text
install ok installed
```

You can also inspect the relevant files:

```bash
command -v gcc
command -v pkg-config
python3-config --includes
pkg-config --modversion cairo
```

The last command may print an error if `libcairo2-dev` or its pkg-config metadata is missing. `build-essential` is a metapackage, so checking `gcc`, `make`, and `dpkg-query` is useful too.

[assistant]
I ran those commands:

```text
$ dpkg-query -W -f='${Package}\t${Status}\t${Version}\n' \
    build-essential python3-dev pkg-config libcairo2-dev
build-essential  install ok installed 12.10ubuntu1
python3-dev      unknown ok not-installed
pkg-config       install ok installed 1.8.1-2build1
libcairo2-dev    unknown ok not-installed

$ command -v gcc
/usr/bin/gcc
$ command -v pkg-config
/usr/bin/pkg-config
$ python3-config --includes
-I/usr/include/python3.12 -I/usr/include/python3.12
$ pkg-config --modversion cairo
Package cairo was not found in the pkg-config search path.
```

So it looks like only the development packages are missing.

[assistant]
That interpretation is correct. `gcc`, `make`, `pkg-config`, and the build-essential metapackage are already available. The missing items are:

```text
python3-dev
libcairo2-dev
```

Install just those two:

```bash
sudo apt update
sudo apt install python3-dev libcairo2-dev
```

Then verify Cairo’s metadata is visible:

```bash
pkg-config --modversion cairo
pkg-config --cflags --libs cairo
```

You should get a version number and compiler/linker flags. After that, retry the package installation from the activated virtual environment:

```bash
python -m pip install pycairo
```

[assistant]
The install completed, and the verification commands now show:

```text
$ pkg-config --modversion cairo
1.18.0

$ pkg-config --cflags --libs cairo
-I/usr/include/cairo -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include \
-I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng16 \
-lcairo -lgobject-2.0 -lglib-2.0
```

But `pip install pycairo` still fails. The new error is:

```text
$ python -m pip install pycairo
Collecting pycairo
  Using cached pycairo-1.26.0.tar.gz (346 kB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Building wheels for collected packages: pycairo
  Building wheel for pycairo (pyproject.toml) ... error
  error: subprocess-exited-with-error

  × Building wheel for pycairo (pyproject.toml) did not run successfully
  │ exit code: 1
  ╰─> [26 lines of output]
      ../cairo/meson.build:42:2: ERROR: C compiler cc cannot compile programs.
      The following exception(s) were encountered:
      Running `cc sanitycheckc.c -o sanitycheckc.exe`
      ... 
      /usr/bin/ld: cannot find -lz
      collect2: error: ld returned 1 exit status
```

[assistant]
Cairo is now detected, but the compiler’s link test cannot find the zlib linker file. On Ubuntu, that file is provided by `zlib1g-dev`.

Install it:

```bash
sudo apt update
sudo apt install zlib1g-dev
```

You can confirm the linker file afterward:

```bash
dpkg -L zlib1g-dev | grep '/libz\.so'
```

Typical output includes a path like:

```text
/usr/lib/x86_64-linux-gnu/libz.so
```

Then retry:

```bash
python -m pip install pycairo
```

The `-lz` option means “link against zlib”; it does not indicate that the Python package itself needs a separate Python zlib module.

[assistant]
I checked first, and `zlib1g-dev` was not installed. After installing it, the `dpkg -L` check prints:

```text
$ dpkg -L zlib1g-dev | grep '/libz\.so'
/usr/lib/x86_64-linux-gnu/libz.so
```

The package installation now succeeds:

```text
$ python -m pip install pycairo
Collecting pycairo
  Using cached pycairo-1.26.0.tar.gz (346 kB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Building wheels for collected packages: pycairo
  Building wheel for pycairo (pyproject.toml) ... done
  Created wheel for pycairo: filename=pycairo-1.26.0-cp312-cp312-linux_x86_64.whl size=...
Successfully built pycairo
Successfully installed pycairo-1.26.0
```

Is there a quick way to verify that Python can import it and that it is linked to Cairo correctly?

[assistant]
Yes. Run a small import and version check inside the activated virtual environment:

```bash
python - <<'PY'
import cairo

print("pycairo:", cairo.version)
print("Cairo:", cairo.cairo_version_string())

surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 16, 16)
context = cairo.Context(surface)
context.set_source_rgb(1, 1, 1)
context.paint()
print("surface:", surface.get_width(), "x", surface.get_height())
PY
```

Expected output will contain version strings and:

```text
surface: 16 x 16
```

To see which shared libraries the extension uses, locate the installed module and pass it to `ldd`:

```bash
python - <<'PY'
import pathlib
import cairo
print(pathlib.Path(cairo.__file__).resolve())
PY
```

Then run:

```bash
ldd /path/printed/by/the/previous/command
```

Look for a `libcairo.so` entry resolving to a system path, rather than `not found`.

[assistant]
The import test worked:

```text
$ python - <<'PY'
import cairo
print("pycairo:", cairo.version)
print("Cairo:", cairo.cairo_version_string())
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 16, 16)
context = cairo.Context(surface)
context.set_source_rgb(1, 1, 1)
context.paint()
print("surface:", surface.get_width(), "x", surface.get_height())
PY
pycairo: 1.26.0
Cairo: 1.18.0
surface: 16 x 16
```

The module path is:

```text
/home/user/project/.venv/lib/python3.12/site-packages/cairo/_cairo.cpython-312-x86_64-linux-gnu.so
```

And `ldd` includes:

```text
libcairo.so.2 => /lib/x86_64-linux-gnu/libcairo.so.2
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1
libpng16.so.16 => /lib/x86_64-linux-gnu/libpng16.so.16
```

[assistant]
That confirms the installation is working. The import succeeded, Cairo reported version `1.18.0`, and the test created and painted an image surface. The `ldd` output also shows that the extension can resolve its Cairo, zlib, and PNG runtime libraries.

For future setup on this Ubuntu system, the relevant commands are:

```bash
sudo apt update
sudo apt install build-essential python3-dev pkg-config libcairo2-dev zlib1g-dev

python3 -m venv .venv
. .venv/bin/activate
python -m pip install --upgrade pip
python -m pip install pycairo
```

The distinction is that `libcairo2-dev` supplied Cairo headers and pkg-config data, while `zlib1g-dev` supplied the linker file needed during compilation. The already-installed runtime libraries were not sufficient for building the extension.