Featured image of post Raspberry Pi mini project: building a home eBook library using Calibre

Raspberry Pi mini project: building a home eBook library using Calibre

Building an eBook library using Calibre Content server and Ubuntu server on a Raspberry Pi 3A+.

Today I accidently found that there are a collection of eBooks laying in my hard drive, that I barely read. In addition, some friends shared with me kid books in various format (pdf, epub, mobi etc) and it’s simply too many to import them to iPad or Android tablet. I think it might be a good idea to build a home ebook library so that I can access the contents from different devices, e.g. mobile or tablet or Kindle. The Raspberry Pi is good for this purpose. Although I have the powerful RPi 4 but my 3 A+ has been sleeping in the gaming case for a long time, maybe it’s time to wake her up.

For the management system, of course we can use cloud storage (eg Google Drive) and install an ereader app on terminal devices. However, this will take up spaces on our device and downloading takes time as well. I used Calibre in the past and it can satisfy my needs.

1. install Ubuntu server on RPi

I used the 32 bit Ubuntu 20.04 Server image for Rpi 3. Here is the tutorial. After downloading, I flashed the image to a 32GB MicroSD card using BalenaEtcher and then edit the network-config (in the root of system-boot partition) to add WIFI name and password for connection. Just uncomment the following lines and add name/password:

1
2
3
4
5
6
7
wifis:
  wlan0:
  dhcp4: true
  optional: true
  access-points:
    "<wifi network name>":
      password: "<wifi password>"

Then eject SD card, insert it to Pi, and connect with the power supply. Wait for a few minutes until the green led blinks regularly. Then I checked the ip address (name “ubuntu”) on my router.

Then we can ssh into the RPi (“ubuntu” as default username and password) from our own computer and update the system:

1
2
3
4
5
# will prompt to change the initial password
ssh ubuntu@192.168.x.xx

sudo apt update
sudo apt upgrade

On Rasperry Pi 4 you might encounter an error like this:

Waiting for cache lock: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 1941 (unattended-upgr)

It is is a daemon that runs in the background and check for system updates automatically when you start your system. Just wait for a few minutes until it is finished.

Check by: ps aux | grep -i apt and run sudo apt upgrade again

ubu-pi

enable FTP

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
## install vsftpd
sudo apt install vsftpd

## run at start-up
sudo systemctl start vsftpd
sudo systemctl enable vsftpd

# allow ftp traffic
sudo ufw allow 20/tcp
sudo ufw allow 21/tcp

# enable write permission: write_enable=YES
sudo vi /etc/vsftpd.conf
sudo service vsftpd restart

Later we can use ftp client (eg. Cyberduck) to access the default user directory (e.g. /home/ubuntu) or use scp to transfer files, e.g. to backup calibre database locally.

1
2
# zsh to add \ before wildcard
scp -r ubuntu@192.168.x.xx:/home/ubuntu/calibreLib/\* .

2. Install and configure Calibre

This can be done using APT on the Pi. The installation takes >10min on Pi 3A+ and much shorter on Pi 4.

1
2
3
4
sudo apt-get install calibre
# xvfb and imagemagic are needed to deal with graphics
sudo apt-get install xvfb
sudo apt-get install imagemagick

Note that after installing xvfb, the system will boot with gnome manager (GUI). To disable GUI at boot use sudo systemctl set-default multi-user. (To use GUI sudo systemctl set-default graphical)

Setup the Calibre library directory and another folder to holder resource books

The library path can be under user’s directory (e.g., /home/ubuntu)

1
2
3
# setup library path and permission
cd ~
mkdir calibreLib

Setup another folder for uploading books to the library, eg. books downloaded or from external drive

1
2
3
4
5
6
# create a new directory to mount the drive and make it available to all users
cd /
sudo mkdir /exData
#sudo chmod -R 777 /exData
sudo chown ubuntu:ubuntu /exData/
sudo chmod ugo+rw /exData/

We can download a book here:

1
2
cd /exData
wget https://www.gutenberg.org/ebooks/1342.kindle.noimages -O pride.mobi

Alternatively, we can mount an external USB drive to this directory

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# check drives
sudo fdisk -l
# normally external usb is /dev/sda?
# mount external drive
sudo mount /dev/sda1 /exData

# find out UUID
ls -l /dev/disk/by-uuid
# find ou the GID
id -g ubuntu
# locate the UID
id -u ubuntu

# create an entry in /etc/fstab to mount external drive automatically
sudo vi /etc/fstab
# add a line
UUID=your_uuid /exData auto nofail,uid=your_uid,gid=your_gid,noatime 0 0

sudo reboot

adding books to calibre library

1
2
3
(xvfb-run) calibredb add /exData/pride.mobi --library-path ~/calibreLib
# or after mounting the usb
calibredb add /exData/* --library-path ~/calibreLib

managing the library

For the headless server, we need to define –with-library all the time, otherwise there will be an error about “unable to open database file”.

1
2
3
4
5
6
7
8
# list all books
calibredb list --with-library ~/calibreLib/

# remove book
calibredb remove [book id] --with-library ~/calibreLib/

# search
calibredb search author:unknown title:astro --with-library ~/calibreLib/

run calibre content server

calibre-server ~/calibreLib

Now we can go to https://192.168.x.xx:8080/ on any device in the same network to browse and read books.

calibre

But I found it really slow to start and stop the server on Pi 3A+. Probably the RAM does matter a lot (3A+ for 512MB / 3B+ 1GB vs 4GB) and maybe I should switch to my Pi 4 instead.

to start the server automatically on system boot

Here is the manual to start the server automatically on system boot

First we create a file /etc/systemd/system/calibre-server.service with the following content:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
[Unit]
Description=calibre content server
After=network.target

[Service]
Type=simple
User=ubuntu
Group=ubuntu
ExecStart=/opt/calibre/calibre-server "/home/ubuntu/calibreLib"

[Install]
WantedBy=multi-user.target

Replace ExecStart location accordingly by checking the location of calibre-server

1
2
ubuntu@ubuntu:~$ which calibre-server
/usr/bin/calibre-server

and then push it to system

1
2
3
4
5
6
7
# start server
sudo systemctl start calibre-server
# check status
sudo systemctl status calibre-server

# make it start at boot
sudo systemctl enable calibre-server

Finally, Calibre is very bad at converting PDF documents and sometimes the output is horrible. For a large amount of PDF to read maybe it’s better to store the files on cloud drive and read through a PDF reader app (I used Xodo and I like it).

20200627 Updata for PDF library

For PDF files, e.g. scanned books I now store them on my raspberry pi and share via samba. On the terminal device, e.g. mobile/tablet/computer there is usually PDF readers that can directly read the files.

I firstly attached an external USB drive (usb stick) with PDF files, MP3 and videos to my Pi (see section above). I mounted it to /media/usb1

install and configure samba

1
2
3
4
5
6
7
## Install samba

    sudo apt install samba samba-common-bin
    
### Configure samba

    sudo vi /etc/samba/smb.conf

add something like this (I shared my USB stick)

1
2
3
4
5
6
7
[Pi4USB]
path = /media/usb1
writeable=Yes
read only=no
create mask=0777
directory mask=0777
public=no

I set public access as no so we need to set up username and password to access the folder

sudo smbpasswd -a ubuntu

will prompt to set the password for ‘ubuntu’

May also need to change the owner of the shared folder:

1
2
sudo chown ubuntu:ubuntu /media/usb1
#sudo chmod ugo+rw

Now restart samba to apply changes

sudo systemctl restart smbd

Now we can access the shared folder on our computer, mobile or tablet.

I am using an Android phone and I found an app called Network Browser that supports smb share. In addition it also has build-in video player. I opened PDF files using Xodo because the sliding is really nice.

pi-smb

On iOS I tried the FE File Explorer and it also works great. It’s easy to read the PDF book while listening to the corresponding mp3.

fe-ios

comments powered by Disqus
CC-BY-NC 4.0
Built with Hugo Theme Stack