CodingBowl

Solving the Dependency 'cairo' not found" Error in GitHub Codespaces

Published on 23 Mar 2026 Tech Development
image
Photo by Federico Beccari on Unsplash

A quick guide to resolving Meson build failures caused by missing Cairo development headers in containerized environments like VS Code Codespaces, specifically for Django PDF generation.

The Problem

When building C or C++ projects in a fresh environment like a GitHub Codespace, you may encounter the following error:

ERROR: Dependency "cairo" not found, tried pkgconfig and cmake

This happens because the default Linux image provides the runtime environment for Cairo but lacks the development headers required to compile new software against it.

The Connection to Django and PDFs

If you are a Django developer, you likely ran into this while installing WeasyPrint or CairoSVG. Python is excellent for logic, but it relies on the C-based Cairo engine to "paint" HTML and CSS onto a PDF page. Without the -dev package, your Python environment cannot "bridge" the gap between your code and the system's graphics engine.

The Solution

To fix this, you need to install the libcairo2-dev package. This provides the pkg-config files that Meson and Ninja use to locate the library during the build process.

Run this command in your terminal:

sudo apt update && sudo apt install -y libcairo2-dev

Automating the Fix

Since Codespaces are rebuilt from scratch occasionally, you should automate this fix. Add the installation to your .devcontainer/devcontainer.json file so it persists:


{
  "name": "My Project",
  "postCreateCommand": "sudo apt update && sudo apt install -y libcairo2-dev"
}

Why Use libcairo2-dev?

This package is the industry standard for 2D vector graphics on Linux. It is maintained by official distribution security teams, making it a safe and reliable dependency for rendering everything from PDF invoices to complex UI elements.

Meow! AI Assistance Note

This post was created with the assistance of Gemini AI and ChatGPT.
It is shared for informational purposes only and is not intended to mislead, cause harm, or misrepresent facts. While efforts have been made to ensure accuracy, readers are encouraged to verify information independently. Portions of the content may not be entirely original.

image
Photo by Yibo Wei on Unsplash