为什么 PHP Composer 这么慢?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28436237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is PHP Composer so slow?
提问by AgmLauncher
Why is PHP Composerso slow when all I do is init a project with zero dependencies? Here are the commands I run:
当我所做的只是初始化一个零依赖项的项目时,为什么PHP Composer如此缓慢?这是我运行的命令:
composer init
<step through composer.json creation, define 0 zero dependencies>
<逐步完成composer.json的创建,定义0个零依赖>
composer install
Wait 3 minutes (not an exaggeration).
等待 3 分钟(不夸张)。
All composer has to do is pull in an autoloader and create /vendor
, so why does it take so long? For that matter, why doesn't that step happen on composer init
?
作曲家所要做的就是拉入一个自动加载器并创建/vendor
,那么为什么要花这么长时间呢?就此而言,为什么那一步没有发生composer init
?
Is there a configuration option I can use to pull in a cached autloader and vendor upon init
?
是否有一个配置选项可以用来拉入缓存的自动加载程序和供应商init
?
采纳答案by adjco
回答by hiraku
Because Composer is implemented by file_get_contents()
. That has no TCP optimizations, no Keep-Alive, no multiplexing, etc.
因为 Composer 是由file_get_contents()
. 没有 TCP 优化,没有 Keep-Alive,没有多路复用等。
I created a Composer plugin to download packages in parallel: https://packagist.org/packages/hirak/prestissimo
我创建了一个 Composer 插件来并行下载包:https: //packagist.org/packages/hirak/prestissimo
$ composer global require hirak/prestissimo
Please try it. In my environment, composer install
becomes 10 times faster.
请尝试一下。在我的环境中,composer install
速度提高了 10 倍。
回答by sgr12
- Make sure you have the latest version of Composer.
- Install in verbose mode by adding -vvv, for example
composer global require "squizlabs/php_codesniffer=*" -vvv
- If you are able to find out where Composer is facing slowness, for example mine was getting stuck for 5 minutes when downloading packages. It took >5 minutes to download a 20 kB file on a 50 Mbit/s connection. This was because it was downloading the packages from packagist using HTTP and not HTTPS. Making these changes to the configuration has resolved my issue:
composer config --global repo.packagist composer https://packagist.org
- 确保您拥有最新版本的 Composer。
- 例如,通过添加 -vvv 以详细模式安装
composer global require "squizlabs/php_codesniffer=*" -vvv
- 如果您能够找出 Composer 遇到缓慢的地方,例如我的下载包时卡住了 5 分钟。在 50 Mbit/s 连接上下载 20 kB 文件需要 5 分钟以上。这是因为它使用 HTTP 而不是 HTTPS 从 packagist 下载包。对配置进行这些更改解决了我的问题:
composer config --global repo.packagist composer https://packagist.org
回答by ppan
The same here. Get more details with "composer install --profile -vvv". In my case it takes a long time to download a few JSON files. They get cached on my server, but they are still downloaded with every Composer update/install call.
和这里一样。使用“composer install --profile -vvv”获取更多详细信息。就我而言,下载一些 JSON 文件需要很长时间。它们缓存在我的服务器上,但仍然会在每次 Composer 更新/安装调用时下载。
... 30 minutes later ...
…… 30 分钟后……
It looks like some performance problem @packagist.org. Now the Composer install runs within 2 seconds! And downloaded JSON files are properly cached.
看起来像一些性能问题@packagist.org。现在 Composer 安装在 2 秒内运行!并且下载的 JSON 文件被正确缓存。
回答by Aaron Chamberlain
I was running into this issue, and it was throwing me for a curve since I don't have Xdebug installed anywhere on my machine. It turns out that it was IPv6addressing mode failures. So to test I ran
我遇到了这个问题,它让我陷入困境,因为我的机器上没有安装 Xdebug。结果证明是IPv6寻址模式失败。所以为了测试我跑了
curl --ipv4 'https://packagist.org/packages.json'
curl --ipv6 'https://packagist.org/packages.json'
IPv4 went through, but IPv6 failed. In the end, you should look to find out why you're networking stack doesn't support it, but in my case, I decided to just give preference to IPv4 traffic until I can resolve that. On CentOSI created/modified the file /etc/gai.conf and put in the following:
IPv4 通过,但 IPv6 失败。最后,您应该看看为什么您的网络堆栈不支持它,但就我而言,我决定只优先考虑 IPv4 流量,直到我能解决这个问题。在CentOS 上,我创建/修改了文件 /etc/gai.conf 并输入以下内容:
label ::1/128 0
label ::/0 1
label 2002::/16 2
label ::/96 3
label ::ffff:0:0/96 4
precedence ::1/128 50
precedence ::/0 40
precedence 2002::/16 30
precedence ::/96 20
precedence ::ffff:0:0/96 100
On Ubuntu you can also edit that file and uncomment the line
在 Ubuntu 上,您还可以编辑该文件并取消注释该行
precedence ::ffff:0:0/96 100
回答by Liam Mitchell
On Ubuntu Xenial 16.04 VPS, you need to do the following:
在 Ubuntu Xenial 16.04 VPS 上,您需要执行以下操作:
sudo sh -c "echo 'precedence ::ffff:0:0/96 100' >> /etc/gai.conf"
composer global require hirak/prestissimo
回答by sh6210
In my case, the composer version i was running backdated. After updated the composer version itself the problem was gone.
就我而言,我运行的作曲家版本已过时。更新作曲家版本本身后,问题就消失了。
To update the composer version run
更新 Composer 版本运行
composer self-update
and then require the composer package as sudo worked for me.
然后需要 composer 包,因为 sudo 为我工作。
sudo composer require "<package-name>"
回答by FentomX1
Prior I tried every single solution mention in this question to this time. However, it still did not help me, though I tried/noticed installing any other single library was considerably fast, so I went on investigating libraries one by one which could cause the bottle-neck. And I located it to be codeception, "codeception/codeception": "2.4.0",
之前我尝试了这个问题中提到的每一个解决方案。然而,它仍然没有帮助我,尽管我尝试/注意到安装任何其他单个库都相当快,所以我继续一个一个地调查可能导致瓶颈的库。我把它定位为代码接收,"codeception/codeception": "2.4.0",
It took almost 200 seconds alone, though running it together with other libraries seemed at some times running almost for 12 minutes. Furthermore composer does not seem to reflect real-time when after initiating for 20 seconds it outputs it run 0.35s and so on later.
单独花费了将近 200 秒,尽管与其他库一起运行它有时似乎运行了将近 12 分钟。此外,composer 在启动 20 秒后输出它运行 0.35 秒后似乎没有反映实时,依此类推。
Btw, prestissimo as I mentioned was already applied and it seemed to help, as on another very fast computer which has much faster internet altogether without codeception, installation took 5 times more, 518 seconds, while perhaps due to prestissimo or other advices here, it took 110 seconds.
顺便说一句,我提到的 prestissimo 已经应用,它似乎有所帮助,因为在另一台非常快的计算机上,它的互联网速度要快得多,而无需编码,安装需要 5 倍多,518 秒,而也许是由于这里的 prestissimo 或其他建议,它用了 110 秒。
[374.9MiB/173.14s] 39/45: https://codeload.github.com/sebastianbergmann/php-text-template/legacy.zip/31f8b717e51d9a2afca6c9f046f5d69fc27c8686
[374.9MiB/173.16s] 40/45: https://codeload.github.com/sebastianbergmann/diff/legacy.zip/720fcc7e9b5cf384ea68d9d930d480907a0c1a29
[374.9MiB/173.17s] 41/45: https://codeload.github.com/sebastianbergmann/exporter/legacy.zip/68609e1261d215ea5b21b7987539cbfbe156ec3e
[374.9MiB/173.19s] 42/45: https://codeload.github.com/sebastianbergmann/phpunit-mock-objects/legacy.zip/f9756fd4f43f014cb2dca98deeaaa8ce5500a36e
[374.9MiB/173.20s] 43/45: https://codeload.github.com/sebastianbergmann/recursion-context/legacy.zip/5b0cd723502bac3b006cbf3dbf7a1e3fcefe4fa8
[374.9MiB/173.23s] 44/45: https://codeload.github.com/sebastianbergmann/php-code-coverage/legacy.zip/4cab20a326d14de7575a8e235c70d879b569a57a
[374.9MiB/173.24s] 45/45: https://codeload.github.com/doctrine/instantiator/legacy.zip/ae466f726242e637cebdd526a7d991b9433bacf1
[374.9MiB/173.24s] Finished: success: 45, skipped: 0, failure: 0, total: 45
[374.8MiB/173.25s] Analyzed 6622 packages to resolve dependencies
[374.8MiB/173.25s] Analyzed 435842 rules to resolve dependencies
[374.8MiB/173.25s] Package operations: 54 installs, 0 updates, 0 removals
[374.8MiB/173.26s] Installs: sebastian/recursion-context:3.0.0, sebastian/exporter:3.1.2, doctrine/instantiator:1.3.0, phpunit/php-text-template:1.2.1, phpunit/phpunit-mock-objects:6.1.2, codeception/stub:1.0.4, sebastian/diff:3.0.2, sebastian/comparator:3.0.2, theseer/tokenizer:1.1.3, sebastian/version:2.0.1, sebastian/environment:3.1.0, sebastian/code-unit-reverse-lookup:1.0.1, phpunit/php-token-stream:3.1.1, phpunit/php-file-iterator:1.4.5, phpunit/php-code-coverage:6.0.5, sebastian/object-reflector:1.1.1, sebastian/object-enumerator:3.0.3, sebastian/global-state:2.0.0, phpunit/php-timer:2.1.2, symfony/polyfill-ctype:v1.17.0, webmozart/assert:1.8.0, phpdocumentor/reflection-common:2.1.0, phpdocumentor/type-resolver:1.1.0, phpdocumentor/reflection-docblock:5.1.0, phpspec/prophecy:v1.10.3, myclabs/deep-copy:1.9.5, sebastian/resource-operations:1.0.0, phar-io/version:1.0.1, phar-io/manifest:1.0.1, phpunit/phpunit:7.1.5, codeception/phpunit-wrapper:7.6.1, symfony/yaml:v4.4.8, behat/gherkin:v4.6.2, symfony/polyfill-mbstring:v1.17.0, symfony/dom-crawler:v4.4.8, symfony/css-selector:v4.4.8, symfony/browser-kit:v4.4.8, symfony/event-dispatcher-contracts:v1.1.7, psr/container:1.0.0, symfony/event-dispatcher:v4.4.8, symfony/service-contracts:v2.0.1, symfony/polyfill-php73:v1.17.0, symfony/console:v4.4.8, symfony/finder:v4.4.8, ralouphie/getallheaders:3.0.3, psr/http-message:1.0.1, guzzlehttp/psr7:1.6.1, guzzlehttp/promises:v1.3.1, symfony/polyfill-php72:v1.17.0, symfony/polyfill-intl-idn:v1.17.0, guzzlehttp/guzzle:6.5.3, symfony/process:v4.4.8, facebook/webdriver:1.7.1, codeception/codeception:2.4.0
[374.8MiB/173.28s] - Installing sebastian/recursion-context (3.0.0): [374.9MiB/173.28s] Loading from cache[374.9MiB/173.28s]
[374.9MiB/173.28s] Extracting archive[374.9MiB/173.51s] - Installing sebastian/exporter (3.1.2): [374.9MiB/173.52s] Loading from cache[374.9MiB/173.52s]
[374.9MiB/173.52s] Extracting archive[374.9MiB/173.75s] - Installing doctrine/instantiator (1.3.0): [374.9MiB/173.76s] Loading from cache[374.9MiB/173.76s]
[374.9MiB/173.76s] Extracting archive[374.9MiB/174.00s] - Installing phpunit/php-text-template (1.2.1): [374.9MiB/174.00s] Loading from cache[374.9MiB/174.00s]
[374.9MiB/174.00s] Extracting archive[374.9MiB/174.23s] - Installing phpunit/phpunit-mock-objects (6.1.2): [374.9MiB/174.23s] Loading from cache[374.9MiB/174.23s]
[374.9MiB/174.23s] Extracting archive[374.9MiB/174.55s] - Installing codeception/stub (1.0.4): [374.9MiB/174.56s] Loading from cache[374.9MiB/174.56s]
[374.9MiB/174.56s] Extracting archive[374.9MiB/174.80s] - Installing sebastian/diff (3.0.2): [374.9MiB/174.80s] Loading from cache[374.9MiB/174.80s]
[374.9MiB/174.80s] Extracting archive[374.9MiB/175.07s] - Installing sebastian/comparator (3.0.2): [374.9MiB/175.07s] Loading from cache[374.9MiB/175.07s]
[374.9MiB/175.07s] Extracting archive[374.9MiB/175.33s] - Installing theseer/tokenizer (1.1.3): [374.9MiB/175.33s] Loading from cache[374.9MiB/175.33s]
The run before
之前的运行
Problem 1
- Installation request for codeception/codeception 2.4.0 -> satisfiable by codeception/codeception[2.4.0].
- Conclusion: remove symfony/finder v5.0.8
- Conclusion: don't install symfony/finder v5.0.8
- codeception/codeception 2.4.0 requires symfony/finder >=2.7 <5.0 -> satisfiable by symfony/finder[v2.7.0, v2.7.1, v2.7.10, v2.7.11, v2.7.12, v2.7.13, v2.7.14, v2.7.15, v2.7.16, v2.7.17, v2.7.18, v2.7.19, v2.7.2, v2.7.20, v2.7.21, v2.7.22, v2.7.23, v2.7.24, v2.7.25, v2.7.26, v2.7.27, v2.7.28, v2.7.29, v2.7.3, v2.7.30, v2.7.31, v2.7.32, v2.7.33, v2.7.34, v2.7.35, v2.7.36, v2.7.37, v2.7.38, v2.7.39, v2.7.4, v2.7.40, v2.7.41, v2.7.42, v2.7.43, v2.7.44, v2.7.45, v2.7.46, v2.7.47, v2.7.48, v2.7.49, v2.7.5, v2.7.50, v2.7.51, v2.7.6, v2.7.7, v2.7.8, v2.7.9, v2.8.0, v2.8.1, v2.8.10, v2.8.11, v2.8.12, v2.8.13, v2.8.14, v2.8.15, v2.8.16, v2.8.17, v2.8.18, v2.8.19, v2.8.2, v2.8.20, v2.8.21, v2.8.22, v2.8.23, v2.8.24, v2.8.25, v2.8.26, v2.8.27, v2.8.28, v2.8.29, v2.8.3, v2.8.30, v2.8.31, v2.8.32, v2.8.33, v2.8.34, v2.8.35, v2.8.36, v2.8.37, v2.8.38, v2.8.39, v2.8.4, v2.8.40, v2.8.41, v2.8.42, v2.8.43, v2.8.44, v2.8.45, v2.8.46, v2.8.47, v2.8.48, v2.8.49, v2.8.5, v2.8.50, v2.8.52, v2.8.6, v2.8.7, v2.8.8, v2.8.9, v3.0.0, v3.0.1, v3.0.2, v3.0.3, v3.0.4, v3.0.5, v3.0.6, v3.0.7, v3.0.8, v3.0.9, v3.1.0, v3.1.1, v3.1.10, v3.1.2, v3.1.3, v3.1.4, v3.1.5, v3.1.6, v3.1.7, v3.1.8, v3.1.9, v3.2.0, v3.2.1, v3.2.10, v3.2.11, v3.2.12, v3.2.13, v3.2.14, v3.2.2, v3.2.3, v3.2.4, v3.2.5, v3.2.6, v3.2.7, v3.2.8, v3.2.9, v3.3.0, v3.3.1, v3.3.10, v3.3.11, v3.3.12, v3.3.13, v3.3.14, v3.3.15, v3.3.16, v3.3.17, v3.3.18, v3.3.2, v3.3.3, v3.3.4, v3.3.5, v3.3.6, v3.3.7, v3.3.8, v3.3.9, v3.4.0, v3.4.1, v3.4.10, v3.4.11, v3.4.12, v3.4.13, v3.4.14, v3.4.15, v3.4.16, v3.4.17, v3.4.18, v3.4.19, v3.4.2, v3.4.20, v3.4.21, v3.4.22, v3.4.23, v3.4.24, v3.4.25, v3.4.26, v3.4.27, v3.4.28, v3.4.29, v3.4.3, v3.4.30, v3.4.31, v3.4.32, v3.4.33, v3.4.34, v3.4.35, v3.4.36, v3.4.37, v3.4.38, v3.4.39, v3.4.4, v3.4.40, v3.4.5, v3.4.6, v3.4.7, v3.4.8, v3.4.9, v4.0.0, v4.0.1, v4.0.10, v4.0.11, v4.0.12, v4.0.13, v4.0.14, v4.0.15, v4.0.2, v4.0.3, v4.0.4, v4.0.5, v4.0.6, v4.0.7, v4.0.8, v4.0.9, v4.1.0, v4.1.1, v4.1.10, v4.1.11, v4.1.12, v4.1.2, v4.1.3, v4.1.4, v4.1.5, v4.1.6, v4.1.7, v4.1.8, v4.1.9, v4.2.0, v4.2.1, v4.2.10, v4.2.11, v4.2.12, v4.2.2, v4.2.3, v4.2.4, v4.2.5, v4.2.6, v4.2.7, v4.2.8, v4.2.9, v4.3.0, v4.3.1, v4.3.10, v4.3.11, v4.3.2, v4.3.3, v4.3.4, v4.3.5, v4.3.6, v4.3.7, v4.3.8, v4.3.9, v4.4.0, v4.4.1, v4.4.2, v4.4.3, v4.4.4, v4.4.5, v4.4.6, v4.4.7, v4.4.8].
- Can only install one of: symfony/finder[v2.7.0, v5.0.8].
- Can only install one of: symfony/finder[v2.7.1, v5.0.8].
- Can only install one of: symfony/finder[v2.7.10, v5.0.8].
- Can only install one of: symfony/finder[v2.7.11, v5.0.8].
回答by gidomanders
If any of the previous answers don't work, check if your firewall allows for TCP_OUT on port 9418.
如果之前的任何答案都不起作用,请检查您的防火墙是否允许端口 9418 上的 TCP_OUT。
My firewall security was too sharp. This caused Composer to take so long, I never got any timeout or indication the port was blocked.
我的防火墙安全性太强了。这导致 Composer 需要很长时间,我从未收到任何超时或端口被阻塞的迹象。