Installing PyMuPDF on MacOS Big Sur

Viewed 3443

I wanted to import fitz in my code. To do that, I tried installing PyMuPDF using

pip3 install PyMuPDF

However, this installation fails and returns this error:

fitz/fitz_wrap.c:2754:10: fatal error: 'fitz.h' file not found
#include <fitz.h>
         ^~~~~~~~
1 error generated.
error: command '/opt/homebrew/clang' failed with exit code 1

I also tried installing mupdf and mupdf-tools via Homebrew. None of them could fix this issue. I would appreciate any help to fix this installation error!

2 Answers

Assuming you are using M1 Mac, you can solve this problem installing mupdf, swig, freetype and latest version of PyMuPDF:

brew install mupdf swig freetype
pip install https://github.com/pymupdf/PyMuPDF/archive/master.tar.gz

Source: https://github.com/pymupdf/PyMuPDF/discussions/875

The instructions provided by Juan Garcia worked until June 18th 2022 for me. There's a few workarounds.

Solution #1

Install the wheel package using pip and swig + freetype using homebrew. PyMuPDF should build itself successfully when running pip install pymupdf. This takes about five minutes on a late model M1 MBP. This does not require the mupdf package installed via homebrew.

Solution #2

If you're using homebrew you can downgrade to 1.19.x with the following:

cd "$(brew --repo homebrew/core)" && git checkout 457cb59a52ff7666dc146c1c480d2ea98d63564f;

# If you already have mupdf installed use `brew reinstall` instead of `brew install`
cd "$(brew --repo homebrew/core)" && HOMEBREW_NO_AUTO_UPDATE=1 brew install mupdf;
cd "$(brew --repo homebrew/core)" && git checkout master && brew pin mupdf;

brew install swig freetype;

pip install https://github.com/pymupdf/PyMuPDF/archive/refs/tags/1.19.6.tar.gz;

For reference here is the Formula commit history: https://github.com/Homebrew/homebrew-core/commits/master/Formula/mupdf.rb

Related