Creating E-books Locally with GitBook

December 1, 2018

Introduction

For frequent readers, creating local e-books is practically a necessity.

Many online guides are incomplete, so I’ve compiled a detailed, step-by-step tutorial. It’s been personally tested and proven effective — a one-time setup that lasts forever.

The general approach is:

  • Install GitBook locally
  • Install Calibre and configure ebook-convert
  • Export e-books in formats like EPUB (or MOBI, PDF) and refine the table of contents structure

Usage Steps

Step 1: Install gitbook-cli via npm

Run the following command:

npm install -g gitbook-cli

PS: If you're unfamiliar with npm, please refer to how to install the npm environment.


Step 2: Install ebook-convert

(1) On macOS, first install Calibre. After installation, run:

sudo ln -s /Applications/calibre.app/Contents/MacOS/ebook-convert /usr/bin

If you encounter an Operation not permitted error, this indicates system permission restrictions. In that case, you need to configure environment variables.

(2) Environment variable configuration:

vim ~/.bash_profile

export EBOOK_PATH=/Applications/calibre.app/Contents/MacOS
export PATH=$PATH:$EBOOK_PATH

Then reload the configuration:

source ~/.bash_profile

Verify that ebook-convert can be called correctly:

ebook-convert --version

Step 3: Export E-books

Create a new empty folder for your project.

(1) In the project directory, run the following command to initialize:

gitbook init

This will automatically generate two key files (very important):

  • README.md: Write the book’s introduction here.
  • SUMMARY.md: Configure the table of contents here.

(2) Preview the e-book locally:

gitbook serve

Running this command converts Markdown files in the project to HTML by default, and then displays: Serving book on http://localhost:4000.

Open your browser and visit http://localhost:4000 to preview your e-book.

(3) Generate EPUB format e-book (EPUB is the most common and universal e-book format):

gitbook epub ./ ./mybook.epub

Generate MOBI format:

gitbook mobi ./ ./mybook.mobi

You can also generate PDF format:

gitbook pdf ./ ./mybook.pdf

Step 4: Configure the Table of Contents

Place your local Markdown files (your e-book content) into the project directory, then configure the table of contents in SUMMARY.md. Example:

# Table of Contents

* [Preface](README.md)

* [Introduction](Chapter1/README.md)

* [How to Purchase](Chapter2/README.md)

* [After-sales Service](Chapter3/README.md)

* [FAQ](Chapter4/README.md)

* [Update Log](Chapter5/README.md)

	* [# 2018-12-02](Chapter5/20181202.md)
	* [# 2018-12-04](Chapter5/20181204.md)
	* [# 2018-12-05](Chapter5/20181205.md)
	* [# 2018-12-06](Chapter5/20181206.md)
	* [# 2018-12-07](Chapter5/20181207.md)
	* [# 2018-12-10](Chapter5/20181210.md)
	* [# 2018-12-11](Chapter5/2018121.md)
	* [# 2018-12-12](Chapter5/20181212.md)
	* [# 2018-12-13](Chapter5/20181213.md)
  ------

The resulting table of contents will look like this:


Configure Cover Image

You can customize the cover for all e-book formats. Place cover.jpg and cover_small.jpg in the project’s root directory. The e-book generator will automatically add a cover page.


Reference Links


Advanced Section

Modify E-book Font Size

You can also create a book.json file in your project directory to adjust font sizes.

Article Title: Creating E-books Locally with GitBook

Author:

Published: December 1, 2018

Copyright: This article is licensed under CC BY-NC-SA 4.0 . Please keep attribution when reusing.

Related Posts

Comments