brasimon
brasimon

Reputation: 779

Faster composer install

A composer install normally takes a few minutes. And on a production environment it's feels too slow. Is it possible to make a composer install to a temp directory and then switch it? If that is possible the downtime should be about zero.

Or are there any other way to do a composer install faster?

Upvotes: 17

Views: 7339

Answers (3)

axiac
axiac

Reputation: 72226

You are asking two different and unrelated things.

Yes, it is a solution to build the next version of your site in a separate directory then put it in place after you moved the old version out of the way. It is, actually, the best solution.

This is how the deployment scripts I build work:

  1. prepare the next version of the site in a separate directory (let's say /var/www/new); the following list of items and their order is not static, some projects need a different flow:
    • get the last version of the code from the repo;
    • remove the files that are not needed on the live site (.gitignore, IDE project files, placeholders etc);
    • run composer install;
    • copy/generate the configuration files containing the settings for the live servers (the ones stored in the repository contain dummy values);
    • change the user and permissions of all files; make some directories writable by the web server;
    • create symlinks, directories etc; for example, the directory that contains the user uploaded files is somewhere outside the server directory and a symlink to it is created during deploy to make its content available through the web server;
  2. move the live code out of the way; I use mv to move the entire directory (/var/www/html to /var/www/old, for example);
  3. move the prepared new version to the proper place (mv /var/www/new /var/www/html);
  4. move the previous version into the archive (after I remove the content of vendor and other files that do not change or are external).

The advantages:

  • the downtime is zero (microseconds, probably, between steps #2 and #3);
  • a failed build doesn't affect the live site (if handled properly);
  • reverting to the previous version (if needed) can be done easily.

Regarding the other question, the only way I know to speed composer up is to avoid running it using a PHP that loads the xdebug extension. The xdebug extension shouldn't be loaded on the production server anyway.

Upvotes: 2

hiraku
hiraku

Reputation: 4257

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 become 10 times faster.

Upvotes: 20

Jeremy Kendall
Jeremy Kendall

Reputation: 2869

You can sometimes speed up composer install significantly by using the --prefer-dist flag, which just happens to be recommended for production use:

--prefer-dist: Reverse of --prefer-source, composer will install from dist if possible. This can speed up installs substantially on build servers and other use cases where you typically do not run updates of the vendors.

composer install docs here: http://getcomposer.org/doc/03-cli.md#install

Edited To Clarify Sometimes

I say it sometimes speeds up composer install because there are plenty of factors that go into it feeling slow, not the least of which are network performance and the current Github status. A slow install can be really frustrating, but it's not always b/c of Composer.

Upvotes: 4

Related Questions