Skip to content

Getting Started with Material for MkDocs

MkDocs Python Hero

This guide walks you through setting up a complete MkDocs site using the Material for MkDocs theme, recommended plugins, GitHub integration, and best practices for maintaining clean, reproducible environments.


1. Prerequisites

  • Python 3.9+ (recommend using Python 3.10 for plugin compatibility)
  • Git (version control)
  • GitHub account (for deployment)
  • Familiarity with terminal commands

Keeping your project layout consistent makes it easier to understand, share, and collaborate on. Here’s a tidy MkDocs structure that separates content, config, templates, and environment files.

mkdocs/
├── .venv/                        # Python virtual environment (isolated dependencies)
├── docs/                         # Main site content (Markdown, assets, styles, scripts)
│   ├── assets/                   # Site-wide shared static files
│   │   ├── favicon/              # Favicon and browser icon files
│   │   │   └── favicon.ico
│   │   └── images/               # Global illustrations or shared graphics
│   │       └── image-01.png
│   ├── javascripts/              # Custom JS for site enhancements
│   │   └── extra.js
│   ├── posts/                    # Organized blog content (per year + slug structure)
│   │   ├── 2025/
│   │   │   ├── your-third-post-slug/
│   │   │   │   ├── your-third-post-slug.md
│   │   │   │   └── assets/       # Post-specific images (match slug)
│   │   │   │       ├── your-third-post-slug-image1.png
│   │   │   │       ├── your-third-post-slug-diagram2.jpg
│   │   │   │       └── your-third-post-slug-chart3.svg
│   │   │   └── your-second-post-slug/
│   │   │       ├── your-second-post-slug.md
│   │   │       └── assets/
│   │   │           ├── your-second-post-slug-image1.png
│   │   │           └── your-second-post-slug-image2.svg
│   │   └── 2024/
│   │       └── your-first-post-slug/
│   │           ├── your-first-post-slug.md
│   │           └── assets/
│   │               ├── your-first-post-slug-image1.png
│   │               └── your-first-post-slug-image2.svg
│   ├── stylesheets/             # Custom CSS overrides and theme tweaks
│   │   └── custom.css
│   ├── .authors.yml             # Author profile list (used by blog plugin)
│   └── index.md                 # Landing page (homepage)
├── raw_posts/                   # Drafts and staging zone for blog posts
│   └── assets/                  # Unprocessed images (auto-moved based on slug match)
├── scripts/                     # Helper scripts and automations
│   ├── organize_posts.py        # Blog organizer script (auto-structure .md + assets)
│   └── logs/                    # Timestamped logs from script runs (auto-rotated)
├── nginx/                       # Reverse proxy configs for deployment (e.g. NGINX)
│   └── config/
├── overrides/                   # Jinja2 templates to override default theme
│   └── partials/                # HTML snippets (e.g. custom logo, post layout)
│       ├── logo.html
│       ├── posts-index.html
│       ├── blog.html
│       ├── landing.html
│       └── main.html
├── site/                        # Output folder (auto-generated on build)
├── .env                         # Environment variables (e.g., tokens, plugin configs)
├── .gitignore                   # Git exclusions (e.g., .venv/, site/, logs)
├── build.sh                     # Build script or Docker wrapper
├── docker-compose.yml           # Docker config for local MkDocs + NGINX (optional)
├── mkdocs.yml                   # Main MkDocs config (theme, plugins, nav, etc)
└── requirements.txt             # Python dependencies (with version pins)

📇 mkdocs/ is a project root directory.

📇 .venv/ folder houses your isolated Python environment — useful for keeping dependencies clean and separate.

📇 docs/ folder holds your actual site content — including blog posts, landing pages, images, stylesheets, and scripts.

  • assets/ folder stores site-wide static files such as images and icons.
  • javascripts/ folder is where extra JS files (like extra.js) live.
  • posts/ directory is for blog-style articles. Each post lives in its own folder (named by slug), with related images in its assets/ subfolder.
  • stylesheets/ folder contains your custom CSS (like nord.css or any theme tweaks).
  • .authors.yml file inside docs/ defines contributors, useful when using author-aware plugins.
  • index.md file acts as your homepage or root entry point.

📇 nginx/config/ folder stores NGINX config files, handy if you're deploying behind a reverse proxy.

📇 overrides/ folder contains custom Jinja2 templates to modify or extend the default Material theme.

📇 partials/ folder inside holds reusable snippets like a custom logo, post listings, or landing page layouts.

📇 raw_posts/ directory is used as a staging area for writing. Place .md drafts here before publishing and related images in raw_posts/assets/ with matching slug-based names. These files will be automatically organized into the correct docs/posts/YYYY/slug/ structure using the organize_posts.py script.

📇 scripts/ contains helper tools like organize_posts.py to automate blog draft from raw_posts/ into docs/posts/ and log each run with rotation.

📇 site/ directory is where MkDocs outputs your built static files. This is auto-generated, so you don’t typically touch it manually.

📇 .env file holds environment-specific variables, like plugin settings or build-time secrets.

📇 .git/ directory is Git’s own internal tracking system — it stores your commit history, branches, and metadata.

📇 .gitignore file tells Git which files and folders to exclude from version control (like .venv/ and site/).

📇 build.sh script is a helper for automating local builds or Docker workflows.

📇 docker-compose.yml file defines your Docker services, volumes, and settings for local or production use.

📇 mkdocs.yml file is the main configuration hub — it defines site structure, theme settings, plugins, extensions, and more.

📇 requirements.txt file lists all Python dependencies with version pins to ensure consistency across environments.


3. Create MkDocs Project Structure

Start by making a new directory and initializing it as a Git repo:

Bash
mkdir mkdocs && cd mkdocs
git init

Initialize the MkDocs project with default files and directories.

Bash
mkdocs new .

This creates:

  • mkdocs.yml configuration file.
  • docs/index.md starter page.

Now expand the full structure to match your working setup.

Create core content and asset directories

Bash
mkdir -p docs/{assets/{favicon,images},javascripts,posts,stylesheets}
touch docs/javascripts/extra.js
touch docs/stylesheets/custom.css
touch docs/.authors.yml

Add example blog posts

Bash
touch docs/posts/post-01.md
touch docs/posts/post-02.md

Set up custom theme overrides (Material partials)

Bash
mkdir -p overrides/partials
touch overrides/partials/{logo.html,posts-index.html,blog.html,landing.html,main.html}

Create reverse proxy folder (for Docker + NGINX)

Bash
mkdir -p nginx/config

Add environment and project control files

Bash
touch .env .gitignore build.sh docker-compose.yml requirements.txt

Add Docker files

Create a .env file and docker-compose.yml to containerize your MkDocs project using linuxserver/mkdocs.

.env
# ============================================ #
#    📖 MkDocs - Static Docs (Nginx - LSIO)    #
# ============================================ #

# User & Group IDs
MKDOCS_PUID=1000
MKDOCS_PGID=1000

# Timezone
TZ=Asia/Kuala_Lumpur

# Domain settings
DOMAINNAME=domain.com
MKDOCS_SUBDOMAIN=www

# Port settings (host:container)
MKDOCS_WEBPORT=8008:80
MKDOCS_LOCPORT=80

# Volume mounts (host_path:container_path)
MKDOCS_CONFIG=/path/to/your/nginx/config:/config
MKDOCS_SITE=/path/to/your/generated/site:/config/www:ro

Replace /path/to/... with actual paths on your host machine.

yaml
services:
  mkdocs: ##### [http://localhost:${MKDOCS_WEBPORT}] MkDocs - Static Docs (Nginx by LSIO) #####
    image: linuxserver/nginx:latest                     # Uses the official LinuxServer.io Nginx image.
    container_name: mkdocs                              # Names the container for easy management.
    hostname: mkdocs                                    # Container hostname.
    restart: unless-stopped
    networks:
      - crossover
    ports:
      - '${MKDOCS_WEBPORT:?err}'                        # Exposes MkDocs site on specific port.
    volumes:
      - ${MKDOCS_CONFIG:?err}                           # Custom nginx configurations.
      - ${MKDOCS_SITE:?err}                             # Mounts the built MkDocs site (read-only).
    environment:
      - PUID=${MKDOCS_PUID:?err}                        # Set user ID for file permissions.
      - PGID=${MKDOCS_PGID:?err}                        # Set group ID for file permissions.
      - TZ=${TZ:?err}                                   # Timezone
    labels:
      # [Expose this container to Traefik]
      - "traefik.enable=true"
      # [HTTP Routers]
      - "traefik.http.routers.mkdocs.rule=Host(`${MKDOCS_SUBDOMAIN:?err}.${DOMAINNAME:?err}`)"
      # [Services]
      - "traefik.http.routers.mkdocs.service=mkdocs"
      - "traefik.http.services.mkdocs.loadbalancer.server.port=${MKDOCS_LOCPORT}"
      # [Routing Control - Mapped from Docker and File Provider]
      - "traefik.http.routers.mkdocs.middlewares=chain-auth@file"
      # [Optional: Watchtower Auto Update]
      - "com.centurylinklabs.watchtower.enable=true"
      # - "com.centurylinklabs.watchtower.monitor-only=true"
networks:
  crossover:
    external: true

Your project is now scaffolded, container-ready with clean structure and ready for virtual environment, theming, plugins, and Docker deployment.


4. Initial Environment Setup

Set up a working Python environment and install dependencies for your MkDocs project.

Set up a virtual environment

This keeps your project dependencies isolated and manageable:

Bash
python3 -m venv .venv
source .venv/bin/activate

Upgrade pip and install dependencies

Always start by upgrading pip to avoid outdated resolver issues:

Bash
pip install --upgrade pip

Then install all dependencies listed in your requirements.txt:

Bash
pip install -r requirements.txt

Tip

Make sure your requirements.txt pins specific versions to keep things consistent across setups.

Freezing dependencies (for version locking)

Once your environment is fully set up and working, generate a fresh requirements.txt using:

Bash
pip freeze > requirements.txt

This will capture all installed packages and their exact versions, ensuring other developers or CI/CD environments get the exact same setup.

Optional cleanup before freezing

To avoid bloated requirements.txt from testing or unused packages:

Bash
pip uninstall -y $(pip freeze | cut -d '=' -f 1)
pip install -r requirements.txt   # reinstall only what's listed
pip freeze > requirements.txt     # then lock fresh

Using pipdeptree to review dependencies

If you're unsure which packages are actually required by your project, you can use pipdeptree to visualize everything.

Step 1: Install pipdeptree

Bash
pip install pipdeptree

Step 2: Run the tree viewer

Bash
pipdeptree

This will print a nested tree of installed packages and their relationships. For example:

mkdocs-material==9.6.14
  - pygments [required: Any, installed: 2.19.1]
  - pymdown-extensions [required: >=10, installed: 10.15]

In this example:

  • pygments and pymdown-extensions are required by mkdocs-material
  • They are indirect dependencies, so you don’t need to list them manually unless you want strict version control.

5. Customizing MkDocs

Modify your mkdocs.yml to unlock some of the slick UI features from the Material for MkDocs theme.

Here's a sample config snippet to get you started:

yaml
theme:
  name: material
  custom_dir: overrides
  features:
    - navigation.tabs
    - navigation.instant
    - navigation.top
    - navigation.path
    - search.suggest
    - search.highlight
    - search.share
    - content.code.annotate
    - content.code.copy
    - content.tooltips
    - header.autohide

Info

  • These features enhance navigation, search, and code presentation. Giving your docs a more polished and interactive feel.
  • You can always tweak or add more features based on what your project needs.

6. Enabling Advanced Plugins

Extend functionality to your MkDocs site by including these handy plugins in your mkdocs.yml:

yaml
plugins:
  - search
  - include-markdown
  - blog:
      enabled: true
      blog_dir: .
      post_url_format: "posts/{file}"
      archive: true
      archive_name: Archive
      categories: true
  - mkdocs-git-revision-date-localized-plugin:
      type: timeago
  - mkdocs-git-committers-plugin-2:
      repository: your-username/your-repo
      branch: main

They’ll add support for blog-style posts, Git commit info, markdown file includes, and nice, human-readable timestamps.


7. GitHub Pages Deployment

Configure your mkdocs.yml with the correct GitHub URLs:

yaml
site_url: https://your-username.github.io/your-repo
repo_url: https://github.com/your-username/your-repo
repo_name: your-username/your-repo

Build and publish your site:

Bash
mkdocs build
ghp-import -n -p -f site
git push origin gh-pages

Then go to GitHub → Settings → Pages, and set the source branch to gh-pages.


8. Clean Reinstall (Reset Environment)

If your environment is broken or outdated, start fresh:

Bash
deactivate
rm -rf .venv
pip cache purge

Create a new virtual environment and reinstall dependencies:

Bash
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

9. Uninstall MkDocs (Optional)

To fully remove MkDocs from your system:

Bash
deactivate
rm -rf .venv

If you installed MkDocs globally (outside of venv):

Bash
pip uninstall mkdocs

10. Final Tips

  • Pin dependency versions in requirements.txt to avoid version drift.
  • Backup your mkdocs.yml and all templates in the overrides/ folder.
  • Always test locally with mkdocs serve before deploying.
  • Use Git to track and collaborate on changes.
  • Enable strict: true in mkdocs.yml to catch build-time errors early.