Composer/PHP: How to check if composer package is installed?

Composer/PHP: How to check if composer package is installed?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Checking Composer Package Installation Without Autoloading or Exec

Determining whether a specific Composer package is installed within your PHP application is a common requirement, often arising when implementing optional dependencies or conditional feature flags. The goal is to write a function like hasComposerPackage(string $packageName) that returns a boolean value reliably, without triggering expensive autoloading mechanisms or spawning external processes.

The most robust and standard approach relies on inspecting the Composer metadata files located in the vendor directory. Specifically, you should look for the presence of a specific file generated during the installation process rather than trying to load the package's class or check the vendor/composer/installed.json directly via file I/O logic that might bypass Composer's own tracking.

The definitive indicator of an installed package is the existence of the vendor/composer/autoload_files.php or, more simply and universally, checking the composer.lock file combined with the actual presence of the package's directory in the vendor folder. However, a cleaner method that avoids reading the lock file manually (which can be complex to parse across versions) is to check for the specific directory structure Composer creates.

A reliable implementation checks if the vendor directory exists and then verifies the existence of the package's directory within it. This approach minimizes overhead because it relies solely on the file_exists function, which operates at the OS level without loading any PHP classes or triggering the autoloader chain.

Here is a practical implementation pattern:

function hasComposerPackage(string $packageName): bool
{
    // Normalize the package name to match the directory structure
    // e.g., 'vendor/package-name' becomes 'vendor/package/name'
    $normalizedPath = str_replace('/', DIRECTORY_SEPARATOR, 'vendor/' . $packageName);
    
    // Construct the full path assuming vendor is in the current working directory or project root
    // Adjust based on how your application resolves its root.
    $packageDir = __DIR__ . '/..' . DIRECTORY_SEPARATOR . $normalizedPath;

    return is_dir($packageDir) && !empty($packageDir);
}

This function normalizes the package name by replacing slashes with directory separators and prepends the vendor folder. It then checks if that specific directory exists on the filesystem. This method ensures that you are checking for the physical presence of the installed files, which Composer only creates after a successful install command.

One critical trade-off to consider is how you determine the root of your vendor directory. In monolithic applications where composer.json resides in the project root, assuming the current working directory or using __DIR__ relative to the project root is usually safe. However, in complex architectures like Laravel, which often places the vendor folder outside the application's public directory or within a specific subdirectory (e.g., /app/vendor), you must adjust the path resolution logic accordingly. If you are working within a framework that manages its own vendor path, hardcoding __DIR__/../vendor might be incorrect if your entry point is deeper in the hierarchy.

Another potential pitfall involves package names that include version constraints or specific subdirectories. The check must strictly rely on the canonical directory name as defined in the package's source repository, not the version string included in the dependency declaration. For instance, checking for vendor/phpunit/phpunit is correct regardless of whether you installed version 9.x or 10.x, because the directory name remains consistent unless a major version bump changes the namespace structure significantly.

If your application runs in an environment where the current working directory might vary (such as a CLI script invoked from different locations), relying on relative paths can lead to false negatives. In such scenarios, it is often better to resolve the path based on the location of the composer.lock file if it exists, ensuring the check is context-aware regardless of how the PHP process was started.

By adhering to filesystem checks for directory existence, you avoid the latency of requiring Composer's autoloader and prevent the security implications or performance costs associated with spawning exec calls to php composer require. This keeps your application lightweight and ensures that the presence check is a direct reflection of the actual deployment state.

Our network