Global Parameters
The following parameters can be used in combination with every command:
-
–verbose (-v): Increase the level of detail in the feedback information.
- -v means normal output.
- -vv means more detailed output.
- -vvv is for debugging.
- –help (-h): Display help information.
- –quiet (-q): Suppress all output.
- –no-interaction (-n): Do not ask any interactive questions.
- –working-dir (-d): If specified, use the given directory as the working directory.
- –profile: Display timing and memory usage information.
- –ansi: Force ANSI output.
- –no-ansi: Disable ANSI output.
- –version (-V): Display version information for the current application.
Process Exit Codes
- 0: OK
- 1: General/unknown error
- 2: Dependency resolution error
Initialize init
In the chapter on “Libraries”, we saw how to manually create a composer.json file. In fact, there is also an init command that makes this much easier.
When you run this command, it will interactively ask you to fill in some information, while intelligently using some default values.
php composer.phar init
Initialize – Options
- –name: The package name.
- –description: The package description.
- –author: The package author.
- –homepage: The package homepage.
-
–require: Other packages required as dependencies; each must include a version constraint. They should follow the format
foo/bar:1.0.0. - –require-dev: Development dependencies, using the same format as –require.
-
–stability (-s): The value of the
minimum-stabilityfield.
Installing install
The install command reads the composer.json file from the current directory, resolves the dependencies, and installs them into the vendor directory.
php composer.phar install
If a composer.lock file exists in the current directory, it will read the dependency versions from that file instead of retrieving dependencies based on the composer.json file. This ensures that every user of the library gets the same versions of the dependencies.
If there is no composer.lock file, composer
will create it after resolving the dependencies.
Install – Options
-
–prefer-source: There are two ways to download packages:
sourceanddist. For stable versions,
composer
will usedistby default.sourcerefers to the version control source
. If--prefer-sourceis enabled, composer
will install fromsource(if available). This is very useful if you want to apply a
bugfix
to your project. It also allows dependencies to be fetched directly from the local repository. -
–prefer-dist: The opposite of
--prefer-source; composer
will try to fetch fromdistwhenever possible, which will significantly speed up installation on
build servers. This is also a way to avoid git
issues if you are not sure how to set it up correctly. -
–dry-run: If you only want a demonstration rather than actually installing a package, you can run the
--dry-runcommand, which will simulate the installation and show what would happen. -
–dev: Install the packages listed in the
require-devfield (this is the default). -
–no-dev: Skip the packages listed in the
require-devfield. -
–no-scripts: Skip the scripts defined in the
composer.jsonfile. - –no-plugins: Disable plugins.
- –no-progress: Removes progress output, which can prevent garbled display in terminals or scripts that do not handle line breaks properly.
-
–optimize-autoloader (-o): Convert
PSR-0/4 autoloading to a classmap
for faster loading support. This is especially recommended in production environments, but since it takes some time to run, it is not the default.
Update update
To get the latest versions of the dependencies and update the composer.lock file, you should use the update command.
php composer.phar update
This will resolve all dependencies for your project and write the exact version numbers into composer.lock.
If you only want to update a few packages, you can list them individually like this:
php composer.phar update vendor/package vendor/package2
You can also use wildcards to update in bulk:
php composer.phar update vendor/*
Update – Options
-
–prefer-source: Install from
sourcewhen available. -
–prefer-dist: Install from
distwhen available. - –dry-run: Simulate the command without performing any actual operations.
-
–dev: Install the packages listed in the
require-devfield (this is the default). -
–no-dev: Skip the packages listed in the
require-devfield. -
–no-scripts: Skip the scripts defined in the
composer.jsonfile. - –no-plugins: Disable plugins.
- –no-progress: Remove progress output to avoid messy displays in terminals or scripts that do not handle newlines properly.
-
–optimize-autoloader (-o): Convert
PSR-0/4 autoloading to a classmap
for faster loading support. This is especially recommended in production environments, but since it takes some time to run, it is not the default. -
–lock: Only update the
hash in the lock file, suppressing warnings about the lock file being outdated. - –with-dependencies Also update the dependencies of packages in the whitelist; this will update them recursively.
Declare Dependencies require
The require command adds new dependency packages to the composer.json file in the current directory.
php composer.phar require
When adding or changing dependencies, the modified dependencies will be installed or updated.
If you do not want to specify the dependency package interactively, you can directly specify it in this command.
php composer.phar require vendor/package:2.* vendor/package2:dev-master
Declare Dependencies – Options
-
–prefer-source: Install from
sourcewhen available. -
–prefer-dist: Install from
distwhen available. -
–dev: Install the packages listed in the
require-devfield. - –no-update: Disable automatic dependency updates.
- –no-progress: Remove progress information, which can avoid garbled display in some terminals or scripts that do not handle line breaks properly.
- –update-with-dependencies Update the dependencies of newly installed packages as well.
Global Execution global
The global command allows you to run other commands such as install, require, or update in the COMPOSER_HOME directory.
And if you add $COMPOSER_HOME/vendor/bin to the $PATH environment variable, you can use it to install global applications from the command line. Here is an example:
php composer.phar global require fabpot/php-cs-fixer:dev-master
Now php-cs-fixer can be used globally (assuming you have already set your
PATH). If you want to update it later, you only need to run global update:
php composer.phar global update
Search search
The search command allows you to search for dependency packages for the current project. Usually it only searches packages
on packagist.org, and you can simply enter your search terms.
php composer.phar search monolog
You can also perform multi-condition searches by passing multiple arguments.
Search – Options
- –only-name (-N): Search only for the specified name (exact match).
Show show
To list all available packages, you can use the show command.
php composer.phar show
If you want to see detailed information about a package, you can enter a package name.
php composer.phar show monolog/monolog
name : monolog/monolog
versions : master-dev, 1.0.2, 1.0.1, 1.0.0, 1.0.0-RC1
type : library
names : monolog/monolog
source : [git] http://github.com/Seldaek/monolog.git 3d4e60d0cbc4b888fe5ad223d77964428b1978da
dist : [zip] http://github.com/Seldaek/monolog/zipball/3d4e60d0cbc4b888fe5ad223d77964428b1978da 3d4e60d0cbc4b888fe5ad223d77964428b1978da
license : MIT
autoload
psr-0
Monolog : src/
requires
php >=5.3.0
You can even enter a package version number to display detailed information for that version.
php composer.phar show monolog/monolog 1.0.2
Show – Options
- –installed (-i): List installed dependencies.
- –platform (-p): List only platform packages (PHP and its extensions).
- –self (-s): List only information about the current project.
Dependency Check depends
The depends command lets you find out whether a package installed in your project is being depended on by other packages, and lists them.
php composer.phar depends --link-type=require monolog/monolog
nrk/monolog-fluent
poc/poc
propel/propel
symfony/monolog-bridge
symfony/symfony
Dependency Check – Options
-
–link-type: The type to check; defaults to
requirebut can also berequire-dev.
Validation validate
Before committing the composer.json file and creating a tag,
you should always run the validate command. It will check whether your composer.json file is valid.
php composer.phar validate
Validation options
-
–no-check-all: Whether Composer
should perform full validation.
Dependency package status check status
If you often modify code inside dependency packages, and they are installed from
source (a custom source), then the status command allows you to check for any local changes and will notify you if there are any.
php composer.phar status
You can use the --verbose series of options (-v|vv|vvv) to get more detailed information:
php composer.phar status -v
You have changes in the following dependencies:
vendor/seld/jsonlint:
M README.mdown
Self-update self-update
To upgrade Composer
itself to the latest version, simply run the self-update command. It will replace your composer.phar file with the latest version.
php composer.phar self-update
If you want to upgrade to a specific version, you can simply specify it like this:
php composer.phar self-update 1.0.0-alpha7
If you have installed Composer system-wide (see global installation), you may need to run it with root privileges:
sudo composer self-update
Self-update – Options
- –rollback (-r): Roll back to the last version you had installed.
- –clean-backups: Remove old backups during the update process, so that the updated current version is the only backup available.
Change configuration config
The config command allows you to edit some of Composer’s basic settings,
whether in the local composer.json or the global config.json file.
php composer.phar config --list
Changing Configuration – Usage
config [options] [setting-key] [setting-value1] ...
[setting-valueN]
setting-key is the name of a configuration option, and setting-value1 is a configuration value. Arrays can be used as configuration values (such as github-protocols), and multiple setting-value entries are allowed.
For valid configuration options, see config in the “Schema” section.
Changing Configuration – Options
-
–global
(-g): Operate on the global configuration file located at$COMPOSER_HOME/config.json. If this option is not specified, this command will affect the
composer.json
file of the current project, or the file pointed to by the--fileoption. -
–editor (-e): Open the
composer.json
file in a text editor. By default, it always opens the current project’s file. When the--globaloption is present, it will open the global
composer.json file. -
–unset: Remove the configuration option named by
setting-key. -
–list (-l): Display a list of the current configuration options. When the
--globaloption is present, it will display a list of the global configuration options. -
–file=”…” (-f): Operate on a specified file instead of
composer.json. Note: cannot be used together with the--globaloption.
Modifying Package Sources
In addition to changing configuration options, the config command also supports modifying repository information in the following way:
php composer.phar config repositories.foo vcs http://github.com/foo/bar
Create Project create-project
You can use Composer
to create a new project from an existing package. This is equivalent to running a git clone or svn checkout command and then installing that package’s dependencies into its own vendor
directory.
This command has several common uses:
- You can deploy your application quickly.
- You can check out any package and develop patches for it.
- For projects developed by multiple people, it can be used to speed up application initialization.
To create a new Composer-based project, you can use the
“create-project”
command. Pass it a package name, and it will create a project directory for you. You can also specify a version number as the third argument; otherwise, the latest version will be fetched.
If the directory does not currently exist, it will be created automatically during installation.
php composer.phar create-project doctrine/orm path 2.2.*
In addition, you can also start this project without using this command by using an existing composer.json file.
By default, this command looks for the package you specify on packagist.org.
Create Project – Options
-
–repository-url: Provide a custom repository to search for the package, which will be used instead of
packagist.org. This can be an
HTTP
URL pointing to acomposerrepository, or a local path to apackages.jsonfile. -
–stability (-s): The minimum stability of the package, defaulting to
stable. -
–prefer-source: Install from
sourcewhen available. -
–prefer-dist: Install from
distwhen available. -
–dev: Install the packages listed in the
require-devfield. - –no-install: Disable installation of package dependencies.
- –no-plugins: Disable plugins.
- –no-scripts: Prevent execution of scripts defined in the root package.
- –no-progress: Remove progress output; this can avoid garbled display in some terminals or scripts that do not handle line breaks properly.
-
–keep-vcs: Skip missing VCS
when creating. This is very useful if you run the create command in non-interactive mode.
Dump Autoload Index dump-autoload
In some cases, you need to update the
autoloader, for example after adding a new class to your package. You can do this with dump-autoload without having to run the install or update command.
In addition, it can generate an optimized index of classes that complies with the PSR-0/4
standards, which is also done for performance reasons. In large applications there are many class files, and the
autoloader can take up a significant portion of each request. Using classmaps
may not be very convenient during development, but it still lets you benefit from
the convenience provided by the PSR-0/4 standards while maintaining performance.
Dump Autoload Index – Options
-
–optimize (-o): Convert PSR-0/4
autoloading to a classmap
for faster loading speeds. This is especially useful in production environments, but it may take some time to run, so it is not enabled by default at this time. - –no-dev: Disable autoload-dev rules.
View Licenses licenses
Lists the name, version, and license of each installed package. You can use the --format=json option to get
output in JSON format.
Run Script run-script
You can run this command to manually execute a script; you only need to specify the script name. The optional --no-dev option allows you to disable developer mode.
Diagnose diagnose
If you think you have found a bug
or the program is behaving strangely, you may want to run the diagnose command to help detect some common problems.
php composer.phar diagnose
Archive archive
This command is used to create a zip/tar
archive of a specified version of a specified package. It can also be used to archive your entire project, excluding
excluded/ignored files.
php composer.phar archive vendor/package 2.0.21 --format=zip
Archive – Options
-
–format (-f): Specify the archive format: tar or
zip (default is tar). - –dir: Specify the directory where the archive will be stored (default is the current directory).
Get Help help
Use help to get help information for a specified command.
php composer.phar help install
Environment Variables
You can set some environment variables to override the default configuration. It is recommended to set these values in the config field of composer.json whenever possible, rather than setting environment variables via the command line. Note that values in environment variables will always take precedence over values specified in composer.json.
COMPOSER
The COMPOSER environment variable can specify a different filename for the composer.json file.
For example:
COMPOSER=composer-other.json php composer.phar install
COMPOSER_ROOT_VERSION
By setting this environment variable, you can specify the version of the root
package if the program cannot infer the version number from VCS
and it is not declared in the composer.json file.
COMPOSER_VENDOR_DIR
By setting this environment variable, you can specify a directory other than vendor for composer
to install dependencies into.
COMPOSER_BIN_DIR
By setting this environment variable, you can specify a directory other than vendor/bin for the bin (Vendor
Binaries) directory.
http_proxy or HTTP_PROXY
If you are using
Composer through an HTTP proxy, you can use the http_proxy or HTTP_PROXY environment variables. Simply set it to the proxy server’s
URL. Many operating systems already set this variable for you.
It is recommended to use http_proxy (lowercase), or define both. This is because some tools, such as
git or curl,
use the lowercase version http_proxy. In addition, you can also use git config --global http.proxy <proxy url> to set
git’s proxy separately.
no_proxy
If you are using a proxy server and want to disable the proxy for certain domains, you can use the no_proxy environment variable. Just enter a comma-separated exclusion list of domain names.
This environment variable accepts domain names, IPs, and
CIDR address blocks. You can limit it to a port (for example: :80). You can also set it to * to ignore all
HTTP proxy requests.
HTTP_PROXY_REQUEST_FULLURI
If you are using an HTTP
proxy, but it does not support the request_fulluri tag, then you should set this environment variable to false or 0 to prevent
composer from reading the configuration from request_fulluri.
HTTPS_PROXY_REQUEST_FULLURI
If you are using an HTTPS
proxy, but it does not support the request_fulluri tag, then you should set this environment variable to false or 0 to prevent
composer from reading the configuration from request_fulluri.
COMPOSER_HOME
The COMPOSER_HOME environment variable allows you to change Composer’s
home directory. This is a hidden, global directory shared by all projects (available to all users on the local machine).
Its default values on different systems are:
- *nix
/home/<user>/.composer. - OSX
/Users/<user>/.composer. -
Windows
C:Users<user>AppDataRoamingComposer.
COMPOSER_HOME/config.json
You can place a config.json file in the COMPOSER_HOME directory. When you run the install and update commands, Composer
will merge it with the composer.json file in your project.
This file allows you to set configuration options and repositories for the user’s projects.
If the global and project configurations contain the same setting, then the project’s composer.json file takes precedence.
COMPOSER_CACHE_DIR
The COMPOSER_CACHE_DIR environment variable allows you to set Composer’s
cache directory, which can also be configured via cache-dir.
Its default values on different systems are:
- *nix and OSX
$COMPOSER_HOME/cache. -
Windows
C:Users<user>AppDataLocalComposeror%LOCALAPPDATA%/Composer.
COMPOSER_PROCESS_TIMEOUT
This environment variable controls the wait time for commands executed by Composer (for example: git
commands). The default value is 300 seconds (5 minutes).
COMPOSER_DISCARD_CHANGES
This environment variable controls the discard-changes config option.
COMPOSER_NO_INTERACTION
If set to 1, this environment variable will make Composer
disable interaction whenever it executes any command, equivalent to using --no-interaction for all commands. This can be set when setting up a virtual machine/continuous integration server.