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

83,771

Solution 1

In a comment that appears to have been deleted later, someone pointed me to https://github.com/python-pillow/Pillow/blob/c05099f45c0d94a2a98c3609a96bdb6cf7446627/depends/alpine_Dockerfile. Based on that Dockerfile I modified my own as follows:

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

Now it builds successfully.

Solution 2

For debian

sudo apt install libjpeg-dev zlib1g-dev
pip install Pillow

Solution 3

For macOS:

brew install libtiff libjpeg webp little-cms2

Reason: https://pillow.readthedocs.io/en/latest/installation.html

Solution 4

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.

Solution 5

have you seen this QA? Fail during installation of Pillow (Python module) in Linux

they say you have to update your pip and install libjpeg-dev.

Share:
83,771

Related videos on Youtube

Kurt Peek
Author by

Kurt Peek

Hi, I'm Kurt Peek, a backend engineer at Apple.

Updated on April 14, 2022

Comments

  • Kurt Peek
    Kurt Peek about 2 years

    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?

    • Shoo Limberger
      Shoo Limberger over 3 years
      just add: apk add jpeg-dev
  • Boris Mossounov
    Boris Mossounov over 6 years
    After carefully reading error I noticed part "The headers or library files could not be found for jpeg". Those headers are held in *-dev packages. So I went to pkgs.alpinelinux.org/packages?name=libjpeg*&branch=edge and in the end "apk --no-cache --update-cache add libjpeg-turbo-dev zlib-dev" did the trick.
  • Ryan Allen
    Ryan Allen over 6 years
    apk add jpeg-dev is all that you need to fix the "The headers or library files could not be found for jpeg" issue
  • Ryan Allen
    Ryan Allen over 6 years
    They don't mention the alpine based solution: apk add jpeg-dev
  • Jirka Hubáček
    Jirka Hubáček almost 5 years
    I got out with less of linux packages: FROM python:3.7-alpine RUN apk --update add gcc libgcc musl-dev jpeg-dev zlib-dev Builds faster and fetches less of binaries.
  • Carsten
    Carsten over 4 years
    apt-get does not exist on Alpine Linux, the package manager is apk.
  • Dudnikof
    Dudnikof over 4 years
    Even in Ubuntu apt is now the preferred package manager for end user askubuntu.com/a/446484
  • Code-Apprentice
    Code-Apprentice almost 4 years
    I only had to install libjpeg-dev on Ubuntu 20.04
  • egvo
    egvo almost 4 years
    I suppose there are many excess packages (114Mb!) but it worked for me, thanks!
  • Arier
    Arier over 3 years
    sudo dnf install libjpeg-turbo-devel libjpeg-turbo-utils for Fedora 32
  • Shirish Hirekodi
    Shirish Hirekodi over 3 years
    I used this answer on Raspberry Pi 4b and it worked
  • ethanfox27
    ethanfox27 over 3 years
    This got pillow working for me! Working in Python 3.9 and going through Django 3 by Example Third Edition.
  • Lonely
    Lonely over 3 years
    if different brew prefix then use arch -arm64 brew install libtiff libjpeg webp little-cms2
  • aheze
    aheze almost 3 years
    Same, works great on Raspberry Pi 4b! Thanks so much!
  • Sandeepa Kariyawasam
    Sandeepa Kariyawasam over 2 years
    libjpeg-turbo-devel worked for redhat7
  • Lauri Elias
    Lauri Elias over 2 years
    I only needed libjpeg.
  • David Alford
    David Alford over 2 years
    This works for using Pillow with Docker.