Why is PHP Composer so slow?

元气小坏坏 提交于 2020-02-26 04:26:26

问题


Why is PHP Composer so slow when all I do is init a project with zero dependencies? Here are the commands I run:

composer init

<step through composer.json creation, define 0 zero dependencies>

composer install

Wait 3 minutes (not an exaggeration).

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?

Is there a configuration option I can use to pull in a cached autloader and vendor upon init?


回答1:


Also, disable Xdebug. Xdebug can cause Composer to take minutes even when running a command as simple as composer --version.




回答2:


Because Composer is implemented by file_get_contents(). That has no TCP optimizations, no Keep-Alive, no multiplexing, etc.

I created a Composer plugin to download packages in parallel: https://packagist.org/packages/hirak/prestissimo

$ composer global require hirak/prestissimo

Please try it. In my environment, composer install becomes 10 times faster.




回答3:


  1. Make sure you have the latest version of Composer.
  2. Install in verbose mode by adding -vvv, for example composer global require "squizlabs/php_codesniffer=*" -vvv
  3. 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



回答4:


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.

... 30 minutes later ...

It looks like some performance problem @packagist.org. Now the Composer install runs within 2 seconds! And downloaded JSON files are properly cached.




回答5:


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 IPv6 addressing mode failures. So to test I ran

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 CentOS I created/modified the file /etc/gai.conf and put in the following:

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

precedence ::ffff:0:0/96  100

Source on Rackspace Community Hub




回答6:


On Ubuntu Xenial 16.04 VPS, you need to do the following:

sudo sh -c "echo 'precedence ::ffff:0:0/96 100' >> /etc/gai.conf"
composer global require hirak/prestissimo



回答7:


If any of the previous answers don't work, check if your firewall allows for TCP_OUT on port 9418.

My firewall security was too sharp. This caused Composer to take so long, I never got any timeout or indication the port was blocked.




回答8:


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 self-update

and then require the composer package as sudo worked for me.

sudo composer require "<package-name>"


来源:https://stackoverflow.com/questions/28436237/why-is-php-composer-so-slow

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!