Tutorial

How To Create a calibre Ebook Server on Ubuntu 14.04

Updated on September 12, 2020
How To Create a calibre Ebook Server on Ubuntu 14.04
Not using Ubuntu 14.04?Choose a different version or distribution.
Ubuntu 14.04

Introduction

Calibre is a free and open source ebook manager.

Although Calibre is probably better known for its desktop client, it can also act as a powerful server, allowing you to access your ebooks from anywhere in the world (or share your collection with friends). Keeping your ebooks on a server is great, as you aren’t reliant on having the same reading device with you whenever you want to read. And if you go traveling, you don’t need to worry about taking your ebook collection with you!

The server includes a simple and elegant browser front-end that allows you to search for and download books from your library. It also has a mobile-friendly site built in, making it easy to download books straight to an e-reader – even to ones with only the most basic web functionality.

For example, Calibre’s browser works with the Kindle Touch, which can download books directly even though the device only has an e-ink display and an experimental browser.

In this tutorial we’ll look at how to install, set up, and use Calibre on a Ubuntu 14.04 server. We’ll also take a look at how to use the calibredb command to create, customize, and maintain your ebook database right from the server.

For this tutorial we’ll cover:

  • Installing Calibre
  • Creating an ebook library, or importing an existing one
  • Making Calibre server a background service
  • Automatically adding new books to the library

By the end of this tutorial, you’ll have a small initial library to which you can easily add new books!

Prerequisites

Please make sure you have these prerequisites:

Examples in this tutorial are shown for a Droplet running a fresh installation of Ubuntu 14.04, but they should be easily adaptable to other operating systems.

Step 1 — Installing Calibre

Calibre is available from the APT software repositories, but as advised by its creators it is far better to install from the binaries provided on their website. Calibre is updated very frequently and the version in the repos tends to lag behind.

Luckily, the creators of Calibre have made this very simple to do. Just run the following Python command on your server. Before running the command, please double-check the official Calibre site in case the command has been changed.

Install Calibre (make sure you scroll to get the entire command):

sudo -v && wget -nv -O- https://raw.githubusercontent.com/kovidgoyal/calibre/master/setup/linux-installer.py | sudo python -c "import sys; main=lambda:sys.stderr.write('Download failed\n'); exec(sys.stdin.read()); main()"

You will notice some warnings about failed desktop integration, but these are safe to ignore, since you are installing Calibre on a remote server.

Step 2 — Installing Dependencies

The Calibre command line tool calibredb is used for various operations on your Calibre library, such as adding or importing books, and fetching metadata and covers for books.

We’ll take a look at how to use some of these commands later, but for now we’ll just install two dependencies. The first is ImageMagick, without which calibredb won’t run; and the second is xvfb which we’ll use to run calibredb in a virtual X display server – in order to sidestep issues caused by running Calibre in a non-display environment.

To install these just run the following commands.

Update your package lists:

sudo apt-get update

Install xvfb:

sudo apt-get install xvfb

Install ImageMagick:

sudo apt-get install imagemagick

Step 3 — Creating the Library

Now we’re almost ready to start running the server. We need to get some books to serve, however.

You may well have your own ebook library already, so we’ll look at two ways of doing this.

  1. Add ebook files directly; we’ll grab a couple from Project Gutenberg
  2. Import an existing Calibre library; useful if you’re already running the desktop version of Calibre

Getting Books

First let’s make a directory for our Calibre library. This example creates the directory in your user’s home directory, although you could place it anywhere on the server. Run the following commands:

mkdir ~/calibre-library
mkdir ~/calibre-library/toadd

We’ve created two directories: the first, ~/calibre-library is the one that Calibre will organize automatically, while we’ll add books manually to the toadd sub-directory. Later, we’ll take a look at how to automate this process too.

How we’ll grab some books from Project Gutenberg. For this tutorial we’ll download Pride and Prejudice by Jane Austen and A Christmas Carol by Charles Dickens.

Change to the toadd directory to get started.

cd ~/calibre-library/toadd

Download the two ebooks:

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

Calibre relies somewhat on file extensions to correctly add books, so the -O flag in the wget command specifies a more friendly filename. If you downloaded a different format from Gutenberg (such as .epub) then you need to change the file extension accordingly.

Adding the Books to Calibre’s Database

Now we need to add these books to the Calibre database using the calibredb command through the xvfb virtual display we installed earlier. To do this, run:

xvfb-run calibredb add ~/calibre-library/toadd/* --library-path ~/calibre-library

The asterisk means that Calibre will add all books found in the toadd directory to the library, in the calibre-library directory. You might see an error about not finding a cover (we chose to download the .mobi files without images), but you should also see confirmation that the books were added to the Calibre database.

Sample output:

Failed to read MOBI cover
Backing up metadata
Added book ids: 1, 2
Notifying calibre of the change

That’s all we need to start seeing the first results. Let’s test out the server. Run:

calibre-server --with-library ~/calibre-library

The command won’t produce any output, but will just appear to hang in your terminal. This is fine for now; we’ll look at daemonizing it properly later. Now open a web browser and navigate to:

  • http://your_server_ip:8080

Replace your_server_ip with your Droplet’s IP address. You should see the main page of your library, looking similar to the screenshot below.

Calibre home page

If you click on the All books link, you should see the two books that we added earlier. You can click on the Get button below either book to download it.

Viewing Your Books

Uploading an Existing Calibre Library

If you’re already running the desktop version of Calibre and already have your library set up, you can import it to your server easily.

Double-check your current library folder for a file called metadata.db. If this file exists, then everything should just work without any additional configuration.

Upload your entire library folder to your server.

Then, run this command:

calibre-server --with-library /path/to/calibre-library

This will add your existing library in its entirety to the server. You can add more books to it on the server by placing the book files in the toadd directory, as explained in this tutorial.

Step 4 — Making Calibre a Background Service

We don’t really want to keep a shell open with the calibre-server command running in it just to keep the server running.

While we could add the --daemonize flag to the command, there are better ways to do it. Below we’ll look at how easy it is to make calibre-server into a service so that it will automatically start on system reboot and so that we can very easily start, stop, or restart the process.

Until recently, the way to achieve this was to write complex scripts and put them in the /etc/init.d/ directory. The currently recommended way is to use a far simpler Upstart script, which is a .conf file placed in the /etc/init/ directory. We’ll take a look at how to do this:

If the server is still running, hit CTRL + C in your terminal to stop it.

Now create a new configuration file:

sudo nano /etc/init/calibre-server.conf

Create the Upstart script, being sure to replace the variables marked in red:

description "Calibre (ebook manager) content server"

start on runlevel [2345]
stop on runlevel [^2345]

respawn

env USER='myusername'
env PASSWORD='mypassword'
env LIBRARY_PATH='/home/user/calibre-library'
env MAX_COVER='300x400'
env PORT='80'

script
    exec /usr/bin/calibre-server --with-library $LIBRARY_PATH --auto-reload \
                                 --max-cover $MAX_COVER --port $PORT \
                                 --username $USER --password $PASSWORD
end script

Paste this into your text editor and save it. (CTRL + X, then Y, then ENTER). We’ll look at what each line does below:

  • The first line is just a description to help you (or others) know what the script does
  • The next two lines state at what level you want your script to start and stop on, since Upstart, allows for order specification so that scripts that rely on each other will start in the right order. Level 1 is for all essential services, so we’ll start on level 2, by which time we know that the network and anything else we need will be up and running
  • respawn means that if the service stop unexpectedly, it’ll try to restart

The next lines are all variables that we pass to the calibre-server command. Before, we just used the minimum of specifying the --with-library option, but we can see now how much flexibility Calibre offers. Above, we’ve specified:

  • Username and password to access the library from the web (please change these from the examples provided)
  • Library location path, as before
  • Max image size for book cover images (this is useful to make the page load more quickly)
  • Port number (here we’ve changed it to 80; change this to something else if you already use port 80 to serve standard web pages, etc.)
  • Finally, in the script section (known as a stanza) we run the main command using exec, and passing in all our variables. The /usr/bin/calibre-server part is the path to the executable

Once you’ve saved the script and closed the editor, start up the server:

sudo start calibre-server

This time you should see this output, but with a different process number:

calibre-server start/running, process 7811

Now use a browser to navigate to your server’s IP address or domain name.

You should see a popup form asking for the username and password. These should be the ones you added to the Upstart script. Enter these and you’ll be taken to your ebook library as before.

The server can now easily be stopped, started, and restarted using the following commands:

sudo service calibre-server stop
sudo service calibre-server start
sudo service calibre-server restart

This makes managing the server a lot easier than having to manually deal with daemon processes and process IDs!

The site by default has a mobile version that works nicely with smaller-screen devices such as phones and e-readers. This should load automatically if you visit the site from a mobile device.

Step 5 — Creating a Cron Job to Add Books Automatically

We can write a simple cron job to watch our toadd directory for new books.

Every 10 minutes it will look for files in the /home/user/calibre-library/toadd/ directory, add any files in there to our Calibre database, and then remove the original files. (Calibre makes copies of the files when it adds them to our library so we don’t need the originals once the add has taken effect.) This means that if you transfer book files via scp, ssh, etc. to this directory from your main machine, or just download them directly into the toadd directory, then they’ll automatically be added to your Calibre database and be available for download from your library!

To create a cron job, execute:

crontab -e

You might have to make a selection about your preferred text editor.

At the end of the file add the line:

*/10 * * * * xvfb-run calibredb add /home/user/calibre-library/toadd/ -r --with-library /home/user/calibre-library && rm /home/user/calibre-server/toadd/*

The first part of the command (*/10 * * * *) means that the command should be run every ten minutes. The second part is the same as the command we manually ran earlier. It adds all the books from the toadd folder to the database and then removes the original files.

That’s that. You can now access your ebooks from anywhere in the world.

Note: The search results in Calibre aren’t sorted by relevance, so if you enter a common term you often find unrelated books before the one you’re looking for. However, you can specify to search only by title or author, which does help a lot, and the browse options (browse alphabetically by Author, for example) are very well implemented as well.

Conclusion

There are one or two things to keep in mind about running and maintaining a Calibre server. We’ll take a brief look at these to finish off.

If you are only hosting books from Gutenberg or similar sites (i.e., books that are out of copyright), then there is little to say. Just make sure you follow the Gutenberg terms of service. Specifically, if you give access to your book collection to others, make sure to read the section of Gutenberg’s TOS regarding redistribution.

If you are hosting commercially-purchased books, remember that they’ll probably have DRM (Digital Rights Management) and will therefore only be readable from your registered device.

It goes without saying that you should never host pirated or illegal books on your ebook server.

Updates

Calibre pushes updates very frequently. Although most of these are bug fixes and functionality updates, some might be to do with security. It is therefore recommended that you keep up with the updates.

If an important update is published you should manually update the server software. (Again, the APT repos tend to lag behind, so it is not recommended to rely on them for updates).

Security

Even if you don’t publish the IP address of your Droplet, it may be discovered by scripts scanning for open ports. Because the Calibre login functionality does not allow automatic lockout after a number of incorrect attempts, there is a possibility of a brute force attack. To mitigate against this, it is highly recommended that you:

  • Don’t use a common username such as admin, calibre, or ebooks
  • Don’t use a common or short password, and definitely don’t use your username as a password
  • Consider running Calibre on a non-standard port, instead of port 80

And that brings our tutorial to a close. We hope that you enjoy accessing your ebooks from any location or device!

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?
 
10 Comments


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!

Hey, great guide.

I am using a headless ubuntu server for this project, and I have a few questions:

  1. using “calibredb add…” does not update the library.

  2. is there any way to auto convert all the books since there is no option to convert them on the webui. I have a solution for this by using the included “ebook-covnert” however I can only convert before adding and also only in one format.

Cheers.

Hi, Thanks your article. I got worked. A question is about the specific language. My ubuntu is none-English interface which is installed the specific fonts and LANG=my specific language. Currently, if I run “sudo calibre-server --with-library ~/calibre-library” this command line after stoping the calibre-server script as you mention at the end of this article, it shows up the specific language. On the contrary, it didn’t. What do I add into the script for showing my specific language interface? Thanks.

“couple of bits of obsolete advice in that digital ocean writeup. You dont need xvfb-run any more and you dont need to install imagemagick.” http://www.mobileread.com/forums/showpost.php?p=3399027&postcount=2

Looks like new ubuntu 16.04 xenial has added “calibre” to it’s package base http://www.ubuntuupdates.org/package/core/xenial/universe/base/calibre

Now if I could just find the rest of the configuration instructions…

Great trick!

It didn’t work for me until I added an equal sign (=) at the exec line for each argument.

I also removed the $USERNAME and $PASSWORD arguments, since my library is accessible only from within the LAN.

This comment has been deleted

    I followed this tutorial and it was great… but my CPU usage was through the roof. It turns out to have been the “–auto-reload” argument in the upstart script which (according to the author of Calibre) is “a definite no-no in production”! The docs suggest this is for reloading the executable if it is replaced by a new version (i.e. if it is constantly recompiled as in a development environment).

    Also, he told me that advice to use of xvfb is now “obsolete” and this step in the tutorial is no longer necessary.

    This comment has been deleted

      I had an issue where sudo start calibre-server showed that it started, but the process immediately stopped. The issue was a permissions issue with the folder of the library you create. Whatever upstart runs calibre-server as, it can’t access that folder, therefore it stops.

      The fix is to simply add

      setuid *username*
      setgid *username*
      

      directly under the runlevels in the conf file.

      Hope that helps someone else~

      OK, couple of questions, I’ve been running the standard GUI version of calibre for years so:

      1. is it possible to run more than one library (I have a few pertaining to different things) per server or do I have to run a separate server for each library?
      2. Can the calibre-server service coexist with the GUI version? i.e. if I use the GUI version to update the meta-data of a book in a library also shared by the command line version, with everything continue to work, or will things break?

      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.