问题
composer install
will install whenever stated in the composer.lock
file, but composer update
will update all the dependencies and create a new composer.lock
file based on what is required in composer.json
.
So many said only run composer update
in development. But my question is doing composer update
did replaced the old composer.lock
file, if your app is going to break it will break, because there might be conflict with the new updated dependencies.
I came across with a situation where I must do composer update
, the issue is related to pcntl
extension. The only solution is to do composer update
PHP pcntl module installation
I don't understand why people are afraid of running composer update
on production.
回答1:
My thoughts about this are,
- The current working state of the system is very important as I would assume some tests have been run against it.
- To do composer update would mean that, libraries that are part of the app would have their updates and which may lead to breakage in the system. Because they are libraries that depends on libraries that depends on libraries.
- Finally, I would rather do this if
composer-update
is needed:- Checkout on a dev environment and
composer update
, - Ensure the app is thoroughly tested on a dev environment
- then install on live/production with
composer install
- Checkout on a dev environment and
回答2:
TLDR;
Do not run composer update
nor composer install
in production. Execute it somewhere else and upload the result to production. But if you HAVE to run either: always run install
and create a fresh installation; and never update
. install
is more predictable and reliable, with update
you are at the mercy of any of the project's dependencies.
Composer works recursively. So even if you have very tight version constraints in your composer.json
, by running composer update
you would be updating not only your dependencies, but your dependencies' dependencies.
While most of the time this won't introduce breakage, sometimes it will. One dependency down the line may introduce a change of behaviour that may impact your code in a way you may have not tested against.
Also, it's basically using the wrong tool for the job. Composer is a dependency management tool, not a deployment tool. To deploy your code to production you should be using some sort of code deployment tool (even if that "tool" is as simple as an FTP upload and a couple of scripts).
The appropriate flow is:
- Do all the
require
andupdate
calls on your development machine, where you can test the project without risk. This generates acomposer.lock
, which is a known state for the whole project, with discrete installed versions. Create a new installable version doing
install --no-dev
. On this step you also should dump an optimized autoloader, run after-install scripts, etc. I usually separate this in more than one step:composer install --prefer-dist --no-scripts --no-progress --no-suggest --no-interaction --no-dev
:^^ This for a complete, silent installation of everything, excluding development dependencies.
composer dump-autoload --optimize --no-dev
^^ To dump an optimized autoloader script suitable for production.
composer run-script --no-dev post-install-cmd
^^ This is mostly for Symfony, but if you have any post-install scripts to run (e.g. to copy assets to your "public" directory, warmp-up some type of cache, anything like that), this would be a good moment to do it.
The result of the above step should be tested (in what typically is a staging environment), and then pushed to production whole (your client code, the vendor folder, the configuration tailored for prod, etc); using whatever deployment method you prefer.
回答3:
My thoughts here :
You should never use composer update without argument.
composer update
reads every package listed on composer.json, and updates it to the latest available version compatible with the specified version constraints.
In a perfect world, all librairies would follow semver correctly, and it shouldn't have any side effects. But technically, that is never always true, and you could download a version incompatible with the previous one, or just a version with uncorrected bugs.
So, updating all your packages at once would probably lead to some issues, unless you have the time to check everything on your website to ensure nothing went wrong.
But of course, you'll have to update specific packages sometimes, so using composer update xxx/xxx
is useful, assuming you'll check all your implementations of the package.
When the updated package is fully tested, you can commit your code to staging/production, and then run composer install
to ensure you'll have the same exact version of package and dependencies on all your platforms.
Long story short, here's the process I use :
composer require xxx/xxx
to install new packagescomposer update xxx/xxx
to update a specific packagecomposer install
on all environments when the package.lock file has been updated.
Additional thoughts
I stumbled once upon an implementation which would give the exact version of the package in composer.json. The developer explained that this way you could use composer update
without damage.
I disagree with this option, since even with the exact versions in composer.json, the dependencies are not fixed, and a composer update could lead to potential bugs in them.
来源:https://stackoverflow.com/questions/42065931/why-should-i-never-run-composer-update-in-production