Skip to main content
  1. Blogs/

Setting up Thumbor on Ubuntu

·671 words·4 mins
Tech thumbor ubuntu resize images open source

Hello friends 👋🏾 In this article I will walk you through how to set up thumbor, a smart imaging service that enables on-demand crop, resizing and flipping of images.

Introduction #

Thumbor is an open-source imaging tool that leverages state-of-the-art face and feature detection algorithms to enhance image cropping and resizing. Furthermore, this tool comes with a variety of filters like watermarking, blurring, sharpening, Upscaling, etc.

Prerequisites #

  • Ubuntu >=18.04
  • Python >=3.7

Installation #

When it comes to installing Thumbor on linux there are a couple of options:

  1. Installing using aptitude
  2. From the source of a stable release
  3. From the latest version of the source

In this article, we will use the third option since it is the easiest and most reliable. First, we will clone the official Thumbor repository on our server.

git clone https://github.com/thumbor/thumbor.git

We should now have a folder named thumbor on our active directory. Next, we need to run the setup.py script using Python to install Thumbor.

cd thumbor
python3 setup.py install

If you encounter a dependency error during this step, don’t panic. You need to install the specific library version and rerun the setup. Use the following command as an example:

pip install webcolors==1.11.1

You should now have thumbor installed in your system. To confirm this run this command

thumbor --version

The expected output of this command should be the installed version of thumbor. At the time of writing this article, my output is Thumbor v7.5.2 (07-Jul-2023).

Configuration #

Awesome 🥳, now that we have thumbor installed we can start playing around with it and configure it for optimal performance. First, let’s confirm that it is working. To do this we resize the this image to 100 x 100 by running this URL in our browser.

http://YOUR_VIRTUAL_MACHINE:8888/unsafe/100x100/https://github.com/thumbor/thumbor/raw/master/example.jpg

Where YOUR_VIRTUAL_MACHINE is your server’s IP address. Note that by default thumbor runs on port 8888. This means that you will need to update your firewall to open this port.

HTTPS setup #

You might have noticed the keyword unsafe in your URL. What this means is that Thumbor is currently running in an insecure mode. This is of course okay if you are using it in development but we in most cases want to have it in production. Therefore, you will need to enable HTTPS as follows;

Run the following command to get a commented configuration file in the Thumbor directory you cloned

thumbor-config > ./thumbor.conf

Open the thumbor.conf file with your preferred text editor and uncomment the SECURITY_KEY property by removing the ‘#’ symbol. Set a secure key for enhanced security. Next, uncomment the ALLOW_UNSAFE_URL and set it to False.

Thumbor as a service #

For a production server, we require that thumbor be restarted when the server restarts on failure. We can achieve this behavior by creating a service file in the systemd directory as follows Create the file thumbor.service file as follows

vi /lib/systemd/system/thumbor.service

Next, paste this code into the file

[Unit]

Description=Service for Thumbor image CDN

Documentation=https://thumbor.readthedocs.io/en/latest/

After=network.target

[Service]

ExecStart=/usr/local/bin/thumbor --conf /home/ubuntu/Downloads/thumbor/thumbor.conf

Restart=on-failure

[Install]

WantedBy=multi-user.target

In the thumbor.service file, under the ExecStart directive, specify the correct path to your thumbor.conf file to ensure Thumbor operates correctly. Make sure to adjust the path based on your server setup. The Restart=on-failure directive ensures that Thumbor restarts automatically if it encounters any failures.

We can now test our thumbor instance on the terminal using a tool called thumbor-url which comes with thumbor.

thumbor-url github.com/thumbor/thumbor/raw/master/example.jpg -w 100 -e 100 -k My_thumbor_key

this will give you an encoded URL as below which you can then use to load your image on a browser. /kt57ssrqRnIVEPnqwQeol-NOZsg=/100x100/github.com/thumbor/thumbor/raw/master/example.jpg

Conclusion #

You might be asking yourself, how do you integrate this into your codebase? Well, fear not, Thumbor has an active community that has created libraries that you can use in your code to generate the encoded URL by just providing your secret key. In conclusion, this tool has a lot of potential use cases that could help you cut down on costs when it comes to image manipulation.

Until next time au revoir!👋🏾