Explain Codes LogoExplain Codes Logo

No such file or directory "limits.h" when installing Pillow on Alpine Linux

python
package-management
alpine-linux
dependencies
Anton ShumikhinbyAnton Shumikhin·Oct 14, 2024
TLDR

To swiftly resolve the "limits.h" missing file error on Alpine Linux, execute the following command:

apk add build-base

The build-base package is a compilation of essential developer tools, including the GCC compiler (gcc), libraries, and headers (libc-dev). These provide necessary files like limits.h, a key element for compiling Python packages like Pillow.

Understanding the issue and the solution

Alpine Linux is unique in its use of musl libc, which differs from the more common glibc used by many other Linux distributions. The musl-dev package supplies development resources for musl libc, including headers like limits.h.

When you install Pillow, it attempts to compile certain native extensions that require these particular headers. Therefore, installing the musl-dev package is necessary.

Typically, installing build-base covers all requirements. But, if you need to be more explicit with your packages, consider installing the following:

apk add python3-dev musl-dev libc-dev linux-headers # Don't forget to invite these to your install party! 🎉

Here, python3-dev includes header files for Python development, while libc-dev and linux-headers supply general C development headers. This combination ensures a more comprehensive range of development files, covering not just Pillow but other Python packages with similar necessities.

Efficiency hacks: --no-cache

While installing packages, apk add offers a --no-cache shortcut, a simple yet effective way to save precious disk space by preventing package caching:

apk --no-cache add build-base # Who needs caching when you are this efficient, eh?

This option is particularly advantageous in space-limited environments like Docker containers.

Up-to-date prerequisite tools

Before building Pillow, ensure your pip and setuptools are current. In several instances, problems with package building can be resolved just by updating these tools:

pip install --upgrade pip setuptools # Because fresh tools cut cleaner.

Compatibility-aware installation

If you're working with Docker or Raspberry Pi, acknowledge that Alpine packages may offer greater compatibility and stability over their pip counterparts. This is primarily due to the musl libc dependencies:

apk add py3-pillow # Alpine's way of saying "I'm here for you"

Remember to double-check the case sensibility and correct naming while dealing with package installations.

A proactive approach: Checking for dependencies

Package installations can fail due to missing dependencies. Always check the full list of dependencies for your package, which might include certain developer libraries or specific versions of other packages.