w3resource

Composer command line interface and commands (Part 2)

Introduction

This is the second part of the Composer command line interface and commands.

#update/u#update/u

The “composer update” command gets the latest versions of the dependencies required by the project and then update the “composer.lock” file.

compose update

The above  will resolve all the dependencies in our project and write the exact versions into composer.lock file.

We can only update a few packages simply by listing them as shown below:

composer update vendor/package4  vendor/package6

We can also use the composer wild card command as shown in the code snippet below:

The above command will update all the packages in the vendor folder and also update the “composer.lock “ file.

composer update update vendor/*

The “composer update” command have the following options:

  • --prefer-source: With this command, packages will be installed from source if available.
  • --prefer-dist: With this command, packages will be installed from dist if available.
  • --dry-run: This simulates an update, without actually updating the dependencies.
  • --dev: This simulates composer default behavior of installing packages listed in the “require-dev” of the composer.json file.
  • --no-dev: This causes composer to skip installing packages listed in “require-dev”. After which the “composer.autoload” file is not generated
  • --lock: Updates only the lock file, thus removing unnecessary errors.
  • --no-autoloader: This cause the generation of autoloader file to be skipped.
  • --no-scripts: This causes composer to skip the execution of scripts listed in the composer.json file.
  • --no-progress: This stopes progress display, thereby saving terminal or scripts that cannot handle back slashes.
  • --no-suggest: Installs packages and also skip the suggestion of other recommended packages.
  • --with-dependencies: This adds all the dependencies of whitelisted packages to the whitelist, except those that are root requirements.
  • --with-all-dependencies: This is the same as “—with-dependencies” command except that this also whitelist, packages  that are root requirements.
  • --optimize-autoloader (-o): This is used to optimize class map for faster autoloader. This is not composer default behavior since it takes a bit of time to complete.
  • --classmap-authoritative (-a): This autoloads all the classes from classmap only. It implicitly enables --optimize-autoloader.
  • --apcu-autoloader: This command uses  APCu to cache found/not-found classes.
  • --ignore-platform-reqs: This ignores all the system requirements and go about installing the packages, even when the requirements are not met.
  • --prefer-stable: This is used to prefer the installation of stable versions of dependencies.
  • --prefer-lowest: This is used to prefer the installation of lowest versions of dependencies. It is very useful when testing minimal versions of requirements.
  • --interactive: This command provides an interactive interface with autocompletion to select the packages to update.
  • --root-reqs: This restricts the ability  update to our first degree dependencies.

#require

The require command is used to add new packages to the composer.json file from the current directory. If the file does not exist, it will be created on the go.

composer require

After adding and changing the requirements, these affected packages will be installed or updated. We can also specify the exact package to require by passing the package name as shown in the code snippet below.

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

If we do not specify the exert package, composer will prompt us to search for the package, and the given results will provide us a list of matches to require.

The “composer require” command have the following options:

  • --dev: This simulates composer default behavior of installing packages listed in the “require-dev” of the composer.json file.
    composer require –- dev
  • --prefer-source: With this command, packages will be installed from source if available.
  • --prefer-dist: With this command, packages will be installed from dist if available.
  • --no-progress: This stopes progress display, thereby saving terminal or scripts that cannot handle back slashes.
  • --no-scripts: This causes composer to skip the execution of scripts listed in the composer.json file.
  • --update-no-dev: This updates our dependency update with the --no-dev option.
  • --update-with-dependencies: Also update dependencies of the newly required packages, except those that are root requirements.
    composer require -–update-with-dependencies
  • --update-with-all-dependencies: Also update dependencies of the newly required packages, including those that are root requirements.
  • --ignore-platform-reqs: This tells composer to  ignore php, hhvm, lib-* and ext-* requirements and force the installation even when the target machine does not meet the specifications.
  • --prefer-stable: Installs stable versions of dependencies.
  • --prefer-lowest: Install only lowest versions of dependencies. Used in testing minimal versions of requirements.
  • --sort-packages: sorts packages in “composer.json” file.
  • --optimize-autoloader (-o):This command  Convert PSR-0/4 autoloading to classmap to get an optimized autoloader. This is commonly used for production.
  • --classmap-authoritative (-a): This autoloads all the classes from classmap only. It implicitly enables --optimize-autoloader.
  • --apcu-autoloader: This command uses  APCu to cache found/not-found classes.
    composer require - - apcu-autoloader

Previous: Composer Command Line Interface and Commands(Part 1)
Next: Composer command line interface and commands (Part 3)



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/php/composer/composer-command-line-interface-and-commands-part-2.php