Command-line interface / Commands - Composer (original) (raw)

You've already learned how to use the command-line interface to do some things. This chapter documents all the available commands.

To get help from the command-line, call composer or composer listto see the complete list of commands, then --help combined with any of those can give you more information.

As Composer uses symfony/console you can call commands by short name if it's not ambiguous.

php composer.phar dump

calls composer dump-autoload.

Bash Completions#

To install bash completions you can run composer completion bash > completion.bash. This will create a completion.bash file in the current directory.

Then execute source completion.bash to enable it in the current terminal session.

Move and rename the completion.bash file to /etc/bash_completion.d/composer to make it load automatically in new terminals.

Global Options#

The following options are available with every command:

Process Exit Codes#

init#

In the Libraries chapter we looked at how to create acomposer.json by hand. There is also an init command available to do this.

When you run the command it will interactively ask you to fill in the fields, while using some smart defaults.

php composer.phar init

Options

install / i#

The install command reads the composer.json file from the current directory, resolves the dependencies, and installs them into vendor.

php composer.phar install

If there is a composer.lock file in the current directory, it will use the exact versions from there instead of resolving them. This ensures that everyone using the library will get the same versions of the dependencies.

If there is no composer.lock file, Composer will create one after dependency resolution.

Options

update / u / upgrade#

In order to get the latest versions of the dependencies and to update thecomposer.lock file, you should use the update command. This command is also aliased as upgrade as it does the same as upgrade does if you are thinking of apt-get or similar package managers.

php composer.phar update

This will resolve all dependencies of the project and write the exact versions into composer.lock.

If you only want to update a few packages and not all, you can list them as such:

php composer.phar update vendor/package vendor/package2

You can also use wildcards to update a bunch of packages at once:

php composer.phar update "vendor/*"

If you want to downgrade a package to a specific version without changing your composer.json you can use --with and provide a custom version constraint:

php composer.phar update --with vendor/package:2.0.1

Note that with the above all packages will be updated. If you only want to update the package(s) for which you provide custom constraints using --with, you can skip --with and instead use constraints with the partial update syntax:

php composer.phar update vendor/package:2.0.1 vendor/package2:3.0.*

Note: For packages also required in your composer.json the custom constraint must be a subset of the existing constraint. The composer.json constraints still apply and the composer.json is not modified by these temporary update constraints.

Options

Specifying one of the words mirrors, lock, or nothing as an argument has the same effect as specifying the option --lock, for example composer update mirrors is exactly the same as composer update --lock.

require / r#

The require command adds new packages to the composer.json file from the current directory. If no file exists one will be created on the fly.

If you do not specify a package, Composer will prompt you to search for a package, and given results, provide a list of matches to require.

php composer.phar require

After adding/changing the requirements, the modified requirements will be installed or updated.

If you do not want to choose requirements interactively, you can pass them to the command.

php composer.phar require "vendor/package:2.*" vendor/package2:dev-master

If you do not specify a version constraint, composer will choose a suitable one based on the available package versions.

php composer.phar require vendor/package vendor/package2

If you do not want to install the new dependencies immediately you can call it with --no-update

Options

remove / rm / uninstall#

The remove command removes packages from the composer.json file from the current directory.

php composer.phar remove vendor/package vendor/package2

After removing the requirements, the modified requirements will be uninstalled.

Options

bump#

The bump command increases the lower limit of your composer.json requirements to the currently installed versions. This helps to ensure your dependencies do not accidentally get downgraded due to some other conflict, and can slightly improve dependency resolution performance as it limits the amount of package versions Composer has to look at.

Running this blindly on libraries is NOT recommended as it will narrow down your allowed dependencies, which may cause dependency hell for your users. Running it with --dev-only on libraries may be fine however as dev requirements are local to the library and do not affect consumers of the package.

Options

reinstall#

The reinstall command looks up installed packages by name, uninstalls them and reinstalls them. This lets you do a clean install of a package if you messed with its files, or if you wish to change the installation type using --prefer-install.

php composer.phar reinstall acme/foo acme/bar

You can specify more than one package name to reinstall, or use a wildcard to select several packages at once:

php composer.phar reinstall "acme/*"

Options

check-platform-reqs#

The check-platform-reqs command checks that your PHP and extensions versions match the platform requirements of the installed packages. This can be used to verify that a production server has all the extensions needed to run a project after installing it for example.

Unlike update/install, this command will ignore config.platform settings and check the real platform packages so you can be certain you have the required platform dependencies.

Options

global#

The global command allows you to run other commands like install, remove, requireor update as if you were running them from the COMPOSER_HOMEdirectory.

This is merely a helper to manage a project stored in a central location that can hold CLI tools or Composer plugins that you want to have available everywhere.

This can be used to install CLI utilities globally. Here is an example:

php composer.phar global require friendsofphp/php-cs-fixer

Now the php-cs-fixer binary is available globally. Make sure your globalvendor binaries directory is in your $PATHenvironment variable, you can get its location with the following command :

php composer.phar global config bin-dir --absolute

If you wish to update the binary later on you can run a global update:

php composer.phar global update

The search command allows you to search through the current project's package repositories. Usually this will be packagist. You pass it the terms you want to search for.

php composer.phar search monolog

You can also search for more than one term by passing multiple arguments.

Options

show / info#

To list all of the available packages, you can use the show command.

php composer.phar show

To filter the list you can pass a package mask using wildcards.

php composer.phar show "monolog/*"
monolog/monolog 2.4.0 Sends your logs to files, sockets, inboxes, databases and various web services

If you want to see the details of a certain package, you can pass the package name.

php composer.phar show monolog/monolog
name     : monolog/monolog
descrip. : Sends your logs to files, sockets, inboxes, databases and various web services
keywords : log, logging, psr-3
versions : * 1.27.1
type     : library
license  : MIT License (MIT) (OSI approved) https://spdx.org/licenses/MIT.html#licenseText
homepage : http://github.com/Seldaek/monolog
source   : [git] https://github.com/Seldaek/monolog.git 904713c5929655dc9b97288b69cfeedad610c9a1
dist     : [zip] https://api.github.com/repos/Seldaek/monolog/zipball/904713c5929655dc9b97288b69cfeedad610c9a1 904713c5929655dc9b97288b69cfeedad610c9a1
names    : monolog/monolog, psr/log-implementation

support
issues : https://github.com/Seldaek/monolog/issues
source : https://github.com/Seldaek/monolog/tree/1.27.1

autoload
psr-4
Monolog\ => src/Monolog

requires
php >=5.3.0
psr/log ~1.0

You can even pass the package version, which will tell you the details of that specific version.

php composer.phar show monolog/monolog 1.0.2

Options

outdated#

The outdated command shows a list of installed packages that have updates available, including their current and latest versions. This is basically an alias forcomposer show -lo.

The color coding is as such:

Options

browse / home#

The browse (aliased to home) opens a package's repository URL or homepage in your browser.

Options

suggests#

Lists all packages suggested by the currently installed set of packages. You can optionally pass one or multiple package names in the format of vendor/packageto limit output to suggestions made by those packages only.

Use the --by-package (default) or --by-suggestion flags to group the output by the package offering the suggestions or the suggested packages respectively.

If you only want a list of suggested package names, use --list.

Options

fund#

Discover how to help fund the maintenance of your dependencies. This lists all funding links from the installed dependencies. Use --format=json to get machine-readable output.

Options

depends / why#

The depends command tells you which other packages depend on a certain package. As with installation require-dev relationships are only considered for the root package.

php composer.phar depends doctrine/lexer
doctrine/annotations  1.13.3 requires doctrine/lexer (1.*)
doctrine/common       2.13.3 requires doctrine/lexer (^1.0)

You can optionally specify a version constraint after the package to limit the search.

Add the --tree or -t flag to show a recursive tree of why the package is depended upon, for example:

php composer.phar depends psr/log -t
psr/log 1.1.4 Common interface for logging libraries
├──composer/composer 2.4.x-dev (requires psr/log ^1.0 || ^2.0 || ^3.0)
├──composer/composer dev-main (requires psr/log ^1.0 || ^2.0 || ^3.0)
├──composer/xdebug-handler 3.0.3 (requires psr/log ^1 || ^2 || ^3)
│  ├──composer/composer 2.4.x-dev (requires composer/xdebug-handler ^2.0.2 || ^3.0.3)
│  └──composer/composer dev-main (requires composer/xdebug-handler ^2.0.2 || ^3.0.3)
└──symfony/console v5.4.11 (conflicts psr/log >=3) (circular dependency aborted here)

Options

prohibits / why-not#

The prohibits command tells you which packages are blocking a given package from being installed. Specify a version constraint to verify whether upgrades can be performed in your project, and if not why not. See the following example:

php composer.phar prohibits symfony/symfony 3.1
laravel/framework v5.2.16 requires symfony/var-dumper (2.8.*|3.0.*)

Note that you can also specify platform requirements, for example to check whether you can upgrade your server to PHP 8.0:

php composer.phar prohibits php 8
doctrine/cache        v1.6.0 requires php (~5.5|~7.0)
doctrine/common       v2.6.1 requires php (~5.5|~7.0)
doctrine/instantiator 1.0.5  requires php (>=5.3,<8.0-DEV)

As with depends you can request a recursive lookup, which will list all packages depending on the packages that cause the conflict.

Options

validate#

You should always run the validate command before you commit yourcomposer.json file (and composer.lock if applicable), and before you tag a release.

It will check if yourcomposer.json is valid. If a composer.lock exists, it will also check if it is up to date with the composer.json.

php composer.phar validate

Options

status#

If you often need to modify the code of your dependencies and they are installed from source, the status command allows you to check if you have local changes in any of them.

php composer.phar status

With the --verbose option you get some more information about what was changed:

php composer.phar status -v
You have changes in the following dependencies:
vendor/seld/jsonlint:
    M README.mdown

self-update / selfupdate#

To update Composer itself to the latest version, run the self-updatecommand. It will replace your composer.phar with the latest version.

php composer.phar self-update

If you would like to instead update to a specific release specify it:

php composer.phar self-update 2.4.0-RC1

If you have installed Composer for your entire system (see global installation), you may have to run the command with root privileges

sudo -H composer self-update

If Composer was not installed as a PHAR, this command is not available. (This is sometimes the case when Composer was installed by an operating system package manager.)

Options

config#

The config command allows you to edit Composer config settings and repositories in either the local composer.json file or the global config.json file.

Additionally it lets you edit most properties in the local composer.json.

php composer.phar config --list

Usage#

config [options] [setting-key] [setting-value1] ... [setting-valueN]

setting-key is a configuration option name and setting-value1 is a configuration value. For settings that can take an array of values (likegithub-protocols), multiple setting-value arguments are allowed.

You can also edit the values of the following properties:

description, homepage, keywords, license, minimum-stability,name, prefer-stable, type and version.

See the Config chapter for valid configuration options.

Options

Modifying Repositories#

In addition to modifying the config section, the config command also supports making changes to the repositories section by using it the following way:

php composer.phar config repositories.foo vcs https://github.com/foo/bar

If your repository requires more configuration options, you can instead pass its JSON representation :

php composer.phar config repositories.foo '{"type": "vcs", "url": "http://svn.example.org/my-project/", "trunk-path": "master"}'

In addition to modifying the config section, the config command also supports making changes to the extra section by using it the following way:

php composer.phar config extra.foo.bar value

The dots indicate array nesting, a max depth of 3 levels is allowed though. The above would set "extra": { "foo": { "bar": "value" } }.

If you have a complex value to add/modify, you can use the --json and --merge flags to edit extra fields as json:

php composer.phar config --json extra.foo.bar '{"baz": true, "qux": []}'

create-project#

You can use Composer to create new projects from an existing package. This is the equivalent of doing a git clone/svn checkout followed by a composer installof the vendors.

There are several applications for this:

  1. You can deploy application packages.
  2. You can check out any package and start developing on patches for example.
  3. Projects with multiple developers can use this feature to bootstrap the initial application for development.

To create a new project using Composer you can use the create-project command. Pass it a package name, and the directory to create the project in. You can also provide a version as a third argument, otherwise the latest version is used.

If the directory does not currently exist, it will be created during installation.

php composer.phar create-project doctrine/orm path "2.2.*"

It is also possible to run the command without params in a directory with an existing composer.json file to bootstrap a project.

By default the command checks for the packages on packagist.org.

Options

dump-autoload / dumpautoload#

If you need to update the autoloader because of new classes in a classmap package for example, you can use dump-autoload to do that without having to go through an install or update.

Additionally, it can dump an optimized autoloader that converts PSR-0/4 packages into classmap ones for performance reasons. In large applications with many classes, the autoloader can take up a substantial portion of every request's time. Using classmaps for everything is less convenient in development, but using this option you can still use PSR-0/4 for convenience and classmaps for performance.

Options

clear-cache / clearcache / cc#

Deletes all content from Composer's cache directories.

Options

licenses#

Lists the name, version and license of every package installed. Use--format=json to get machine-readable output.

Options

run-script / run#

Options

To run scripts manually you can use this command, give it the script name and optionally any required arguments.

exec#

Executes a vendored binary/script. You can execute any command and this will ensure that the Composer bin-dir is pushed on your PATH before the command runs.

Options

diagnose#

If you think you found a bug, or something is behaving strangely, you might want to run the diagnose command to perform automated checks for many common problems.

php composer.phar diagnose

archive#

This command is used to generate a zip/tar archive for a given package in a given version. It can also be used to archive your entire project without excluded/ignored files.

php composer.phar archive vendor/package 2.0.21 --format=zip

Options

audit#

This command is used to audit the packages you have installed for potential security issues. It checks for and lists security vulnerability advisories using the Packagist.org api by default or other repositories if specified in the repositories section of composer.json. The command also detects abandoned packages.

The audit command determines if there are vulnerable or abandoned packages and returns the following exit codes based on the findings:

php composer.phar audit

Options

help#

To get more information about a certain command, you can use help.

php composer.phar help install

Command-line completion#

Command-line completion can be enabled by running the composer completion --help command and following the instructions.

Environment variables#

You can set a number of environment variables that override certain settings. Whenever possible it is recommended to specify these settings in the configsection of composer.json instead. It is worth noting that the env vars will always take precedence over the values specified in composer.json.

COMPOSER#

By setting the COMPOSER env variable it is possible to set the filename ofcomposer.json to something else.

For example:

COMPOSER=composer-other.json php composer.phar install

The generated lock file will use the same name: composer-other.lock in this example.

COMPOSER_ALLOW_SUPERUSER#

If set to 1, this env disables the warning about running commands as root/super user. It also disables automatic clearing of sudo sessions, so you should really only set this if you use Composer as a super user at all times like in docker containers.

COMPOSER_ALLOW_XDEBUG#

If set to 1, this env allows running Composer when the Xdebug extension is enabled, without restarting PHP without it.

COMPOSER_AUTH#

The COMPOSER_AUTH var allows you to set up authentication as an environment variable. The contents of the variable should be a JSON formatted object containing http-basic, github-oauth, bitbucket-oauth, ... objects as needed, and following thespec from the config.

COMPOSER_BIN_DIR#

By setting this option you can change the bin (Vendor Binaries) directory to something other than vendor/bin.

COMPOSER_CACHE_DIR#

The COMPOSER_CACHE_DIR var allows you to change the Composer cache directory, which is also configurable via the cache-dir option.

By default, it points to C:\Users\<user>\AppData\Local\Composer (or %LOCALAPPDATA%/Composer) on Windows. On *nix systems that follow the XDG Base Directory Specifications, it points to $XDG_CACHE_HOME/composer. On other *nix systems and on macOS, it points to$COMPOSER_HOME/cache.

COMPOSER_CAFILE#

By setting this environmental value, you can set a path to a certificate bundle file to be used during SSL/TLS peer verification.

COMPOSER_DISABLE_XDEBUG_WARN#

If set to 1, this env suppresses a warning when Composer is running with the Xdebug extension enabled.

COMPOSER_DISCARD_CHANGES#

This env var controls the discard-changes config option.

COMPOSER_FUND#

If set to 0, this env suppresses funding notices when installing.

COMPOSER_HOME#

The COMPOSER_HOME var allows you to change the Composer home directory. This is a hidden, global (per-user on the machine) directory that is shared between all projects.

Use composer config --global home to see the location of the home directory.

By default, it points to C:\Users\<user>\AppData\Roaming\Composer on Windows and /Users/<user>/.composer on macOS. On *nix systems that follow the XDG Base Directory Specifications, it points to $XDG_CONFIG_HOME/composer. On other *nix systems, it points to/home/<user>/.composer.

COMPOSER_HOME/config.json#

You may put a config.json file into the location which COMPOSER_HOME points to. Composer will partially (only config and repositories keys) merge this configuration with your project's composer.json when you run the install andupdate commands.

This file allows you to set repositories andconfiguration for the user's projects.

In case global configuration matches local configuration, the _local_configuration in the project's composer.json always wins.

COMPOSER_HTACCESS_PROTECT#

Defaults to 1. If set to 0, Composer will not create .htaccess files in the Composer home, cache, and data directories.

COMPOSER_MEMORY_LIMIT#

If set, the value is used as php's memory_limit.

COMPOSER_MIRROR_PATH_REPOS#

If set to 1, this env changes the default path repository strategy to mirror instead of symlink. As it is the default strategy being set it can still be overwritten by repository options.

COMPOSER_NO_INTERACTION#

If set to 1, this env var will make Composer behave as if you passed the--no-interaction flag to every command. This can be set on build boxes/CI.

COMPOSER_PROCESS_TIMEOUT#

This env var controls the time Composer waits for commands (such as git commands) to finish executing. The default value is 300 seconds (5 minutes).

COMPOSER_ROOT_VERSION#

By setting this var you can specify the version of the root package, if it cannot be guessed from VCS info and is not present in composer.json.

COMPOSER_VENDOR_DIR#

By setting this var you can make Composer install the dependencies into a directory other than vendor.

COMPOSER_RUNTIME_ENV#

This lets you hint under which environment Composer is running, which can help Composer work around some environment specific issues. The only value currently supported isvirtualbox, which then enables some short sleep() calls to wait for the filesystem to have written files properly before we attempt reading them. You can set the environment variable if you use Vagrant or VirtualBox and experience issues with files not being found during installation even though they should be present.

http_proxy or HTTP_PROXY#

HTTP_PROXY_REQUEST_FULLURI#

HTTPS_PROXY_REQUEST_FULLURI#

no_proxy or NO_PROXY#

See the proxy documentation for more details on how to use proxy env vars.

COMPOSER_AUDIT_ABANDONED#

Set to ignore, report or fail to override the audit.abandonedconfig option.

COMPOSER_MAX_PARALLEL_HTTP#

Set to an integer to configure how many files can be downloaded in parallel. This defaults to 12 and must be between 1 and 50. If your proxy has issues with concurrency maybe you want to lower this. Increasing it should generally not result in performance gains.

COMPOSER_MAX_PARALLEL_PROCESSES#

Set to an an integer to configure how many processes can be executed in parallel. This defaults to 10 and must be between 1 and 50.

COMPOSER_IPRESOLVE#

Set to 4 or 6 to force IPv4 or IPv6 DNS resolution. This only works when the curl extension is used for downloads.

COMPOSER_SELF_UPDATE_TARGET#

If set, makes the self-update command write the new Composer phar file into that path instead of overwriting itself. Useful for updating Composer on a read-only filesystem.

COMPOSER_DISABLE_NETWORK#

If set to 1, disables network access (best effort). This can be used for debugging or to run Composer on a plane or a starship with poor connectivity.

If set to prime, GitHub VCS repositories will prime the cache, so it can then be used fully offline with 1.

COMPOSER_DEBUG_EVENTS#

If set to 1, outputs information about events being dispatched, which can be useful for plugin authors to identify what is firing when exactly.

COMPOSER_SKIP_SCRIPTS#

Accepts a comma-seperated list of event names, e.g. post-install-cmd for which scripts execution should be skipped.

COMPOSER_NO_AUDIT#

If set to 1, it is the equivalent of passing the --no-audit option to require, update, remove or create-project command.

COMPOSER_NO_DEV#

If set to 1, it is the equivalent of passing the --update-no-dev option to requireor the --no-dev option to install or update. You can override this for a single command by setting COMPOSER_NO_DEV=0.

COMPOSER_PREFER_STABLE#

If set to 1, it is the equivalent of passing the --prefer-stable option toupdate or require.

COMPOSER_PREFER_LOWEST#

If set to 1, it is the equivalent of passing the --prefer-lowest option toupdate or require.

COMPOSER_MINIMAL_CHANGES#

If set to 1, it is the equivalent of passing the --minimal-changes option toupdate, require or remove.

COMPOSER_IGNORE_PLATFORM_REQ or COMPOSER_IGNORE_PLATFORM_REQS#

If COMPOSER_IGNORE_PLATFORM_REQS set to 1, it is the equivalent of passing the --ignore-platform-reqs argument. Otherwise, specifying a comma separated list in COMPOSER_IGNORE_PLATFORM_REQ will ignore those specific requirements.

For example, if a development workstation will never run database queries, this can be used to ignore the requirement for the database extensions to be available. If you set COMPOSER_IGNORE_PLATFORM_REQ=ext-oci8, then composer will allow packages to be installed even if the oci8 PHP extension is not enabled.

COMPOSER_WITH_DEPENDENCIES#

If set to 1, it is the equivalent of passing the --with-dependencies option toupdate, require or remove.

COMPOSER_WITH_ALL_DEPENDENCIES#

If set to 1, it is the equivalent of passing the --with-all-dependencies option toupdate, require or remove.

Libraries | Schema

Found a typo? Something is wrong in this documentation?Fork and edit it!