Tutorial

How To Create a calibre Ebook Server on Ubuntu 18.04

Updated on October 27, 2020
How To Create a calibre Ebook Server on Ubuntu 18.04
Not using Ubuntu 18.04?Choose a different version or distribution.
Ubuntu 18.04

The author selected the COVID-19 Relief Fund to receive a donation as part of the Write for DOnations program.

Introduction

calibre is a free and open source ebook manager that’s well known for its cross-platform desktop application. You can use calibre to manage your ebook library on a single device, but the application also includes a powerful server component. Setting up an ebook server allows you to:

  • Access your books from anywhere in the world
  • Easily transfer your books to mobile devices
  • Share books with your friends and family

The calibre Content server includes a backend for serving your books and a basic front end for searching through your library, downloading individual titles, or even reading your books directly through a web browser. The server also offers a basic mobile interface that works with a wide variety of devices, including the basic browsers that ship with many e-ink readers such as Kindle and Kobo.

In this tutorial, you’ll set up and use the calibre Content server on Ubuntu 18.04. Specifically, you will download and install the calibre server, configure calibre as a service so that it starts automatically when your server reboots, and add books to your library using command line tools. You will also encounter options to set up a cron job to automatically add new books to your library from a specific directory, add authentication to your server so that only you can access it, and add a free SSL/TLS certificate to serve your library over HTTPS for extra security.

Warning: As with all creative content, it is important to respect the copyright licenses associated with materials created by others. This tutorial uses public domain books legally downloaded from Project Gutenberg, which holds over 60,000 free ebooks.

Prerequisites

Step 1 — Downloading and Installing the calibre Content Server

Although calibre exists in Ubuntu’s software repositories, the version there often lags behind the latest release. Therefore, the official calibre documentation recommends that you install it from a binary hosted on their site instead.

First, install some necessary dependencies:

  1. sudo apt update && sudo apt install -y libfontconfig libgl1-mesa-glx

Now download and install the calibre server.

  1. wget https://download.calibre-ebook.com/linux-installer.sh

Inspect the contents of the script:

  1. less linux-installer.sh

You can scroll up and down with the k and j keys and hit q to get back to your terminal when you are done.

Now execute the script to install calibre:

  1. sudo sh linux-installer.sh

calibre expects a desktop environment but it will not find one on a headless server, so you will see some warnings about desktop integration failing. It is safe to ignore these because we will control calibre entirely via its command line tools and web interface.

After installation, make sure to set the ownership of the ~./config/ file to your user and group and not to root. This is important for calibre to function properly.

Change ownership, replacing the highlighted text with your sudo user and group:

  1. sudo chown sammy:sammy ~/.config/

Now that we have installed calibre, we can begin to explore its functionality.

Step 2 — Creating a Library and Adding Your First Book

To explore calibre’s functionality, let’s download an .epub or .mobi ebook. We’ll use A Christmas Carol by Charles Dickens from Project Gutenberg as an example. Project Gutenberg maintains a massive repository of free, public domain literature and is a great resource for ebooks.

Run the following command to download this book to your server:

  1. wget http://www.gutenberg.org/ebooks/46.kindle.noimages -O christmascarol.mobi

Now create a directory that calibre can use as your ebook library:

  1. mkdir calibre-library

And add the book you just downloaded to your new library using the calibredb command:

  1. calibredb add *.mobi --with-library calibre-library/

You will see the following output:

Output
Added book ids: 1

With a book added to your library, you can now start calibre and explore the application.

Step 3 — Running the calibre Content Server and Viewing Your Library

With calibre installed and a book downloaded, we are ready to explore the application’s user interface. But before we access the calibre Content server in a web browser, we need to make sure that our server can accept traffic on port 8080, which is the default port for calibre. If you followed the initial server setup guide in the prerequisites section, then you enabled ufw, or Uncomplicated Firewall. You now need to allow port 8080 through the firewall.

Use the following command to open port 8080:

  1. sudo ufw allow 8080

Check the status of ufw to make sure the port is open:

  1. sudo ufw status

You will see an output like this:

Output
Status: active To Action From -- ------ ---- OpenSSH ALLOW Anywhere 8080 ALLOW Anywhere OpenSSH (v6) ALLOW Anywhere (v6) 8080 (v6) ALLOW Anywhere (v6)

Now run the following command to start the calibre content server:

  1. calibre-server calibre-library

calibre-server is the command used to start our server and calibre-library is the name of the directory we created earlier and told calibre to use as our library.

You will see an output like this:

calibre server listening on 0.0.0.0:8080
OPDS feeds advertised via BonJour at: your_server_ip port: 8080

From your local machine, visit your_server_ip:8080 (substituting your server’s IP address) in a web browser and you will see the default calibre screen. Click on calibre-library and you will see the book that you added in the previous step.

calibre server menu

Hit Ctrl+C to stop the manual server process that you started.

This method of running the calibre Content server works well, but you probably want your library to work all of the time, even after you close the SSH connection to your server. To make sure it always runs, even after rebooting your server, let’s turn the calibre Content server into a service.

Step 4 — Creating a Service for the calibre Content Server

To improve the usability of the calibre Content server, let’s replace our manual server process with a service that will start on boot.

First, create a file called calibre-server.service in the directory /etc/sytemd/system/:

  1. sudo nano /etc/systemd/system/calibre-server.service

Now add the following configurations, which will start the calibre Content server on boot. Make sure to replace the highlighted text with your user and group:

calibre-server.service
## startup service
[Unit]
Description=calibre content server
After=network.target

[Service]
Type=simple
User=sammy
Group=sammy
ExecStart=/opt/calibre/calibre-server /home/sammy/calibre-library --enable-local-write

[Install]
WantedBy=multi-user.target

Here we tell our service to use the --enable-local-write flag when starting the server. When the server is running, you can’t use the calibredb command to add books as we did for A Christmas Carol. Instead, you have to do this through the running server, and this means that the server needs permission to write new files to disk. This flag allows it to do so as long as it receives the request locally.

Save and close the file.

Now enable the service and start it:

  1. sudo systemctl enable calibre-server
  2. sudo systemctl start calibre-server

Reboot your server:

  1. sudo reboot

Wait a few minutes and then visit http://your_server_ip:8080 again in your local web browser to ensure that the calibre Content server booted automatically.

Now let’s add some authentication to our application.

Step 5 — (Optional) Adding User Authentication to the calibre Content Server

You now have a fully functioning calibre Content server that you can access from any device. Currently, however, anyone who knows your server’s IP address can access your ebooks. You might not want this. Instead, let’s configure calibre’s built-in user management system so you can force visitors to authenticate with a username and password.

First, SSH back into your server and stop calibre. This will allow us to manipulate calibre’s database directly:

  1. sudo systemctl stop calibre-server

Now start calibre’s user management script:

  1. calibre-server --manage-users

When prompted, choose to add a new user. Then select a username and strong password. You will see a final output message like this:

Output
User mycalibreuser added successfully!

Now we need to make one small edit to our service.

Reopen calibre-server.service:

  1. sudo nano /etc/systemd/system/calibre-server.service

To enable authentication, add the --enable-auth flag to the end of the line starting ExecStart. It should look like this:

. . .
ExecStart=/opt/calibre/calibre-server /home/sammy/calibre-library --enable-local-write --enable-auth
. . .

Save and close the file.

Refresh the services daemon to rescan the services files, and start the calibre server again with:

  1. sudo systemctl daemon-reload
  2. sudo systemctl start calibre-server

If you visit your library again, it should now prompt you for a username and password before allowing you to access it.

There are more ways to add functionality to our calibre Content server. In the next step, we will add a cron job to automatically add downloaded books to our calibre library.

Step 6 — (Optional) Automatically Adding Books to Your calibre Library

It can be useful to set up a cron job that watches a specific directory and adds any books it finds to your calibre library. This way, you can download or upload books to this folder and they’ll automatically become available via the calibre web interface.

Create a folder called books-to-add in your home directory and navigate inside:

  1. mkdir ~/books-to-add
  2. cd ~/books-to-add

Download a new book into this directory. Use the following command to download Alice in Wonderland from Project Gutenberg:

  1. wget https://www.gutenberg.org/ebooks/11.epub.images -o alice.epub

Now open your crontab:

  1. crontab -e

Here we will set up a script to add all files in this directory to calibre and then delete them (adding books to calibre creates a copy of the files in your library directory, so we can remove the originals once they are added).

Add the following content:

/var/spool/cron/crontabs/your_username
*/5 * * * * calibredb add /home/sammy/books-to-add/ -r --with-library http://localhost:8080#calibre-library --username mycalibreuser --password StrongPassword! && rm -r /home/sammy/books-to-add/*

Save and close the file.

This will run every 5 minutes, so you shouldn’t have to wait long for your new book to show up in the web interface. Wait a few minutes and then reload the library in your local web browser. Your new book will appear next to A Christmas Carol.

Step 7 — (Optional) Installing Apache2 and Serving Your Library over HTTPS

You need a username and password to access your library, but it’s not really secure because you are serving it over HTTP and sending your username and password unencrypted every time you authenticate. A more secure option is to ensure all traffic gets encrypted using HTTPS. If you own a domain name you can point this to your server and install a free SSL certificate using Let’s Encrypt. (You can also use a subdomain like ebooks.your_domain.)

Installing and Configuring Apache2

We’ll use the webserver Apache2 as a reverse proxy for calibre. Previously we had to append the port number :8080 when we visited our library because that’s the default port that calibre runs on. We’ll now set up Apache2 to listen to requests on port 80 (the default port for HTTP traffic), proxy requests to the locally running calibre server on :8080, and serve these to the end user transparently so that they won’t need to worry about specifying the port number. We will then secure all our traffic on port 443 with an SSL certificate.

Install Apache2 and enable the proxy modules we need with the following commands:

  1. sudo apt install -y apache2
  2. sudo a2enmod proxy proxy_http

Now make sure that your server allows traffic on ports 80 and 443. Close port 8080, too:

  1. sudo ufw allow 'Apache Full'
  2. sudo ufw delete allow 8080

Next, create and open a file at /etc/apache2/sites-available/your_domain.conf:

  1. sudo nano /etc/apache2/sites-available/your_domain.conf

Add the following configurations, which will link your domain to the calibre server:

/etc/apache2/sites-available/your_domain.conf
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so

<VirtualHost *:80>
    ServerName your_domain
    AllowEncodedSlashes On
    ProxyPreserveHost On
    ProxyPass "/"  "http://localhost:8080/"
</VirtualHost>

Finally, enable this new site configuration:

  1. sudo a2ensite your_domain.conf
  2. systemctl reload apache2

Visit http://your_domain in a browser to verify that the calibre Content server loads. Apache is now serving your site.

Installing Certbot and Configuring an SSL Certificate

As a final step, let’s secure all our traffic using Certbot.

First, install Certbot:

  1. sudo snap install --classic certbot

Now run Certbot:

  1. sudo certbot --apache

The Certbot program will take you through a series of prompts to install a certificate.

  • Enter your email address when prompted
  • Enter A to agree to the terms and conditions when prompted
  • Choose Y or N when prompted to share your email address with the EFF
  • Choose 1 when prompted about which domain you want the certificate for (there should only be one)

Upon completion, you will see a congratulations message.

You can now access your library securely at https://your_domain.

Conclusion

In this tutorial, you set up a calibre ebook server. You turned it into a service so that it would start when your server boots, added a cron job to automatically find and add new books to your library, and set up authentication and an SSL certificate to secure it.

To extend the project, you can add more books from your personal library or from Project Gutenberg, Standard Ebooks, or elsewhere. Remember to always respect any copyright laws associated with your content library.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar

Founder @ ritza.co

I’m a software engineer, writer, and mentor. On a mission to make high quality technical educational materials free for everyone.

https://ritza.co



Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.