Backend Development 4 min read

How to Compile and Install the MongoDB PHP Extension

This guide walks through the step‑by‑step process of downloading, compiling, and enabling the MongoDB extension for PHP on a Linux server, covering both online and offline installation methods and how to verify the extension is loaded successfully.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
How to Compile and Install the MongoDB PHP Extension

The article explains how to compile and install a PHP extension, using the MongoDB extension as an example; the same procedure applies to other extensions.

Step 1: Open https://pecl.php.net and search for "mongodb".

Step 2: Download the latest stable version of the extension.

Step 3 (online): If the server has internet access, run the following commands:

wget https://pecl.php.net/get/mongodb-1.19.4.tgz
# Extract the archive
tar -zxvf mongodb-1.19.4.tgz
# Enter the directory
cd mongodb1.19
# Prepare the build environment
phpize
# Configure with php-config
./configure --with-php-config=/usr/bin/php-config
# Compile and install
sudo make && make install
# Enable the extension in php.ini
extension=mongodb.so
# Restart the web server
service apache2 restart   # or appropriate restart command

Step 3 (offline): If the server cannot access the internet, download the package locally, upload it to the server, and then execute the same sequence of commands (extract, phpize, configure, make, install, enable, restart). The commands are identical to the online case.

// Extract the file
tar -zxvf mongodb-1.19.4.tgz
// Enter the directory
cd mongodb1.19
// Prepare the build environment
phpize
// Configure
./configure --with-php-config=/usr/bin/php-config
// Compile and install
sudo make && make install
// Enable in php.ini
extension=mongodb.so
// Restart the server
service apache2 restart

Step 4: Verify the installation by listing loaded PHP modules and locating the php.ini file:

php -m   # should list mongodb among the modules
php --ini   # shows the loaded configuration file

After completing these steps, the MongoDB extension will be installed and ready for use in PHP applications.

backendLinuxPHPextensionInstallationMongoDB
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.