"The headers or library files could not be found for jpeg" installing Pillow on Alpine Linux

Viewed 93723

I'm trying to run Python's Scrapy in a Docker container based on python:alpine. It was working before, but now I'd like to use Scrapy's Image Pipeline which requires me to install Pillow.

As a simplified example, I tried the following Dockerfile:

FROM python:alpine
RUN apk --update add libxml2-dev libxslt-dev libffi-dev gcc musl-dev libgcc openssl-dev curl
RUN apk add libjpeg zlib tiff freetype lcms libwebp tcl openjpeg
RUN pip install Pillow

However, when I try to build this I get an error which contains the following:

Traceback (most recent call last):
  File "/tmp/pip-build-ft5yzzuv/Pillow/setup.py", line 744, in <module>
    zip_safe=not debug_build(), )
  File "/usr/local/lib/python3.6/distutils/core.py", line 148, in setup
    dist.run_commands()
  File "/usr/local/lib/python3.6/distutils/dist.py", line 955, in run_commands
    self.run_command(cmd)
  File "/usr/local/lib/python3.6/distutils/dist.py", line 974, in run_command
    cmd_obj.run()
  File "/usr/local/lib/python3.6/site-packages/setuptools/command/install.py", line 61, in run
    return orig.install.run(self)
  File "/usr/local/lib/python3.6/distutils/command/install.py", line 545, in run
    self.run_command('build')
  File "/usr/local/lib/python3.6/distutils/cmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "/usr/local/lib/python3.6/distutils/dist.py", line 974, in run_command
    cmd_obj.run()
  File "/usr/local/lib/python3.6/distutils/command/build.py", line 135, in run
    self.run_command(cmd_name)
  File "/usr/local/lib/python3.6/distutils/cmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "/usr/local/lib/python3.6/distutils/dist.py", line 974, in run_command
    cmd_obj.run()
  File "/usr/local/lib/python3.6/distutils/command/build_ext.py", line 339, in run
    self.build_extensions()
  File "/tmp/pip-build-ft5yzzuv/Pillow/setup.py", line 545, in build_extensions
    raise RequiredDependencyException(f)
__main__.RequiredDependencyException: jpeg

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/tmp/pip-build-ft5yzzuv/Pillow/setup.py", line 756, in <module>
    raise RequiredDependencyException(msg)
__main__.RequiredDependencyException:

The headers or library files could not be found for jpeg,
a required dependency when compiling Pillow from source.

Please see the install instructions at:
   https://pillow.readthedocs.io/en/latest/installation.html

I went through the requirements on https://pillow.readthedocs.io/en/latest/installation.html and tried to find the corresponding packages for Alpine, although one I couldn't find was libimagequant, so this might be the 'culprit'. Nonetheless, it the traceback and error message seem to be saying that jpeg is missing, whereas I have installed openjpeg.

How can I modify the Dockerfile so that pip install Pillow runs?

12 Answers

I ran into this problem with docker image python:3.6-alpine I solved it by adding these packages apk add jpeg-dev zlib-dev.

Just in case anyone else is still struggling like I was you can see the official alpine Dockerfile for Pillow here: https://github.com/python-pillow/docker-images/blob/master/alpine/Dockerfile#L20

It states the following dependencies:

RUN apk --no-cache add python3 \

                   ...

                   # Pillow dependencies
                   jpeg-dev \
                   zlib-dev \
                   freetype-dev \
                   lcms2-dev \
                   openjpeg-dev \
                   tiff-dev \
                   tk-dev \
                   tcl-dev \
                   harfbuzz-dev \
                   fribidi-dev

In short, this helps:

RUN apt-get update -qq && apt-get install -y build-essential libsqlite3-dev \
  libpng-dev libjpeg-dev

Detailed:

I have the same error with python:3.8-slim-buster image. Solution presented by @pierangelo-orizio worked for me, but I just cleaned it to a minimal required packages list. So here is my Dockerfile:

FROM python:3.8-slim-buster

RUN apt-get update -qq && apt-get install -y build-essential libsqlite3-dev \
    libpng-dev libjpeg-dev

COPY requirements.txt .
RUN pip install --upgrade pip
RUN pip install -r requirements.txt

EXPOSE 8000
VOLUME /usr/src/app
WORKDIR /usr/src/app
CMD python manage.py runserver 0.0.0.0:8000

And requirements.txt:

Django>=2.1,<2.2
wagtail>=2.4,<2.5
django-cors-headers==2.5.3
python-dotenv==0.10.3

Run into the same issue on python 3.9. Fixed it by bumping pillow version in requirements.txt to Pillow>=8.0. There's a nice matrix of which pillow version supports which python version here.

enter image description here

If you're missing libjpeg.so package in runtime, try this (alpine linux):

apk add --no-cache jpeg

For yum users, this should work:

sudo yum -y install libjpeg-turbo-devel
pip3 install Pillow

Just adding "RUN apk add jpeg-dev" to Dockerfile fixed the problem for me.

Related