问题
What are the differences between composer update
and composer install
?
回答1:
composer update
composer update
will update your depencencies as they are specified in composer.json
For example, if you require this package as a dependency:
"mockery/mockery": "0.9.*",
and you have actually installed the 0.9.1
version of the package, running composer update
will cause an upgrade of this package (for example to 0.9.2
, if it's already been released)
in detail composer update
will:
- Read
composer.json
- Remove installed packages that are no more required in
composer.json
- Check the availability of the latest versions of your required packages
- Install the latest versions of your packages
- Update
composer.lock
to store the installed packages version
composer install
composer install
will not update anything; it will just install all the dependencies as specified in the composer.lock
file
In detail:
- Check if
composer.lock
file exists (if not, runcomposer-update
and create it) - Read
composer.lock
file - Install the packages specified in the
composer.lock
file
When to install and when to update
composer update
is mostly used in the 'development phase', to upgrade our project packages according to what we have specified in thecomposer.json
file,composer install
is primarily used in the 'deploying phase' to install our application on a production server or on a testing environment, using the same dependencies stored in the composer.lock file created by composer update.
回答2:
When you run composer install
it will look for a lock file and install whatever is contained in it, if it can't find one, it'll read composer.json
, install its dependencies and generate a lockfile.
When you run composer update
it simply reads composer.json
, installs the dependencies and updates the lockfile (or creates a new lockfile).
回答3:
composer install
- If
composer.lock
does exist.- Processes and installs dependencies from the
composer.lock
file.
- Processes and installs dependencies from the
- If
composer.lock
does not exist.- Process package installs from
composer.json
. - Creates the
composer.lock
file based on the installed packages.
- Process package installs from
As per: composer help install
:
The install command reads the
composer.lock
file from the current directory, processes it, and downloads and installs all the libraries and dependencies outlined in that file. If the file does not exist it will look forcomposer.json
and do the same.
composer update
- Processes dependencies from the
composer.json
file (installs, updates and removes). - Creates or updates the
composer.lock
file according to the changes.
As per: composer help update
:
The update command reads the
composer.json
file from the current directory, processes it, and updates, removes or installs all the dependencies.
See also: Composer: It’s All About the Lock File
回答4:
The best difference between composer update
and composer install
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
- Otherwise read composer.json file to look out what dependencies needs to be installed
- 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
- The composer.lock file will be ignored
- 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
- composer.json file will be modified automatically and the new dependency will be added
- 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
- Twig will be removed with all his dependencies
来源:https://stackoverflow.com/questions/33052195/what-are-the-differences-between-composer-update-and-composer-install