Signup/Sign In
MARCH 13, 2023

[SOLVED] fatal error: 'pcre2.h' file not found - macOS/Macbook (M1/M2 chip)

    If you are using pecl to install some PHP extension for your local development environment, or if you are installing some other software, tool or service on you macOSx machine, and you get the following error,


    fatal error: 'pcre2.h' file not found
    #include "pcre2.h"
    ^~~~~~~~~
    1 error generated.

    Then this article will help you solve the issue and move forward with your installation process.

    Steps to solve the error

    The first step is to install pcre2 using Homebrew. If you do not have Homebrew installed, first install that - How to install Homebrew in your Macbook.

    Once you have Homebrew installed, run the following command to install pcre2:

    brew install pcre2

    The pcre2 installation will be done in the following directory,

    /opt/homebrew/Cellar/pcre2/

    And you can use the cd.. command from the terminal to see if it is there or not.

    Now that we have pcre2, he have to provide this to our local PHP installation, to fix the error that you are getting.

    If you installed PHP manually, then look for the directory where PHP is installed, once you find that, navigate to, /php/php7.4.33/include/php/ext/ (change the version of PHP as per your installation).

    If you installed PHP using Homebrew, then you will find it at this location: /opt/homebrew/Cellar/php/

    And if you are using PHP from MAMP server, then you will find it here: /Applications/MAMP/bin/php/

    Now that you know your PHP installation, we will create a soft link for the pcre2 in the PHP extension directory, so that it gets picked up when you again try to install whatever you were installing when you got the error, and came to this article for resolution.

    Create Soft Link for pcre2

    Run the following command:

    ln -s /opt/homebrew/Cellar/pcre2/10.42/include/pcre2.h /Applications/MAMP/bin/php/php7.4.33/include/php/ext/pcre/pcre2.h

    The first path will remain the same, because that is for pcre2.h file that we just installed using Homebrew. The second path in the above command is for our PHP installation, so you will have to update the first half of that path, uptil /Applications/MAMP/bin/php/php7.4.33/ and provide your PHP installation's path (the rest of it will remain the same).

    Run the above command to create the soft link, and you are done.

    Conclusion

    Sometimes pcre2 is required when we try to install some tool or service using pecl, and because PHP installation doesn't come pre-installed with this, hence we get the error. But installing pcre2 separately and then creating the soft link for it in PHP directory, should fix this error.

    I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS