How to Publish an Online Book with Github and Leanpub

Share Your Expertise in an Instantly Accessible Online Book

George Marklow

--

Introduction

In this article, I show you how to publish a book instantly with GitHub and Leanpub.

Before you continue: at the time of writing, you’ll need a subscription plan to work with Leanpub and your local GitHub repository. Otherwise, there’s a free version you can use through the browser only.

Create a GitHub Repository

Visit GitHub and create an account if you don’t have one already, and create a new repository by clicking the New button:

Next, give your repository a name and description.

You can choose to make your repository public or private. Only you can see the contents of a private repository, so I think it's a good idea to start with a private repository while you work on your book. Then, change your repository visibly to the public once you’re ready to share the book with others.

Also, make sure none of the checkboxes are ticked.

The following page contains a URL that we’ll use to clone our newly created remote repository locally — please keep this window open!

But first, let's create a folder locally.

I’m using macOS, so I open the terminal using ⌘ + Spacebar and search for “terminal”:

I want to create a new folder for my books, then move inside that folder. I use the following commands for that:

  • ls to print the contents of your current directory.
  • mkdir books to create a new folder called books.
  • cd books to go inside the newly created books folder.
ls                 
mkdir books
cd books

Next, type git clone followed by the address of your newly created repository from the GitHub setup page above. For example, here’s how I cloned by the remote repository:

git clone https://github.com/georgemarklow/maths-and-javascript.git

Cloning a repository means downloading all of the source files and copying them to your computer to allow you to work with them locally.

n.b. At the time of writing, basic authentication is being depreciated by GitHub, and you’ll soon need to use Token Authentication.

Create Your Book with Leanpub

Visit Leanpub to create a book in GitHub, entering a Title and URL for your book:

Next, select the On Your Computer option:

Select Using Git and GitHub and enter the exact name you gave earlier for your new repository:

Finally, choose the appropriate plan for you and continue.

Return to your GitHub repository and click the Settings menu tab at the top, then select Manage Access from the menu on the left-hand side:

Click the Invite a collaborator button, type “leanpub” in the search box on the modal that appears, and click the button Add leanpub to this repository:

You should now see the following screen:

Click this link to download a default manuscript for your book. Then, copy and paste the unzipped manuscript folder from your downloads folder to the local book repository.

Transfer only the manuscript folder — this folder is required at a root level to generate your book.

I did this using the Terminal again in macOS:

  • cp is the copy command
  • -R tells cp to copy the folder and contents
  • Provide the source folder first, then the destination folder
cp -R ~/downloads/default-new-book-content-master/manuscript ~/books/maths-and-javascript

Again, use the ls command to print the working directory and confirm that files have been copied to your book repository sub-folder. You should see the manuscript folder only.

Inside the manuscript folder, we can see all files have been copied over:

Then, use the git add command to add all changes in the working directory to the staging area. The git status command will show us the tracked changes in green:

git add . 
git status

Next, use the git commit command with a description (using -m) to get a snapshot of the currently staged changes:

git commit -m "setup"

Finally, use the git push command to upload the content to the remote repository:

git push

Visit the GitHub remote repository and confirm the new files and folders are there:

Navigate to your book in Leanpub:

Go to Unpublished and click on your book:

Click Preview New Version:

Click the Create Preview to see a preview of your template book:

Thanks for reading! Let me know what you think in the comments section below, and don’t forget to subscribe. 👍

--

--

George Marklow

George is a software engineer, author, blogger, and abstract artist who believes in helping others to make us happier and healthier.