Calibre is a free and open-source e-book manager. It works as a cross-platform desktop application along with a server component. The desktop component can manage your ebook library on a single device. And the server component 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.
- Read ebooks directly on the web.
In this tutorial, you will learn to install 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) is enabled and running.
-
A Fully Qualified domain name pointed to the server. For our tutorial, we will be using the domain
calibre.example.com
. -
Everything is updated.
$ sudo apt update && sudo apt upgrade
Step 1 - Configure Firewall
The first step before installing any packages is to configure the firewall to allow HTTP and HTTPS connections.
Check the status of the firewall.
$ sudo ufw status
You should see something like the following.
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
Check the status again to confirm.
$ 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
Ubuntu 22.04 ships with Calibre but it is better to install it directly to grab the latest version.
The first step is to 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
You will receive some warnings because the installer expects a desktop and not a server. It is safe to ignore the warnings.
Step 3 - Create a Calibre Library and add your first book
The next step is to set up a Calibre library and add some books to it. For our tutorial, we will download the book 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 grab the Kindle version of the Book from the site. Here, we are renaming the Kindle version to filename.mobi
because the Mobi format is supported natively in Kindle which is why it is so named on the site. It also offers better formatting options than the Epub version.
Create a directory for the Calibre library.
$ mkdir calibre-library
Add the book you just downloaded to the library using the calibredb
command. We specified the location of the library in the command itself.
$ calibredb add adventuresofsherlockholmes.mobi --with-library calibre-library/ Added book ids: 1
You can add multiple books at once in the following manner.
$ calibredb add *.mobi --with-library calibre-library/
Step 4 - Start Calibre Server
Now that we have our books ready, it is time to start the Calibre server.
$ calibre-server calibre-library
The command calibre-server
takes the location of the library as its argument. You should see a similar output.
calibre server listening on 0.0.0.0:8080 OPDS feeds advertised via BonJour at: your_server_ip port: 8080
This exposes the Calibre server at port 8080. To view the server, you need to open the port first. Run the following command to open port 8080.
$ sudo ufw allow 8080
Open the URL http://<yourserverIP>:8080/
in your browser and you will get the following screen. The screen will show up as dark or light depending on the choice of your Operating system's theme.
Click on the calibre-library link to view the book you added in the previous step.
Close the server by pressing the keys Ctrl + C on your terminal.
You can use a different port to expose the Calibre server. To do that, use the following command. You will need to make sure that the port is open in the firewall.
$ calibre-server calibre-library --port 7654
Step 5 - Create a systemd service file
To make the Calibre server process persistent and survive reboots, you need to create a systemd service file for the same.
Create a file called calibre-server.service
in the directory /etc/sytemd/system/
and open it for editing.
$ sudo nano /etc/systemd/system/calibre-server.service
Paste the following code in it.
[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
flag gives the server write access to add new books to the library. This is because you can't add books using the calibredb
command while the server is running.
Replace the <username>
with your Linux system username in the above code. Save and close the file by pressing Ctrl+W and entering Y when prompted.
Reload the service daemon to activate the service file.
$ sudo systemctl daemon-reload
Enable and start the Calibre service.
$ sudo systemctl enable calibre-server $ sudo systemctl start calibre-server
Check the status of the service.
$ 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
You can add user authentication to Calibre to protect your library from being available to anyone. First, stop the Calibre server.
$ sudo systemctl stop calibre-server
Calibre uses an SQLite database to store user credentials. Start Calibre's user management script to add a user.
$ sudo calibre-server --manage-users
You will be given four options and asked for your choice. Press 1 to add a new user, and provide the username and the password to finish adding a new user.
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!
Next, you need to edit the service file to check for authentication. Open the file for editing.
$ sudo nano /etc/systemd/system/calibre-server.service
Add the --enable-auth
flag at the end of the line starting with ExecStart
to enable user authentication.
... 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 the file by pressing Ctrl+W and entering Y when prompted.
Reload the service daemon and start the service.
$ sudo systemctl daemon-reload $ sudo systemctl start calibre-server
If you open the library URL, you will be prompted for your username and password before allowing you to access it. Enter your credentials and press the Sign in button to access the library.
Step 7 - Automatically add Books to the library
Calibre allows you to add books automatically to the library using a Cron job. It does so by watching a directory at regular intervals for new books and then adding them to the library. This is the fastest way to add books where you can just upload them to the directory and they will appear soon in the library without any manual intervention.
The first step is to create a watch directory.
$ mkdir ~/calibre-watch $ cd ~/calibre-watch
Download the book War and Peace
by Leo Tolstoy
to this directory from Project Gutenberg.
$ wget https://www.gutenberg.org/ebooks/2600.kindle.images -O warandpeace.mobi
If you notice, this time we are downloading a version that has images. It varies from book to book on Project Gutenberg. Some have image versions available while some do not. Calibre works with either format nicely.
Open the Crontab editor.
$ crontab -e
If this is the first time you are using the Crontab, you will be asked to choose an editor.
no crontab for <username> - using an empty one Select an editor. To change later, run 'select-editor'. 1. /bin/nano <---- easiest 2. /usr/bin/vim.basic 3. /usr/bin/vim.tiny 4. /bin/ed Choose 1-4 [1]:
Enter 1 to choose the Nano editor as it is the easiest one to operate.
Add the following command at the bottom of the file.
*/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 will add all the files in the directory /home/<username>/calibre-watch
to the calibre library and delete all the original files since they are no longer needed. The above job will run every 5 minutes. Replace <username>
with your Linux system username and add your Calibre credentials at the appropriate places.
Save and close the file by pressing Ctrl+W and entering Y when prompted. Wait for a few minutes and reload the calibre server site to see the book you just added appear in the library.
Step 8 - Install Nginx
Ubuntu 22.04 ships with an older version of Nginx. To install the latest version, you need to download the official Nginx repository.
Import Nginx's 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 for Nginx's stable version.
$ 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 the system repositories.
$ sudo apt update
Install Nginx.
$ sudo apt install nginx
Verify the installation.
$ nginx -v nginx version: nginx/1.22.0
Step 9 - Install SSL
Right now, Calibre is exposed on an HTTP port via an IP address. To better secure it, we will enable access via a dedicated domain using an SSL certificate.
We need to install Certbot to generate the SSL certificate for Calibre. You can either install Certbot using Ubuntu's repository or grab the latest version using the Snapd tool. We will be using the Snapd version.
Ubuntu 22.04 comes with Snapd installed by default. Run the following commands to ensure that your version of Snapd is up to date.
$ sudo snap install core
Install Certbot.
$ sudo snap install --classic certbot
Use the following command to ensure that the Certbot command can be run by creating a symbolic link to the /usr/bin
directory.
$ sudo ln -s /snap/bin/certbot /usr/bin/certbot
Run the following command to generate an SSL Certificate.
$ sudo certbot certonly --standalone --agree-tos --no-eff-email --staple-ocsp --preferred-challenges http -m name@example.com -d calibre.example.com
The above command will download a certificate to the /etc/letsencrypt/live/calibre.example.com
directory on your server.
Generate a Diffie-Hellman group certificate.
$ sudo openssl dhparam -dsaparam -out /etc/ssl/certs/dhparam.pem 4096
Open the file /etc/letsencrypt/renewal/calibre.example.com.conf
for editing.
$ sudo nano /etc/letsencrypt/renewal/calibre.example.com.conf
Paste the following code at the bottom.
pre_hook = systemctl stop nginx post_hook = systemctl start nginx
Save the file by pressing Ctrl + X and entering Y when prompted.
We have generated the SSL certificate using the standalone option of Certbot. It runs its web server to create the certificate which means Nginx should be shut off during the renewal. The pre_hook and post_hook commands run before and after the renewal to automatically shut and restart the Nginx server thereby requiring no manual intervention.
To check whether the SSL renewal is working fine, do a dry run of the process.
$ sudo certbot renew --dry-run
If you see no errors, you are all set. Your certificate will renew automatically.
Step 10 - Configure Nginx
Open the file /etc/nginx/nginx.conf
for editing.
$ sudo nano /etc/nginx/nginx.conf
Add the following line before the line include /etc/nginx/conf.d/*.conf;
.
server_names_hash_bucket_size 64;
Save the file by pressing Ctrl + X and entering Y when prompted.
Create and open the file /etc/nginx/conf.d/calibre.conf
for editing.
$ sudo nano /etc/nginx/conf.d/calibre.conf
Paste the following code in it. Replace calibre.example.com
with your domain name. We have set the value of the client_max_body_size
variable to 50MB. You can change it as per your requirements.
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; }
Save the file by pressing Ctrl + X and entering Y when prompted.
Verify your Nginx configuration.
$ sudo nginx -t
Restart the Nginx server to enable the configuration files.
$ sudo systemctl restart nginx
Visit https://calibre.example.com
in your browser to access Calibre. You can see that the book added via Cron is also available in the library.
Conclusion
This concludes our tutorial on installing Calibre Server on a Ubuntu 22.04 machine using Nginx. We also configured Calibre to enable basic authentication and to add support for automatically adding new books. Calibre Server ships with a lot of command-line tools. To learn more about them, you can visit the Calibre Documentation. If you have any questions, post them in the comments below.