Composer commands cheat sheet
Installing and updating Composer
-
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"Downloads the official Composer installer to the composer-setup.php file in the current directory. Before you run it, verify its signature, as in the next command.
-
php -r "echo hash_file('sha384', 'composer-setup.php');"Calculates the SHA-384 hash of the installer. Compare it with the value on the getcomposer.org download page or in the https://composer.github.io/installer.sig file. The value changes with every version of the installer, so copy it from the official source each time you install. Caution: when the values differ, delete the file and don't run it.
-
php composer-setup.phpRuns the installer, which creates the composer.phar file in the current directory. The --install-dir option sets a different directory, and the --filename=composer option sets the file name.
-
php -r "unlink('composer-setup.php');"Deletes the installer after the installation is complete.
-
sudo mv composer.phar /usr/local/bin/composerMoves Composer to a directory in the search path (PATH), so you can run it with the composer command from any directory.
-
composer --versionShows the Composer version. The short form is composer -V.
-
composer self-updateUpdates Composer to the latest stable version. When Composer is installed in a system directory, such as /usr/local/bin, you need the right permissions, for example sudo -H composer self-update.
-
composer self-update --2Updates to the latest version of Composer 2 and stays on the 2.x branch. You can also request a specific version, for example composer self-update 2.8.0.
-
composer self-update --rollbackRolls Composer back to the version that was installed before the last update, for example when the new version causes a problem.
-
composer diagnoseRuns automatic checks for common problems in the Composer environment and settings. It's a good first step when something doesn't work.
New projects and adding packages
-
composer initCreates a new composer.json file for the project, by asking about the project name, description, license, and required packages.
-
composer create-project laravel/laravel my-appCreates a new project from an existing package in the my-app directory and installs all its dependencies. You can add a specific version at the end. Without a version, Composer picks the latest one.
-
composer require monolog/monologAdds a package to the composer.json file, installs it, and updates composer.lock. Without a version constraint, Composer picks a constraint that matches the latest version it can install.
-
composer require "monolog/monolog:^3.0"Adds a package with a specific version constraint. The quotes stop the shell from interpreting special characters such as ^ or *.
-
composer require --dev phpunit/phpunitAdds a package to the require-dev list, which is meant for packages needed only for development and testing. When you install for production with --no-dev, these packages aren't installed.
-
composer require vendor/package -WAdds a package and also allows its dependencies to be updated, including packages listed directly in composer.json. Useful when the new package requires a newer version of a package that's already installed. -W is short for --update-with-all-dependencies.
-
composer remove monolog/monologRemoves a package from composer.json and from the vendor directory, and updates composer.lock. Packages that were installed only because of it are removed too.
-
composer remove --dev phpunit/phpunitRemoves a package from the require-dev list.
-
composer remove --unusedRemoves packages that are still in the lock file but are no longer required by any package in the project.
install and update
-
composer installInstalls the dependencies in exactly the versions recorded in the composer.lock file, so every installation, on the development machine and on the server, gets the same versions. When there's no lock file, the command resolves the versions like update and creates one. When composer.json changed and the lock file wasn't updated, a warning is shown.
-
composer install --no-dev --optimize-autoloaderThe recommended installation for a production server: without the development packages from the require-dev list, and with an optimized autoloader that finds classes faster. -o is short for --optimize-autoloader.
-
composer updateCaution: updates all the packages to the latest versions that match the constraints in composer.json and rewrites composer.lock. Run it in the development environment, check that everything works, and commit the lock file to the code repository. On a production server, run install.
-
composer update monolog/monologUpdates only the package you name and keeps the other packages at the versions in the lock file. You can name several packages, and also a pattern with an asterisk, for example "symfony/*".
-
composer update monolog/monolog --with-dependenciesUpdates the package and its dependencies, except packages listed directly in composer.json. The short form is -w.
-
composer update monolog/monolog --with-all-dependenciesUpdates the package and all its dependencies, including packages listed directly in composer.json. The short form is -W.
-
composer update --dry-runShows which packages would be installed, updated, or removed, without actually changing anything.
-
composer update --lockUpdates the content hash in the lock file without changing any package versions. Useful when a warning says the lock file is out of date after a small change to composer.json.
-
composer bumpRaises the lower bound of the constraints in composer.json to the versions currently installed. Suitable for an application. For a package you publish for others, it's better not to run it, because it narrows the versions the package accepts.
Checking packages and security
-
composer outdatedShows packages that have a newer version. Green: the package is up to date. Red: a compatible newer version exists, and it's recommended to update. Yellow: the newer version may break compatibility, so the update needs testing.
-
composer outdated --directShows only the packages listed directly in composer.json, without their dependencies. The short form is -D.
-
composer showLists all the installed packages and their versions. The --installed (-i) option is already the default, so there's no need to add it.
-
composer show -tShows the dependencies as a tree: each package with the packages it requires below it. You can add a package name to see only its tree.
-
composer show monolog/monologShows details about a specific package: description, version, license, download source, and the packages it requires.
-
composer show --platformShows the platform packages available in the environment: the PHP version and PHP extensions, as Composer detects them.
-
composer why psr/logExplains why a package is installed: which packages require it and with which constraint. why is an alias of depends, and the -t option shows the answer as a tree.
-
composer why-not monolog/monolog 4.0Explains what prevents a specific version from being installed: which packages require a different version. why-not is an alias of prohibits. It's also useful for checking before you move to a new PHP version, for example composer why-not php 8.5.
-
composer validateChecks that the composer.json file is valid and has no structural errors. The --strict option also returns an error code on warnings, which suits automated checks.
-
composer auditChecks the installed packages against the database of security advisories and shows vulnerable and abandoned packages. The --locked option checks against the lock file, without needing the vendor directory. The update and require commands run this check automatically.
-
composer licensesShows the name, version, and license of every installed package. The --no-dev option leaves out the development packages, and the --format=json option returns output for processing in a script.
Autoload
-
composer dump-autoloadRegenerates the autoload files in the vendor directory. Run it after you change the autoload settings in composer.json, for example when you add a new namespace.
-
composer dump-autoload -oGenerates an optimized autoloader, which converts the PSR-4 and PSR-0 rules into a class map (classmap) and finds classes faster. Suitable for a production server, not for a development environment where you add and delete classes.
-
composer dump-autoload --classmap-authoritativeLoads classes only from the class map, without searching the file system, and also includes the -o optimization. Caution: classes generated at runtime won't be found, and the application may show a class not found error. The short form is -a.
Settings, repositories, and scripts
-
composer config --listShows all the settings in effect for the current project.
-
composer config --global --listShows the user's global settings, which are saved in the config.json file in the COMPOSER_HOME directory. The short form is -g.
-
composer config process-timeout 900Sets a value in the project's composer.json file, in this example a maximum run time of 900 seconds for processes that Composer starts. The --unset option deletes a setting.
-
composer config repositories.acme vcs https://github.com/acme/fooAdds a package repository named acme, of type vcs, such as a private Git repository, to composer.json. You can then install packages from it with composer require.
-
composer repo add acme vcs https://github.com/acme/fooAdds a package repository with the repo command, which is available in recent versions and recommended over config repositories. composer repo list shows the repositories, and composer repo remove acme removes a repository.
-
"scripts": { "test": "phpunit" }Defines a script named test in composer.json. Scripts can also run commands on fixed events, such as post-install-cmd after install and post-update-cmd after update. @php runs the current PHP, and @composer runs Composer.
-
composer run-script --listLists the scripts defined in composer.json.
-
composer run-script testRuns a script from composer.json. For a script whose name isn't a fixed event name, you can shorten it to composer test and pass options after --, for example composer test -- --filter UserTest.
-
composer run-script --timeout=0 testRuns a script with no time limit. The default is 300 seconds, and a longer script stops with an error.
PHP version, memory, and troubleshooting
-
composer check-platform-reqsChecks that the PHP version and extensions on the server meet the requirements of the installed packages. The --no-dev option skips the requirements of the development packages. It's a good idea to run it on the server before you deploy a new version of the site.
-
composer install --ignore-platform-req=phpCaution: ignores the PHP version requirement of the packages. The packages are installed, but they may not work with the PHP version on the server. It's better to match the PHP version or the package versions.
-
composer update --ignore-platform-req=php+Ignores only the upper bound of the PHP version requirement. Useful when you move to a new PHP version that a package hasn't declared support for yet, and it still checks the lower bound.
-
COMPOSER_MEMORY_LIMIT=-1 composer updateRuns Composer with no memory limit, when the command fails with the Allowed memory size exhausted error. By default, Composer raises the limit to 1.5G. On a server with little memory, it's better to run update on the development machine and only install on the server.
-
COMPOSER_PROCESS_TIMEOUT=900 composer installExtends the maximum time for processes that Composer starts, such as scripts and repository clones, to 900 seconds. The default is 300 seconds, and the value 0 removes the limit.
-
composer clear-cacheDeletes the Composer cache, where downloaded packages are stored. Useful when an installation fails because of a corrupted file in the cache. The short form is composer cc.
-
composer install --no-plugins --no-scriptsInstalls without running plugins and scripts from composer.json. Suitable for a project from an unknown source, because plugins and scripts get full access to the account of the user who runs Composer.
-
sudo -u www-data composer installRuns Composer as the site's user and not as root, in this example the www-data user. When you run Composer as root, it shows the Do not run Composer as root/super user warning and, in a non-interactive run, disables plugins. The COMPOSER_ALLOW_SUPERUSER=1 environment variable turns off the warning and is meant only for environments where you always work as root, such as Docker.
Version constraints
-
1.0.2An exact version: Composer installs only this version. When another package requires a different version, the installation fails.
-
^1.2.3Any version from 1.2.3 up to, but not including, 2.0.0 (>=1.2.3 <2.0.0), because updates within the same major version should stay compatible. This is the recommended constraint for most packages.
-
^0.3For versions before 1.0, the ^ constraint is more cautious: ^0.3 allows 0.3.0 up to, but not including, 0.4.0 (>=0.3.0 <0.4.0).
-
~1.2From 1.2 up to, but not including, 2.0.0 (>=1.2 <2.0.0). With the tilde, only the last digit you specify can go up.
-
~1.2.3From 1.2.3 up to, but not including, 1.3.0 (>=1.2.3 <1.3.0), which means patch updates only.
-
1.0.*Any version that starts with 1.0, the same as >=1.0 <1.1.
-
>=1.0 <2.0A version range. A space or a comma between conditions means "and", and || means "or", for example >=1.0 <1.1 || >=1.2. Caution: an open range such as >=1.0 on its own may later install an incompatible version.
-
1.0 - 2.0A range that includes both ends. When the version on the right is partial, it's completed with a wildcard, so 1.0 - 2.0 is the same as >=1.0.0 <2.1.
-
*Any version. Common in requirements for PHP extensions, such as "ext-gd": "*", where version numbers aren't consistent. Not recommended for regular packages, because an update may install a new, incompatible major version.
No matching commands.
Composer works with two files. In the composer.json file, you define which packages are required and in which version ranges, and the composer.lock file records the exact versions that were actually installed. The composer update command resolves new versions and writes them to the lock file, and the composer install command installs exactly what the lock file lists. That's why you commit composer.lock to the code repository, run update in the development environment, and run composer install --no-dev --optimize-autoloader on the server.
Run Composer as the user the site runs as, not as root, so the files in the vendor directory get the right ownership. Before you update packages on a live site, back up the files and the database, and run composer audit to make sure no package has a known security vulnerability. Support dates for each PHP version are on the PHP lifecycle page.
On a Linux VPS, you connect over SSH with full root access, install Composer, and run every command on this page. On a Laravel VPS, PHP and Composer come installed and configured, so you can start deploying your application right away.
Composer commands FAQ
What's the difference between composer install and composer update?
The composer install command installs the packages in exactly the versions recorded in the composer.lock file, so every installation gets the same versions. The composer update command looks for newer versions that match the constraints in composer.json, installs them, and rewrites composer.lock. Run update in the development environment when you want to update, and run install on the server.
Should I commit composer.lock to the code repository?
Yes. When the composer.lock file is in the code repository, everyone who works on the project, and the server too, installs exactly the same package versions. This way, what you tested in the development environment is what runs on the site. The vendor directory, on the other hand, stays out of the repository, because composer install recreates it.
How do I install Composer packages on a live site's server?
Upload the code to the server together with composer.json and composer.lock, and run composer install --no-dev --optimize-autoloader. This installs only the packages the site needs, in the versions you tested, with an optimized autoloader that speeds up class loading. On a Linux VPS, you can run the command over SSH, and on a Laravel VPS, PHP and Composer are already installed and ready to use.
What do ^ and ~ mean in version constraints?
The ^1.2.3 constraint allows any version from 1.2.3 up to, but not including, 2.0.0, which means updates that should stay compatible. The ~1.2.3 constraint is more cautious and allows only versions up to, but not including, 1.3.0, which means patch updates. In most cases, ^ is the recommended choice, and Composer picks a suitable constraint by itself when you run composer require without a version.
What can I do when Composer fails with a memory error?
The Allowed memory size exhausted error usually appears with the composer update command, which recalculates the versions of all the packages. You can run it with no memory limit: COMPOSER_MEMORY_LIMIT=-1 composer update. An even better way is to run update on the development machine, upload the updated composer.lock to the server, and run only composer install there, which uses much less memory.