Composer - run scripts only in dev environment

Composer - run scripts only in dev environment

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

Stefan Bogdanescu

Founder & Senior Architect

Composer - run scripts only in dev environment

Your composer.json currently contains a post-update-cmd script that performs heavy operations, such as cloning a Behat repository and setting up submodules. This logic is triggered every time you run php composer.phar update or even php composer.phar install. The goal here is to restrict these side effects so they only execute when explicitly updating development dependencies via php composer.phar update --dev.

Composer does not provide a native flag within the post-update-cmd array to conditionally execute commands based on whether the --dev flag was passed. To achieve this, you must leverage Composer's lifecycle events and the distinction between how composer install and composer update handle require versus require-dev.

The most robust approach involves splitting your installation logic into two distinct hooks: post-install-cmd, post-update-cmd, and post-root-package-install. However, a simpler and more direct solution for your specific use case is to move the Behat setup logic into a dedicated command that you invoke manually or via a separate script, rather than relying on the automatic post-install/update hooks.

Alternatively, if you are willing to structure your project dependencies differently, you can rely on Composer's default behavior regarding require-dev. By default, running php composer.phar install or php composer.phar update without flags only installs packages listed under require. Packages in require-dev, such as PHPUnit in your example, are ignored unless the --dev flag is present.

You can wrap your complex Behat setup logic inside a custom command defined in commands.json (Composer 1+) or by creating a simple executable script in your project root that calls Composer with the correct flags. For instance, create a file named install-behat.sh:

#!/bin/bash
# install-behat.sh

# Ensure we are only installing dev dependencies for this specific step
php composer.phar update --dev --no-interaction

# Now that vendor/bin/behat exists (or will exist), run the setup logic
# Note: You must ensure Behat is actually installed before running these commands.
# If Behat is a requirement, you might need to add it to require-dev first.

However, looking at your specific post-update-cmd, there is a fundamental architectural issue. The script clones the Behat repository into the current directory (git clone ...), then installs dependencies inside that local copy, and finally moves the entire Behat folder into vendor/. This violates the principle of keeping project code separate from vendor packages unless you are managing a monolithic bundle, which is generally discouraged in modern PHP development.

A cleaner implementation would define Behat as a package within require-dev or use a dedicated script directory. If you must keep the manual cloning and moving process, you can create a custom command in your composer.json under the scripts key using the commands feature if available, or simply rely on a shell alias.

If you are working within the Laravel ecosystem or similar frameworks where environment-specific configurations are common, consider moving this logic into an artisan command or a dedicated CLI tool. For example, you could define a custom command in your project that executes the setup only when invoked explicitly:

php composer.phar run-script setup-behat

Then, configure composer.json to map this script to a specific action that only runs when dev dependencies are being processed. However, Composer's standard lifecycle events do not support conditional execution based on the --dev flag directly within the array syntax.

The most practical workaround without custom commands is to manually run the setup script after updating dev dependencies. Create a file named install-behat.sh in your repository root:

#!/bin/bash
set -e

echo "Updating development dependencies..."
php composer.phar update --dev

if [ ! -d "vendor/Behat" ]; then
    echo "Running Behat installation hooks manually..."
    # Recreate the logic from post-update-cmd but only if needed
    rm -rf vendor/Behat
    git clone git://github.com/Behat/Behat.git
    cp composer.phar Behat/composer.phar
    cd Behat && git submodule update --init
    cd Behat && php composer.phar install
    cd Behat && php composer.phar require guzzle/guzzle:3.0.*
    mv Behat vendor/Behat
    ln -sf ../vendor/Behat/bin/behat vendor/bin/
fi

Make this script executable (chmod +x install-behat.sh) and run it after your update --dev command. This ensures the heavy lifting happens only when you intentionally target development tools, keeping your production installs clean and predictable.

Our network