i am getting error : fatal error: 'openssl/sha.h' file not found while installing mongodb in mac m1 for the laravel project

 nssl.c:24:10: fatal error: 'openssl/sha.h' file not found
 #include <openssl/sha.h>
     ^~~~~~~~~~~~~~~
 1 error generated.
 make: *** [src/libmongoc/src/libmongoc/src/mongoc/mongoc-crypto-openssl.lo] Error 1
 ERROR: `make' failed

my OS Details are :

ProductName:        macOS
ProductVersion:     14.3
BuildVersion:       23D56

unix variant
local 23.3.0 Darwin Kernel Version 23.3.0: Wed Dec 20 21:30:27 PST 2023; root:xnu-10002.81.5~7/RELEASE_ARM64_T8103 x86_64

php version : PHP 8.2.4 

I issued the terminal command: sudo pecl install mongodb

this is from this site :
MongoDB PHP Driver

where my install has stopped with this error:

 nssl.c:24:10: fatal error: 'openssl/sha.h' file not found
 #include <openssl/sha.h>
     ^~~~~~~~~~~~~~~
 1 error generated.
 make: *** [src/libmongoc/src/libmongoc/src/mongoc/mongoc-crypto-openssl.lo] Error 1
 ERROR: `make' failed

I found the work around below from: Mac OSX fatal error: ‘openssl/sha.h’ file not found

cd /usr/local/include 
ln -s ../opt/openssl/include/openssl .

Quoting a Comment:

Apple deprecated its OpenSSL shared libraries a while back (with OS X 10.7). That’s because OpenSSL doesn’t offer release-to-release binary compatibility, so we can’t update the shared libraries to the latest OpenSSL without breaking all the existing clients.

but i even don’t have include folder in my mac … please help

I got the path to openssl by doing which openssl in terminal –
/opt/homebrew/bin/openssl

doing

    % ls -l /opt/homebrew/bin/openssl 

    lrwxr-xr-x  1 janammaharjan  admin  37 Feb  7 22:12 
    /opt/homebrew/bin/openssl -> ../Cellar/openssl@3/3.2.1/bin/openssl
Asked By: Janam Maharjan

||

The Proper Solution

Let’s start over and use an official install method for PHP, Laravel, and the MongoDB Extension. After all this you should have a working native MAMP server for Development.


Steps

These steps assume we want to use Homebrew natively for everything we need in our Development Environment, while keeping the macOS versions of the needed tools pristine.

  1. Install Homebrew (for completeness sake, you’ve already done this):
    • /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"One liner
  2. Install Node.js/npm:
    • brew install node
  3. Install newest PHP in Homebrew’s repository:
    • brew install phpIf you need a different version use the set identifier @. EX: brew install php@8.1
  4. Install Composer for dependency management:
    • curl -sS https://getcomposer.org/installer | php sudo mv composer.phar /usr/local/bin/composerOne Liner
  5. Verify Installation
    • php --version composer --version
  6. Install Laravel – This will be managed by Composer, and shouldn’t affect daily use:
    • composer global require laravel/installer
  7. Create an empty Project:
    • composer create-project --prefer-dist laravel/laravel <app-name>Be sure to replace app-name with the name of your project.
  8. Verify the Project Starts
    • php artisan serveCheck your browser at whatever IP Address you have setup on port 8000

Reference: How To Install Laravel on macOS

The above steps ensure this answer and your question, are now starting in the same place:

Add MongoDB

Quoting from the MongoDB Docs: Install the MongoDB PHP Library:

Note:

If the build process for either installation method fails to find a TLS library, check that the development packages (e.g. libssl-dev) and
pkg-config are both installed.

Since you’re question references this note, let’s fix it with:

  1. brew install libssl-dev && brew install pkg-config

Explanation

The libssl-dev package contains the missing header file. The pkg config package/script when installed in the Homebrew Cellar will search for Libraries for packages also installed in the Cellar. It will not report a library path outside of Homebrew back to make. After installing these 2 packages, the pecl command should complete as per MongoDB Laravel Integration

Answered By: eyoung100
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.