Composer Scripts

Beyond managing dependencies and autoloading, Composer can also act as a lightweight task runner. The scripts section of composer.json lets you define short, memorable names for commands you run often, so the whole team runs them the same way.

The scripts Section

Each entry in scripts maps a name to the actual command it should run. Here's a common example: a test shortcut for running PHPUnit (the same tool added under require-dev on the Dev Dependencies page).

{
    "require-dev": {
        "phpunit/phpunit": "^10.0"
    },
    "scripts": {
        "test": "phpunit"
    }
}

With that in place, anyone on the project can run the test suite with a short, consistent command instead of remembering the full underlying tool name:

composer test

run-script vs a Bare Command Name

Composer has a small set of reserved command names (things like install, update, require). If your custom script name doesn't collide with one of those, Composer lets you run it directly as composer <name>, exactly like composer test above. The fully explicit form, which always works no matter what the script is named, is composer run-script <name>.

composer run-script test

In everyday use, the short form (composer test) is what most developers reach for, since test isn't a reserved Composer command name.

Automatic Lifecycle Hooks

Composer also fires certain script names automatically at specific points in its own lifecycle, without you ever typing them. Two of the most useful are post-install-cmd and post-update-cmd, which run right after composer install or composer update finishes.

{
    "scripts": {
        "post-install-cmd": "php artisan cache:clear",
        "post-update-cmd": "php artisan cache:clear"
    }
}

With that configured, every time a teammate (or your deploy pipeline) runs composer install, the application's cache is cleared automatically right afterward — no separate manual step to forget.

A Practical scripts Block

A small project might combine a couple of shortcuts like this:

{
    "scripts": {
        "test": "phpunit",
        "post-install-cmd": "echo Dependencies installed."
    }
}

Now composer test runs the test suite on demand, while echo Dependencies installed. prints automatically every time composer install finishes — two different ways of hooking into the same scripts section.

Next up: so far every command has installed something into the current project. Head to Global Packages to see how to install command-line tools that aren't tied to any one project.