Skip to main content
  1. Posts/

Setting up Sphinx + ReadTheDocs from Scratch - Rapidly Deploying Automated Documentation

·562 words·2 mins· ·
Xianpeng Shen
Author
Xianpeng Shen
Table of Contents

In daily open-source projects or team collaborations, we often need an easy-to-maintain, automatically deployable documentation system.

Recently, while maintaining my own open-source project, I tried using Sphinx to generate documentation and ReadTheDocs to achieve automatic building and hosting. The overall experience was quite good.

I’m documenting the configuration process here, hoping it can help others with similar needs.

Why Choose Sphinx and ReadTheDocs
#

  • Sphinx is a documentation generator written in Python, initially designed for the official Python documentation. It supports reStructuredText and Markdown (via plugins).
  • ReadTheDocs is a documentation hosting platform that can automatically pull code from your Git repository, build, and publish documentation, supporting webhook auto-triggering.

The combination of these two tools is ideal for continuously maintaining and updating documentation, and the community is mature with abundant resources.

Basic Configuration Steps
#

Below is the complete process configured in a real project.

1. Install Sphinx and Related Dependencies#

It’s recommended to use a virtual environment, then install:

# docs/requirements.txt
sphinx==5.3.0
sphinx_rtd_theme==1.1.1

# If you need Markdown support, add:
myst_parser==0.18.1

Install dependencies:

pip install -r docs/requirements.txt

Notes:

  • sphinx-rtd-theme is the default theme used by ReadTheDocs
  • myst-parser is used to support Markdown

2. Initialize the Documentation Project Structure
#

In the project root directory, execute:

sphinx-quickstart docs

It is recommended to separate the source and build directories.

After execution, the docs directory will generate files such as conf.py and index.rst.

3. Modify the conf.py Configuration File
#

Several key settings are as follows:

# Read the Docs configuration file
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details
from datetime import datetime

project = "GitStats"
author = "Xianpeng Shen"
copyright = f"{datetime.now().year}, {author}"
html_theme = "sphinx_rtd_theme"

If you need Markdown support, you need to add the following to conf.py:

extensions = [
    'myst_parser',  # Support Markdown
]

Configuring ReadTheDocs for Automatic Building
#

As long as the project structure is clear, ReadTheDocs can basically run with one click.

1. Import the Project to ReadTheDocs
#

  • Log in to https://readthedocs.org/
  • Click “Import a Project” and select your GitHub or GitLab repository
  • Ensure the repository contains docs/conf.py; the system will automatically recognize it.

2. Add the .readthedocs.yml Configuration File
#

To better control the build process, it is recommended to add .readthedocs.yml to the project root directory:

# Read the Docs configuration file
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details
version: 2

build:
  os: ubuntu-24.04
  tools:
    python: "3.12"

sphinx:
   configuration: docs/source/conf.py

python:
   install:
   - requirements: docs/requirements.txt

After configuration, every time you submit a Pull Request, ReadTheDocs will automatically pull and build the latest documentation for preview, ensuring the documentation is as expected.

Final Result
#

After building, ReadTheDocs will provide a documentation address similar to https://your-project.readthedocs.io/, facilitating team collaboration and user consultation.

My current open-source project also uses this scheme, for example: GitStats documentation

GitStats documentation

Summary
#

By following the above configuration, you can almost achieve “submit after writing documentation, and it goes live,” greatly improving the efficiency of documentation maintenance.

If you are writing open-source project documentation, or want to add a documentation system to a team project (especially Python projects), you might want to try Sphinx + ReadTheDocs.


Please indicate the author and source when reprinting this article, and do not use it for any commercial purposes. Welcome to follow the official account “DevOps攻城狮” (DevOps Engineer).

Related

Markdown — Not So Great Anymore? Why More and More Python Projects Use RST?
·600 words·2 mins
Markdown and reStructuredText (RST) are two commonly used markup languages. This article compares their advantages and disadvantages and shares usage suggestions in different scenarios.
Why Are More and More Enterprise Users Abandoning VMware?
·1259 words·3 mins
Following Broadcom’s acquisition of VMware, many enterprise users are seeking alternatives. Nutanix, as a hyper-converged infrastructure (HCI) solution, offers lower costs and a simpler management interface, making it a good option.
Why I chose to review for EuroPython 2025
·652 words·2 mins
I haven’t contributed much code lately, focusing instead on reviewing proposals for EuroPython 2025.
🚀 gitstats Upgrade Arrives—JSON Output, Cross-Platform Compatibility, and Code Refactoring!
·338 words·1 min
After two months of continuous improvement, gitstats now supports JSON output, code refactoring, argparse replacing getopt, and full compatibility with Windows and macOS. Welcome to use and Star support!
How to use Jenkins Docker Cloud
·395 words·1 min
This article explains how to use Jenkins Docker Cloud for building and deploying applications, including setting up a Docker host and creating custom Docker images.
What Optimizations I Made During the Jenkins Upgrade
·481 words·3 mins
This article discusses the optimizations made during the Jenkins upgrade, including using Docker Compose for deployment, refactoring the Jenkins Shared Library, introducing Windows Docker Containers, and more to enhance the efficiency and security of the CI/CD process.