Current Location: Home> Latest Articles> How to Install PHP MongoDB Extension via PECL

How to Install PHP MongoDB Extension via PECL

gitbox 2025-06-30

Preparation

Before starting the installation of the PHP MongoDB extension, ensure your environment meets the following requirements.

PHP Version

It is recommended to use PHP 7.0 or higher, and make sure you have phpize and php-config tools installed. You can check if these tools are installed using the following commands:

php -v
phpize -v
php-config -v

MongoDB Driver Library

The PECL installation method requires the MongoDB C driver library to provide the underlying interface for PHP and MongoDB. On Redhat or CentOS systems, you can install the MongoDB C driver using the following command:

<span class="fun">yum install -y mongodb mongodb-devel</span>

Download the Extension

You can easily install the MongoDB extension via PECL using the following command:

<span class="fun">pecl install mongodb</span>

Configure PHP

Once the extension is downloaded, you need to add it to your PHP configuration file. If PHP was installed via yum, open the PHP configuration file:

<span class="fun">vim /etc/php.ini</span>

In the php.ini file, find the Dynamic Extensions section and add the following line:

<span class="fun">extension=mongodb.so</span>

Restart Web Services

To apply the changes, restart the web services with the following command:

<span class="fun">service httpd restart</span>

Test the Extension

After the installation, you can check if the MongoDB extension is successfully loaded using the following code:

<span class="fun">var_dump(extension_loaded('mongodb'));</span>

If it returns true, the MongoDB extension has been successfully loaded.

Conclusion

These are the steps to install the PHP MongoDB extension via PECL. During the installation, make sure all necessary dependencies are installed and that you are using a compatible PHP version to ensure a smooth installation process.