Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Composer command line


May 25, 2021 Composer


Table of contents


The command line

You've learned how to do something with the command-line interface. This chapter will introduce you to all available commands.

To get help from the command line, run the composer or composer list and then combine the --help command to get more help.

Global parameters

The following parameters can be used in conjunction with each command:

  • --verbose (-v): Increase the level of detail of the feedback.
    • -v indicates normal output.
    • -vv represents a more detailed output.
    • -vvv is for the debug.
  • --help (-h): Displays help information.
  • --quiet (-q): Do not output any information.
  • --no-interaction (-n): Don't ask any interactive questions.
  • --working-dir (-d): If specified, use a given directory as the working directory.
  • --profile: Displays time and memory usage information.
  • --ansi: Force ANSI output.
  • --no-ansi: Turn off the ANSI output.
  • --version (-V): Displays version information for the current application.

The process exit code

  • 0: OK
  • 1: Universal/Unknown error
  • 2: Dependency handling errors

Initialize init

In that chapter of the library we saw how to manually create a composer.json file. In fact, there is init command that makes it easier to do this.

When you run the command, it interactively asks you to fill in some information, while using some default values wisely.

php composer.phar init

Initialization - parameters

  • --name: The name of the package.
  • --description: Description of the package.
  • --author: The author of the package.
  • --homepage: The home page of the package.
  • --require: Other packages that need to be relied upon must have a version constraint. It should also foo/bar:1.0.0
  • --require-dev: The development version of the dependency package, the content format is the same as --require.
  • --stability (-s): The minimum-stability field.

Install the install

install command reads the composer.json the current directory, handles dependencies, and installs it into the vendor directory.

php composer.phar install

If a composer.lock file composer.lock it reads the dependent version from composer.json file. This ensures that each consumer of the library gets the same dependent version.

If composer.lock file, composer creates the dependency after it has been processed.

Installation-parameters

  • --prefer-source: There are two ways to download a package: source and dist T he dist method will be used by default for dist S ource, source other, represents the versioning source. I f --prefer-source enabled, composer will install source if any. T his is useful if you want to use a bugfix into your project. Dependencies can also be obtained directly from the local repository.
  • --prefer-dist: In contrast --prefer-source composer will get as dist as possible, which will greatly speed up the installation on build servers. This is also a way to avoid git questions if you don't know how to set it up correctly.
  • --dry-run: If you just want to demonstrate and not actually install a package, --dry-run which simulates the installation and shows what will happen.
  • --dev: Install the package listed in the require-dev field (this is a default).
  • --no-dev: Skip the require-dev field.
  • --no-scripts: Skip scripts composer.json file.
  • --no-plugins: Turn off plugins.
  • --no-progress: Removes progress information, which avoids confusing displays of terminals or scripts that do not handle line changes.
  • --optimize-autoloader (-o): Convert PSR-0/4 autoloading to classmap for faster load support. This is especially recommended in a production environment, but is not the default because it takes some time to run.

Update update

In order to get the latest version of the dependency composer.lock file, you should use update command.

php composer.phar update

This resolves all dependencies on the project and writes the exact version number composer.lock

If you just want to update a few packages, you can list them separately like this:

php composer.phar update vendor/package vendor/package2

You can also use wildcards for bulk updates:

php composer.phar update vendor/*

Update- Parameters

  • --prefer-source: Install from source when a package is source
  • --prefer-dist: Install from dist when a package dist
  • --dry-run: Simulates commands and does not do the actual thing.
  • --dev: Install the package listed in the require-dev field (this is a default).
  • --no-dev: Skip the require-dev field.
  • --no-scripts: Skip scripts composer.json file.
  • --no-plugins: Turn off plugins.
  • --no-progress: Removes progress information, which avoids confusing displays of terminals or scripts that do not handle line changes.
  • --optimize-autoloader (-o): Convert PSR-0/4 autoloading to classmap for faster load support. This is especially recommended in a production environment, but is not the default because it takes some time to run.
  • --lock: Update only the hash of the lock file and cancel the warning that the lock file is obsolete.
  • --with-dependencies also update the dependencies of packages in the whitelist, which is recursively updated.

Affirm the dependency need

require command adds a new dependency package to the composer.json directory.

php composer.phar require

When you add or change a dependency, the modified dependency is installed or updated.

If you don't want to specify a dependency package through interaction, you can specify the dependent package directly in this order.

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

Affirm dependency-parameters

  • --prefer-source: Install from source when a package is source
  • --prefer-dist: Install from dist when a package dist
  • --dev: Install the require-dev field.
  • --no-update: Disables automatic updates of dependencies.
  • --no-progress: Removes progress information, which avoids confusing displays of terminals or scripts that do not handle line changes.
  • --update-with-dependencies update the dependencies of new packages.

Global execution global

global command allows you to COMPOSER_HOME commands, such as install or update, in the require of update

And if you $COMPOSER_HOME/vendor/bin $PATH environment variable, you can use it to install the global app on the command line, here's an example:

php composer.phar global require fabpot/php-cs-fixer:dev-master

The php-cs-fixer worldwide (assuming you've set up your PATH). If you want to update it later, you just need to global update

php composer.phar global update

Search for search

search command allows you to search for dependency packages for the current item, usually searching only for packages on packagist.org, and you can simply enter your search criteria.

php composer.phar search monolog

You can also perform a multi-conditional search by passing multiple parameters.

Search-parameters

  • --only-name (-N): Search only for the specified name (exactly match).

Show show

List all available packages and you can use show command.

php composer.phar show

If you want to see the details of 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 the version number of a package to display the details of that version.

php composer.phar show monolog/monolog 1.0.2

Presentation-parameters

  • --installed (-i): Lists the installed dependency packages.
  • --platform (-p): Only platform packages (PHP and its extensions) are listed.
  • --self (-s): Only the current project information is listed.

Dependency detection depends

depends command finds out if a package has been installed in your project, is being relied 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 detection - parameters

  • --link-type: The type of detection, which defaults require or require-dev

Validity detection validate

You should always run the validate command before submitting the composer.json file and validate tag. It will detect if composer.json file is valid

php composer.phar validate

Validity detection parameters

  • --no-check-all: Whether composer performs a complete check.

Depends on the package state to detect the status

If you frequently modify the code in the dependency package, and they are installed from source status allows you to check, and it will prompt you if you have any local changes.

php composer.phar status

You can --verbose series parameters (-v|vvvvv| for more detail:

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, you only need to self-update command. It will replace your composer.phar to the latest version.

php composer.phar self-update

If you want to upgrade to a specific version, you can specify it as simple as this:

php composer.phar self-update 1.0.0-alpha7

If you already have Composer installed for the entire system (see Global Installation), you may need root it under root permission:

sudo composer self-update

Self-update-parameters

  • --rollback (-r): Roll back to the last version you installed.
  • --clean-backups: Delete the old backup during the update process, which makes the current version after the update the only backup available.

Change the config

config command allows you to edit some of composer's basic settings, whether composer.json or config.json file.

php composer.phar config --list

Change the configuration - Use method

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

setting-key is the name of a configuration option, setting-value1 is the value of a configuration. You can use arrays as configured values github-protocols and setting-value are allowed.

For valid configuration options, check out the config section of The Architecture.

Change the configuration-parameters

  • --global (-g): The operation $COMPOSER_HOME/config.json If you do not specify this parameter, this command affects the composer.json file for the current project, or --file parameter that points to the file.
  • --editor (-e): Use the text editor to open the composer.json file. T he file for the current project is always open by default. When the --global exists, the global composer.json file opens.
  • --unset: Removes the configuration setting-key the name by set-key.
  • --list (-l): Displays a list of current configuration options. When the --global exists, a list of global configuration options is displayed.
  • --file="..." ( -f): Operate on a specified file, not composer.json. Note: Cannot be used with the --global parameter.

Modify the package source

In addition to modifying configuration config the config command also supports modifying source information by:

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

Create a project create-project

You can use Composer to create a new project from an existing package. This is equivalent to executing a git clone svn checkout and installing the package's dependencies into its own vendor directory.

This command has several common uses:

  1. You can quickly deploy your app.
  2. You can check out any resource pack and develop its patches.
  3. Multiplayer development projects that you can use to accelerate the initialization of your app.

To create a new project based on Composer, you can use the create-project command. P ass a package name that creates a catalog of items for you. You can also specify the version number in the third parameter, otherwise you will get the latest version.

If the directory does not currently exist, it is automatically created during installation.

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

You can also start this project with an existing composer.json of using this command.

By default, this command looks packagist.org package on the page.

Create a project-parameter

  • --repository-url: Provides a custom repository to search for packages, which will be used instead of packagist.org. This can be an composer the composer repository, or a local path to a packages.json file.
  • --stability (-s): The lowest stable version of the resource pack, which defaults to stable
  • --prefer-source: Install from source when a package is source
  • --prefer-dist: Install from dist when a package dist
  • --dev: Install the require-dev field.
  • --no-install: Do not rely on installing packages.
  • --no-plugins: Disable plugins.
  • --no-scripts: Scripts defined in the root resource pack are prohibited.
  • --no-progress: Removes progress information, which avoids confusing displays of terminals or scripts that do not handle line changes.
  • --keep-vcs: Skip missing VCS when created. This is useful if you run the create command in non-interactive mode.

Print the autoload index dump-autoload

In some cases you need to update autoloader, such as adding a new class to your package. You can dump-autoload without having to execute install or update

In addition, it can print an index of an optimized class that conforms to the PSR-0/4 specification, which is also due to performance considerations. There will be many class files in large applications, and autoloader takes up a large portion of the time per request, and using classmaps may not be convenient to develop, but it can still be facilitated by the PSR-0/4 specification while guaranteeing performance.

Print autoload index-parameters

  • --optimize (-o): Convert PSR-0/4 autoloading to classmap for faster load speeds. This is especially useful for production environments, but may take some time to run, so it is not currently the default setting.
  • --no-dev: Disable autoload-dev rules.

View license licenses

Lists the name, version, and license agreement of each package that has been installed. You --format=json parameter to get the output of the JSON format.

Execute the script run-script

You can run this command to execute the script manually, simply specifying the name of the --no-dev allows you to disable developer mode.

Diagnose diagnose

If you think you've found a bug or the program is acting strangely, you might want to run diagnose command to help you detect some common problems.

php composer.phar diagnose

Archive archive

This command is used to archive the specified version of the specified package in zip/tar. It can also be used to archive your entire project, excluding excluded/ignored (excluded/ignored) files.

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

Archive-parameters

  • --format (-f): Specify the archive format: tar or zip (tar by default).
  • --dir: Specifies the directory where the archive is stored (the default is the current directory).

Get help with help

Use help get help information for the specified command.

php composer.phar help install

Environment variables

You can set some environment variables to override the default configuration. I t is recommended that you composer.json config field as much as possible, rather than setting environment variables from the command line. It is important to note that the value in the environment variable will always composer.json

COMPOSER

Environment variable COMPOSER specify additional file names composer.json files.

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 guess the version number from the VCS and does not composer.json file.

COMPOSER_VENDOR_DIR

By setting this environment variable, you can specify that composer will depend on other directories installed outside of vendor

COMPOSER_BIN_DIR

By setting this environment variable, you can bin Binaries) directory to a directory other than vendor/bin

http_proxy or HTTP_PROXY

If you are using Composer through an HTTP proxy, http_proxy or HTTP_PROXY HTTP_PROXY variables. J ust simply set it to the URL of the proxy server. Many operating systems have set this variable for your service.

We recommend http_proxy (small case) or both. B ecause some tools, like git or curl, will use http_proxy version. In addition, git config --global http.proxy <proxy url> set up git's proxy separately.

no_proxy

If you are using a proxy server and want to disable proxy for some domain names, you no_proxy variables. All you need to do is enter a list of domain names with commas separated.

This environment variable accepts domain names, IP, and CIDR address blocks. Y ou can limit it to one port (ex: :80 You can also set * to ignore all HTTP proxy requests.

HTTP_PROXY_REQUEST_FULLURI

If you use an HTTP proxy, request_fulluri tags, you should set this environment variable to false 0 to prevent composer from reading the configuration request_fulluri the file.

HTTPS_PROXY_REQUEST_FULLURI

If you use an HTTPS proxy, request_fulluri tags, you should set this environment variable to false 0 to prevent composer from reading the configuration from request_fulluri

COMPOSER_HOME

COMPOSER_HOME allows you to change Composer's home directory. This is a hidden global directory that all items share (available to all users of the machine).

Its default values on each system are:

  • *nix /home/<user>/.composer
  • OSX /Users/<user>/.composer
  • Windows C:\Users\<user>\AppData\Roaming\Composer

COMPOSER_HOME/config.json

You can COMPOSER_HOME config.json When you install update commands, Composer merges it with the composer.json in your project.

This file allows you to configure information and libraries for the user's project settings.

If global and project have the same configuration item, the composer.json has a higher priority.

COMPOSER_CACHE_DIR

COMPOSER_CACHE_DIR variable allows you to set composer's cache directory, which can also be configured through cache-dir.

Its default values on each system are:

  • *nix and OSX $COMPOSER_HOME/cache
  • Windows C:\Users\<user>\AppData\Local\Composer %LOCALAPPDATA%/Composer

COMPOSER_PROCESS_TIMEOUT

This environment variable controls the wait time for Composer to execute the command (ex: git command). The default 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 causes Composer to discard interaction when executing each command, equivalent to using --no-interaction This can be done when building virtual machines/continuous integration servers.