Installing Calibre Ebook Server on Ubuntu 22.04

Calibre is a free and open-source e-book manager offering cross-platform support with desktop and server components. The desktop component allows local management of your eBook library, whereas the server component provides:

  • Access to your eBook collection from anywhere in the world.
  • Easy transfer of books to mobile devices.
  • The ability to share books with friends and family.
  • Direct reading of eBooks on the web.

This tutorial will guide you through installing the Calibre server on a Ubuntu 22.04 machine.

Prerequisites

  • A server running Ubuntu 22.04.
  • A non-root user with sudo privileges.
  • The Uncomplicated Firewall (UFW) enabled and running.
  • A Fully Qualified Domain Name (FQDN) pointed to your server. In this tutorial, we will use the domain calibre.example.com.
  • Ensure your system is updated.
    $ sudo apt update && sudo apt upgrade

Step 1 – Configure the Firewall

Before installing any packages, configure the firewall to allow HTTP and HTTPS connections.

Check the status of the firewall:

$ sudo ufw status

You should see output similar to:

Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)

Allow HTTP and HTTPS ports:

$ sudo ufw allow http
$ sudo ufw allow https

Confirm the status again:

$ sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
80/tcp                     ALLOW       Anywhere
443/tcp                    ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)
80/tcp (v6)                ALLOW       Anywhere (v6)
443/tcp (v6)               ALLOW       Anywhere (v6)

Step 2 – Download and Install Calibre Server

Although Ubuntu 22.04 comes with Calibre, it’s preferable to install the latest version directly.

First, install some dependencies:

$ sudo apt install libopengl0 libxkbcommon-x11-0 libegl1 libfontconfig libgl1-mesa-glx

Download the Calibre server installer:

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

Make the installer script executable:

$ chmod +x ./linux-installer.sh

Run the installer:

$ sudo ./linux-installer.sh

Ignore any warnings related to the lack of a desktop environment; they’re safe to skip.

Step 3 – Create a Calibre Library and Add Your First Book

Set up a Calibre library and add a book to it. We’ll use The Adventures of Sherlock Holmes by Arthur Conan Doyle from Project Gutenberg.

$ wget http://www.gutenberg.org/ebooks/1661.kindle.noimages -O adventuresofsherlockholmes.mobi

Always opt for the Kindle version (.mobi format), as it is natively supported and provides enhanced formatting.

Create a directory for the Calibre library:

$ mkdir calibre-library

Add the book to the library using the calibredb command:

$ calibredb add adventuresofsherlockholmes.mobi --with-library calibre-library/
Added book ids: 1

To add multiple books at once:

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

Step 4 – Start Calibre Server

With your books ready, start the Calibre server:

$ calibre-server calibre-library

The command calibre-server accepts the library’s location as an argument. You should see output like:

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

This exposes the server on port 8080; make sure to open this port first:

$ sudo ufw allow 8080

Open the URL http://<yourserverIP>:8080/ in your browser to see:

Calibre Server HomeCalibre Server Home

Select the calibre-library link to view the book:

Calibre Server Library ViewCalibre Server Library View

Stop the server with Ctrl + C in your terminal.

To use a different port, ensure it is open in the firewall:

$ calibre-server calibre-library --port 7654

Step 5 – Create a systemd Service File

Create a systemd service file to ensure Calibre server runs persistently even post-reboot.

Create and edit the file /etc/systemd/system/calibre-server.service:

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

Paste the following content and replace <username> with your actual username:

[Unit]
Description=Calibre Server
After=network.target

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

[Install]
WantedBy=multi-user.target

The --enable-local-write option grants the server write permissions to add new books. Save and close with Ctrl+W and Y.

Reload the service daemon:

$ sudo systemctl daemon-reload

Enable and start Calibre service:

$ sudo systemctl enable calibre-server
$ sudo systemctl start calibre-server

Check the service status:

$ sudo systemctl status calibre-server
? calibre-server.service - Calibre Server
     Loaded: loaded (/etc/systemd/system/calibre-server.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2022-07-25 07:23:42 UTC; 15s ago
   Main PID: 1877 (BonJour)
      Tasks: 13 (limit: 2241)
     Memory: 53.3M
        CPU: 762ms
     CGroup: /system.slice/calibre-server.service
             ??1877 /opt/calibre/bin/calibre-server /home/<username>/calibre-library --enable-local-write

Jul 25 07:23:42 calibre systemd[1]: Started Calibre Server.
Jul 25 07:23:42 calibre calibre-server[1877]: QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-<username>'
Jul 25 07:23:42 calibre calibre-server[1877]: calibre server listening on 0.0.0.0:8080
Jul 25 07:23:43 calibre calibre-server[1877]: OPDS feeds advertised via BonJour at: 69.28.84.201 port: 8080

Step 6 – Enable User Authentication

Protect your Calibre library from unwanted access using user authentication. Stop the Calibre server first:

$ sudo systemctl stop calibre-server

To manage users, start Calibre’s user management script:

$ sudo calibre-server --manage-users

Choose option 1 to add a new user and provide username and password:

1) Add a new user
2) Edit an existing user
3) Remove a user
4) Cancel

What do you want to do? [1-4]: (Press 1)
Enter the username: howtoforge
Enter the new password for howtoforge:
Re-enter the new password for howtoforge, to verify:
User howtoforge added successfully!

Edit the service file to require authentication by including the --enable-auth flag:

$ sudo nano /etc/systemd/system/calibre-server.service
...
ExecStart=/opt/calibre/calibre-server "/home/<username>/calibre-library" --userdb "/home/<username>/.config/calibre/server-users.sqlite" --enable-local-write --enable-auth
...

Save and close with Ctrl+W and Y. Reload the daemon and start the service:

$ sudo systemctl daemon-reload
$ sudo systemctl start calibre-server

Accessing the library URL will now require logging in with your credentials.

Calibre Server Authentication PromptCalibre Server Authentication Prompt

Step 7 – Automatically Add Books to the Library

Automate book additions with a cron job. It monitors a directory at intervals to automatically add books to the library.

Create a watch directory:

$ mkdir ~/calibre-watch
$ cd ~/calibre-watch

Download War and Peace by Leo Tolstoy from Project Gutenberg into this directory:

$ wget https://www.gutenberg.org/ebooks/2600.kindle.images -O warandpeace.mobi

Edit the crontab:

$ crontab -e

Add the following line, replacing <username> and placeholders with correct values:

*/5 * * * * calibredb add /home/<username>/calibre-watch/ -r --with-library http://localhost:8080#calibre-library --username mycalibreuser --password StrongPassword! && rm -r /home/<username>/calibre-watch/*

This script runs every 5 minutes to keep the library updated. Save and close with Ctrl+W and Y.

Step 8 – Install Nginx

Install the latest version of Nginx by adding the official Nginx repository:

Import the Nginx signing key:

$ curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
| sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null

Add the repository:

$ echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg arch=amd64] \
http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" \
| sudo tee /etc/apt/sources.list.d/nginx.list

Update and install Nginx:

$ sudo apt update
$ sudo apt install nginx

Verify the installation:

$ nginx -v
nginx version: nginx/1.22.0

Step 9 – Install SSL

Secure your Calibre server with HTTPS by obtaining an SSL certificate for your domain.

Install Certbot using the Snapd package manager:

$ sudo snap install core
$ sudo snap install --classic certbot

Ensure Certbot is accessible:

$ sudo ln -s /snap/bin/certbot /usr/bin/certbot

Request an SSL certificate for your domain:

$ sudo certbot certonly --standalone --agree-tos --no-eff-email --staple-ocsp --preferred-challenges http -m name@example.com -d calibre.example.com

This stores the certificate at /etc/letsencrypt/live/calibre.example.com.

Generate a Diffie-Hellman group certificate:

$ sudo openssl dhparam -dsaparam -out /etc/ssl/certs/dhparam.pem 4096

Edit /etc/letsencrypt/renewal/calibre.example.com.conf for automatic renewal of SSL certificates:

$ sudo nano /etc/letsencrypt/renewal/calibre.example.com.conf
pre_hook = systemctl stop nginx
post_hook = systemctl start nginx

Check if the renewal process is set up correctly:

$ sudo certbot renew --dry-run

Step 10 – Configure Nginx

Open and edit /etc/nginx/nginx.conf:

$ sudo nano /etc/nginx/nginx.conf

Add this configuration line:

server_names_hash_bucket_size  64;

Create and configure /etc/nginx/conf.d/calibre.conf:

$ sudo nano /etc/nginx/conf.d/calibre.conf
server {

    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    http2_push_preload on; # Enable HTTP/2 Server Push
    
    # Enable TLSv1.3's 0-RTT. Use $ssl_early_data when reverse proxying to
    # prevent replay attacks.
    #
    # @see: https://nginx.org/en/docs/http/ngx_http_ssl_module.html#ssl_early_data
    ssl_early_data on;

    server_name calibre.example.com;

    client_max_body_size 50M;

    access_log  /var/log/nginx/calibre.access.log;
    error_log   /var/log/nginx/calibre.error.log;

    ssl_certificate      /etc/letsencrypt/live/calibre.example.com/fullchain.pem;
    ssl_certificate_key  /etc/letsencrypt/live/calibre.example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/calibre.example.com/chain.pem;
    
    ssl_session_timeout  5m;
    ssl_session_cache shared:MozSSL:10m;
    ssl_session_tickets off;
    
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers on;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
    ssl_ecdh_curve X25519:prime256v1:secp384r1:secp521r1;
    ssl_stapling on;
    ssl_stapling_verify on;
    ssl_dhparam /etc/ssl/certs/dhparam.pem;

    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;        
    }
}
# enforce HTTPS
server {
    listen 80;
    listen [::]:80;
    server_name  calibre.example.com;
    return 301   https://$host$request_uri;
}

Check the Nginx configuration:

$ sudo nginx -t

Restart Nginx:

$ sudo systemctl restart nginx

Visit https://calibre.example.com to access Calibre with encryption.

Calibre Library RefreshCalibre Library Refresh

Conclusion

In this comprehensive tutorial, you have learned how to install and configure the Calibre Server on Ubuntu 22.04 using Nginx, enable basic authentication, and automate the addition of new books. Calibre offers many command-line tools for enhanced control and configuration—refer to the Calibre Documentation to explore further. Should you have any inquiries, feel free to comment below.

Frequently Asked Questions (FAQ)

1. Why is it better to install Calibre directly instead of using the Ubuntu repository?

Installing Calibre directly ensures you get the latest version with all the recent features and security updates, which might not be immediately available in the Ubuntu repository.

2. Can I use Calibre Server with other operating systems?

Yes, Calibre is a cross-platform application and can be set up on various operating systems with similar procedures.

3. What format is best for ebook conversion in Calibre?

The Mobi format is generally recommended for Kindle devices due to its superior formatting options, although Calibre supports several other formats, such as EPUB.

4. Is it necessary to use Nginx for Calibre Server?

Nginx is not strictly necessary but is recommended for providing an SSL-secured environment, improved handling of web traffic, and other web server advantages.

5. How can I access my Calibre library remotely?

You can configure the Calibre server to run on your network, and by using a domain name combined with port forwarding, you can access your library remotely through a web browser.

6. Is it safe to ignore desktop-related warnings during Calibre installation?

Yes, if you’re installing the server without a GUI, these warnings indicate missing desktop environments and can be safely ignored.