Laradock: how to enable/install php7 ldap support extension?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Laradock & LDAP Integration: Solving the Missing Extension Mystery in Docker

As developers working with containerized applications, especially those leveraging frameworks like Laravel, we constantly face the challenge of integrating specific system dependencies. When setting up services via tools like Laradock, we often encounter situations where the base images are functional but lack necessary extensions required by our application code. This post walks through a very common hurdle: enabling PHP LDAP support within a Dockerized environment to resolve errors encountered when using packages like adldap2/adldap2-laravel.

The LDAP Extension Dilemma in Docker

You have successfully set up your environment using Laradock and are utilizing the adldap2/adldap2-laravel package. However, you run into an error: Call to undefined function Adldap\ldap_explode_dn(). This is a classic symptom indicating that while the PHP application code expects an LDAP extension to be present, the underlying PHP installation within your Docker container has not successfully compiled and enabled the necessary modules.

The issue isn't with the Laravel package itself, but with the PHP environment setup inside the container. Standard Linux repositories often do not include these specialized extensions by default, forcing us to compile them manually. Trying to install system packages via apt-get usually only installs the underlying libraries (libldap2-dev), not the actual PHP extension modules themselves.

Why Modifying the Dockerfile is the Correct Approach

When working with containerized Laravel applications, customizing the base image is the most robust and reproducible solution. Instead of trying to execute commands inside a running container (which can be messy), we should bake the required dependencies directly into the build process. This ensures that every time you build your application, the LDAP support is correctly compiled and available.

The key lies in modifying the Dockerfile used by your PHP image to explicitly install the necessary development headers and then compile the specific extension for PHP. As a reminder, ensuring proper dependency management is central to building reliable software ecosystems, much like the principles guiding modern Laravel development found on laravelcompany.com.

Step-by-Step: Installing the LDAP Extension via Dockerfile

To resolve the undefined function error, we need to manually compile and install the PHP LDAP extension within the build stage of our Docker image. This process involves leveraging Debian's package manager (apt) to get the necessary development headers before instructing the PHP extension installer to use them.

Here is the corrected sequence for installing the LDAP extension in a php-fpm based image:

Corrected Dockerfile Implementation

We focus on adding the steps required to install the LDAP extension within the build context of your base image. Notice how we incorporate the necessary system dependencies (libldap2-dev) and then use the official PHP tools (docker-php-ext-configure and docker-php-ext-install).

# ... (Previous setup lines)

#####################################
# LDAP Installation Block
#####################################
RUN \
    apt-get update && \
    apt-get install -y libldap2-dev && \
    # Configure the ldap extension, pointing it to the necessary library paths
    docker-php-ext-configure ldap --with-libdir=lib/x86_64-linux-gnu/ && \
    # Install the actual LDAP module for PHP
    docker-php-ext-install ldap

# ... (Rest of your Dockerfile commands)

Explanation of the Fix

  1. apt-get install -y libldap2-dev: This step installs the development package, which contains the header files and libraries needed for compiling PHP extensions against the LDAP library.
  2. docker-php-ext-configure ldap --with-libdir=lib/x86_64-linux-gnu/: This command tells the PHP extension installer where to find the required system libraries, ensuring proper linking during compilation.
  3. docker-php-ext-install ldap: This is the final step that invokes the PECL installation process to compile and install the LDAP module directly into the PHP environment.

By executing these commands sequentially within a single RUN instruction in your Dockerfile, we ensure that the dependencies are installed and the extension is compiled before the container is finalized, eliminating the runtime error you were facing.

Conclusion

Enabling specialized extensions in Docker environments requires shifting focus from simple package installation to comprehensive dependency management during the image build process. By understanding how PHP extensions compile against system libraries—as demonstrated by the sequence above—you gain the power to customize your containers precisely how you need them. This practice of building tailored, secure images is fundamental to deploying robust applications, whether you are working with Laravel or any other framework on laravelcompany.com. Happy coding!