What's the difference between composer install , composer update and composer require


Traditionally, managing third-party code in PHP projects is not an easy maintainable task. Fortunately, the PHP community has Composer, a top-notch dependency manager.

composer install

To add dependencies you need to add it manually to the composer.json file.

If composer.lock file exists, install exactly what's specificated on this file

  1. Otherwise read composer.json file to look out what dependencies needs to be installed
  2. Write the composer.lock with the information of the project (installed dependencies)

Not any component will be updated with this command.

composer update

To add or remove dependencies you need to add it manually to the composer.json file

  1. The composer.lock file will be ignored
  2. composer.json file dependencies will be installed and updated (if a dependency is not installed it will be downloaded)

If you can't (or don't know how to add or remove a library which is in fact easy,just add the name of the dependency and version in the require property of the file) modify the composer.json file manually or you prefer use the command line instead, composer has special functions for this :

composer require

For example if we want to add a dependency with the command line we will simply execute 

composer require twig/twig
  1. composer.json file will be modified automatically and the new dependency will be added
  2. the dependency will be downloaded to the project

composer remove

If you want to remove an unused dependency we will execute simply :

composer remove twig/twig --update-with-dependencies
  1. Twig will be removed with all his dependencies

However is recommendable to remove the dependency from the require property of the file manually and then execute composer update.

Become a more social person