Create diagrams as code using Mermaid

Diagrams help users quickly understand and visualize complex ideas, but they can easily become outdated and difficult to maintain. Creating diagrams as code solves this by keeping them alongside the software source, making updates and reviews simpler.

Mermaid is a popular choice that allows diagrams to be created, maintained, and updated directly in the documentation. This guide explains how to set up the sphinxcontrib-mermaid extension and use Mermaid diagrams in your documentation, with examples included.

Note

While there are many other tools and/or approaches for creating diagrams in visualizations in your documentation (e.g. C4 model, Dia, PlantUML, Structurizr, etc), we only provide support for sphinxcontrib-mermaid in the Sphinx Stack.

Installation and setup

First, add the sphinxcontrib-mermaid extension to your requirements.txt file so it’s installed as a dependency:

canonical-sphinx[full]
packaging
sphinxcontrib-svg2pdfconverter[CairoSVG]
sphinx-last-updated-by-git
sphinx-sitemap
sphinxcontrib-mermaid

Then add sphinxcontrib.mermaid to the extensions list in your conf.py file:

extensions = [
    [...]
    "sphinxcontrib.mermaid",
]

You are now ready embed Mermaid diagrams and visualization in your documentation using the mermaid directive.

Optional configuration

You can further configure Mermaid’s default settings in your project’s conf.py, such as specifying the image output format (e.g., “png”, “raw”), enabling zoom on diagrams, or pinning the Mermaid version used for rendering.

See Mermaid configuration values for more information.

Add a new diagram

Use the mermaid directive to embed a Mermaid diagram into your documentation. You start by declaring the type of diagram (e.g. flowchart, sequenceDiagram, timeline, etc), followed by definition and contents. It is also possible to specify additional configuration or custom styles, depending on the diagram type.

Some examples will be covered below.

See also

See the Mermaid - Diagram syntax reference for details on the syntax and customization options for each diagram type.

Flowchart diagram with default settings

The left-to-right flowchart below uses the default Mermaid settings.

https://assets.ubuntu.com/v1/34c7105c-screenshot_from_2025_10_13_15_05_56.png

Timeline diagram with pre-defined theme

The timeline diagram below uses a pre-defined Mermaid theme.

https://assets.ubuntu.com/v1/24895eed-screenshot_from_2025_10_13_15_06_35.png

Sequence diagram with global custom CSS

The sequence diagram below has custom styling applied using a global CSS file. A global CSS file enables the styles to be easily applied to all sequence diagrams, based on the classes defined in your stylesheet. You can also use the global CSS file to customize the diagrams in dark mode.

https://assets.ubuntu.com/v1/a7eb0fcf-screenshot_from_2025_10_13_15_08_06.png

State diagram with image-specific styles

The state diagram below has image-specific custom styling applied using the classDef keyword.

https://assets.ubuntu.com/v1/dd4fb237-screenshot_from_2025_10_13_15_08_51.png

Projects using Mermaid

Here are some Canonical projects that use Mermaid for diagramming in their documentation: