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.