Installing a calibre server on a Raspberry Pi 4 (Ubuntu Server)
The scope of this little project is simple: run a calibre ebook server off one of my Raspberry Pi 4 units. This particular Pi is running Ubuntu Server 20.04 LTS, so if yours is running something else, adjust the commands appropriately and as needed. I prefer the headless ease of use with Ubuntu Server, but whatever distro you choose ought to be fine. I just wanted to save resources on my Pi, since running an X server still costs horsepower, and my Pi has other tasks it needs to do.
I decided to write all of this down for two reasons: 1. So that I wouldn’t forget when I inevitably have to do it all again, and 2. So that someone else out there (you reading this) could have an easier time of it than I did. This took hours of googling, compiling miscellaneous instructions into something relevant for my system, and getting countless errors back in the terminal. Granted, the latter was mostly due to typos and fat-fingering on my part.
Running calibre via the command line is much less intuitive than using the GUI method, so if you have access to a desktop environment on your Pi, then you’ll have an easier time of organizing, adding, and changing library locations. Like I said, I opted to go headless, so I’m stuck with using calibre’s CLI tool, calibredb.
I spliced together instructions from this tutorial, which walks you through some different steps versus what I will. However, the gist is the same and it offers some good structure, so I won’t fix what ain’t broke. However, one bone that I have to pick with this tutorial is the use of rysnc to copy your ebooks to the local calibre directory. When I did this, it nuked all of my ebooks. Poof, gone. They weren’t in the source directory on my NAS anymore, nor were they in the destination directory. They’re lost somewhere between my DiskStation and my Pi. Alas.
Because of that, I will be using the good ol’ cp command. It’s not as elegant, but it’ll work for now. I suggest using a test PDF first to make sure everything goes smoothly, then add your library later (which I’ll discuss calibredb syntax further on). Obviously, I take no responsibility if you break your machine. I’m not a Linux expert by any means, though I know enough to be dangerous. I will be using the commands that I typed to accomplish my end goal.
I won’t walk you through installing Ubuntu Server because it’s pretty easy. For sake of my sanity, I did change the default username from ‘ubuntu’ to ‘admin,’ though I left the home directory name the same because I like how it looks in the command line. Weird, I know. If you want Pi-specific instructions on getting Server up and running, change the username, and all that, I can probably write a quick how-to for you. It seriously takes all of half an hour at most.
Moving on, here’s the meat and potatoes of the whole affair.
- sudo apt install calibre
Once it’s done installing, you’ll need to set up a directory for the calibre library to call home.
- sudo mkdir ~/CalibreLibrary
Next, you need to get your ebooks copied over to the new calibre library. I keep mine on a NAS and share them via SMB. I will show you how I mount my NAS, though yours will be a different IP and directory.
- sudo mount -t cifs -o cred=~/.smbcredentials //192.168.1.2/library /mnt/XANA_NAS/library
- The cred=~/.smbcredentials part is me telling the OS where to find my credentials to login to my NAS. I do this so that my username and password aren’t just hanging out in fstab or my terminal window. While my paranoia may be unfounded, it gives me a feeling of extra security.
- You can learn how to create a smbcredentials file here, or just replace cred=~/.smbcredentials with username=USERNAME,password=PASSWORD
- If you’re storing the books on a connected hard drive or something, make sure it’s mounted properly.
Copy the ebooks to your CalibreLibrary folder.
- There are several ways to do this, but because I’m no Linux expert, I stick to what I know when I’m working with this sort of stuff. Again, my precise commands below will be different than yours. Replace my source directory with wherever your ebooks (or test PDF if you took my advice) are.
- sudo cp -r /mnt/XANA_NAS/library ~/CalibreLibrary
- This copies all the files and folders under the library directory on my NAS samba share to the calibre library folder I made earlier.
Done? Cool, let’s turn calibredb into a systemd service.
Creating a systemd service for calibreserver
The reason we’re doing this is so that you don’t have to trigger calibredb every time you want it to run. That takes up a terminal window and just doesn’t work for me. The solution is to create a systemd service for calibreserver to run in the background and it’s really simple.
Start by creating a new config file for the service.
- sudoedit /etc/systemd/system/calibreserver.service
- Note: sudo nano /etc/systemd/system/calibreserver.service works, too.
Enter the following (making adjustments to fit your system as needed):
Description=Run a calibre server on the local network
Exit nano (CTRL+X to exit, Y to save, Enter to finish) and start up the service.
- sudo systemctl enable — now calibreserver.service
Creating the calibre library via the command line
Alright, so we’ve created the calibre library and made a calibreserver.service to have it run in the background. Cool stuff. Here’s the part I really struggled with.
You’ll find the documentation for calibredb to be kinda okay. It’s functional, but the former technical writer in me wants more, especially specific examples for the commands and options. This was a huge source of frustration for me, trying to get everything up and running.
If you don’t know, you can head to the IP address your Raspberry Pi with the port 8080 to see what your library looks like right now. There oughtn’t be anything just yet, but that’s next. It’s time to spin up calibredb and get your library sorted.
There are a lot of options for calibredb and I won’t go into all of them here, partially because I’m too lazy and partially because I don’t know what all of them do yet. Yes, humility is one of my strong suits.
Note: The em dashes ( — ) you see here are supposed to be two separate dashes in the terminal. Medium just autocorrects two en dashes to an em dash. Which is nice most of the time, except here. There should be a space between the command and the global option (where you see the em dash).
The command you need to know is this: calibredb add — with-library /path/to/library /path/to/books
Okay, here’s where I’m a bit iffy on things. Earlier, I had you copy over your library (or test PDF) to ~/CalibreLibrary. The way I think that it works is that’s where the books live for calibre to serve up when you access the server later on. I’m still testing honestly, but I think you could use the following syntax:
- calibredb add — with-library /home/ubuntu/CalibreLibrary /home/ubuntu/CalibreLibrary
Again, I’m not 100% sure on that. If you play around with it and I’m wrong, let me know. When I get some new ebooks or have the motivation to get another test PDF, I’ll give it a shot. For now, I told calibredb’s add command to pull the books from my NAS. I’m working on automating this process. If you have tips, pray tell.
There are options with each command. The one I’ve used thus far is the author command, or the -a argument. This allows you to tell calibredb what the author is. For example, I bought a bundle of IT books from Humble, all of whom were from the same publisher. So when I added them to calibre, I said:
- calibredb add -a “No Starch Press” — with-library /home/ubuntu/CalibreLibrary /mnt/XANA_NAS/library/NoStarchPress
Alright, so that’s that. I have a functional calibre server running on a headless Ubuntu system. Using Moon+ Reader on Android, I can access my library easily, as long as I’m on my home network — outside the network is the next thing I want to tackle.
I prefer EPUB format, but I also have a Kindle. Unfortunately, I don’t know if it’s possible to use calibre’s conversion function via calibredb. At least at time of writing, I didn’t see it in the documentation. I also haven’t figured out to how remove remnants of old books or my test PDF from the library. Some data wasn’t corrupted my first time using rsync to copy everything over from my NAS, and calibre has held onto that old metadata for dear life. Again, this is a limitation of calibredb so if you have access to the GUI, you may not have this problem.
Have fun reading your stuff.