Global Packages
Every composer require so far has installed a package into one specific project's
vendor/ folder. Sometimes, though, you want a tool available everywhere on your machine
— not tied to any single project at all. That's what composer global require is for.
Per-Project vs Global Installs
A normal composer require only affects the project you run it in, adding the package to
that project's composer.json and vendor/ folder.
composer require vendor/package
composer global require installs into a separate, machine-wide Composer directory instead
— completely independent of whatever project you happen to be sitting in when you run it.
composer global require vendor/package
composer require | composer global require | |
|---|---|---|
| Scope | One project only | Your whole machine |
| Recorded in | That project's composer.json | A separate global composer.json |
| Typical use | Libraries your app's code actually uses | Standalone command-line tools |
Common Tools Installed Globally
Global installs make the most sense for command-line tools you want to reach for from any directory, regardless of which project you're currently working in.
composer global require laravel/installer
laravel/installer gives you a laravel new command for scaffolding brand new
Laravel projects — useful before you've even created the project it would otherwise belong to.
composer global require friendsofphp/php-cs-fixer
PHP CS Fixer is a code style tool many developers like to run against any project, so installing it once globally saves reinstalling it into every single one.
Make Sure the Global bin Directory Is on Your PATH
Globally installed tools are placed in a special Composer "global bin" directory. For the commands
they provide (like laravel or php-cs-fixer) to actually run from any
terminal, that directory needs to be on your system's PATH environment variable.
composer global config bin-dir --absolute
That command prints the path Composer uses for global binaries. Add it to your PATH
(the exact steps differ by operating system — environment variable settings on Windows, or your
shell's profile file such as .bashrc or .zshrc on macOS/Linux) so a command
like laravel new my-app works no matter which folder your terminal is currently in.
uses or calls at runtime — a logging library, an HTTP client, a framework —
should almost always be a per-project dependency via a normal composer require, recorded
in that project's own composer.json and composer.lock. Global installs are
for standalone command-line tools only; relying on a global package inside application code breaks
reproducibility, since a teammate's machine (or your CI server) may not have that global package
installed at all.