2

The complete installation of gitbook depends on many software:

  • Nodejs depends on the environment, the gitbook command line is based on nodejs
  • nvm manages multiple versions of nodejs. The version of nodejs that gitbook depends on is relatively old, which may conflict with the existing system
  • gitbook-cli main program
  • gitbook
  • calibre generates epub, pdf

The following describes in detail how to install GitBook locally.

Final installed gitbook version:

$ gitbook -V
CLI version: 2.3.2
GitBook version: 3.2.3

Environmental requirements

  • NodeJS (v4.0.0 and above), v10.21.0 recommended, other versions may not be compatible. gitbook is no longer updated.

If there is a local version, it is recommended to install nvm: https://www.jianshu.com/p/6249d1d24914

# nvm: https://github.com/nvm-sh/nvm
 curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
 
 # fast by gitee with 52fhy
 curl -o- https://gitee.com/52fhy/nvm/raw/v0.39.1_gitee/install.sh | bash

By default, the environment variable will be written in the system's bash configuration, and you can use the nvm command in a new window. If it does not take effect, you can set the environment variable according to the installation prompt:

=> Appending nvm source string to /home/yjc/.zshrc
=> Appending bash_completion source string to /home/yjc/.zshrc
=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="$HOME/.config/nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

Then you can use nvm normally:

# 设置NVM下载加速
NVM_NODEJS_ORG_MIRROR=http://npm.taobao.org/mirrors/node  nvm i 10.21.0 

# 常用命令
nvm i 10.21.0  // 安装nodejs 10
nvm use 10.21.0            // 使用 node
nvm ls                   // 查看目前已安装的 node 及当前所使用的 node
nvm ls-remote            // 查看目前线上所能安装的所有 node 版本
nvm alias default 10.21.0  // 使用 10.21.0  作为预设使用的 node 版本
nvm uninstall 10.21.0     // 移除 node 

Install via npm

Run the following command to install gitbook-cli

npm config set registry https://registry.npm.taobao.org
npm install gitbook-cli -g

Among them, gitbook-cli is a command line tool of gitbook, through which multiple versions of gitbook can be installed and managed on the computer.

Print the gitbook version at this point:

gitbook -V               
CLI version: 2.3.2
Installing GitBook 3.2.3
/

The prompt is installing gitbook. Speed up installation method:

wget https://gitee.com/52fhy/gitbook-use/attach_files/963279/download/gitbook_3.2.3.tar.gz
tar zxvf gitbook_3.2.3.tar.gz -C ~/

edit books

It can be edited with tools that support markdown such as vscode and typora.
Example of gitbook project structure:

wegt https://gitee.com/52fhy/gitbook-use/attach_files/964785/download/gitbook-example.zip

preview books

Use the following command to run a server that previews books via http://localhost:4000/

gitbook serve

After running this command, a _book folder will be generated in the book folder, and the content inside is the generated html file.

We can use the following command to generate a web page without starting the server

gitbook build

Generate epub, pdf:

gitbook epub
gitbook pdf

To generate epub and pdf, calibre needs to be installed on the system: https://calibre-ebook.com/download or https://download.calibre-ebook.com/5.html

# linux 通用
sudo -v && wget -nv -O- https://download.calibre-ebook.com/linux-installer.sh | sudo sh /dev/stdin

# ubuntu 版本可能比较老
sudo apt install calibre
This article is excerpted from my github project: https://github.com/52fhy/gitbook-use , welcome to star.

飞鸿影
1.4k 声望442 粉丝

一个人至少拥有一个梦想,有一个理由去坚强。心若没有栖息的地方,到哪里都是在流浪。