How to Speed Up PHP Composer

Every PHP developer depends on Composer. Being the one and only dependency manager in PHP community sometime brings some performance issues.

Slowness is my biggest headache in this case. I know, this might caused by multiple factor, but I assume the problem is in my internet connection.

I have an OpenVPN server located in Singapore, and when I use the VPN, the Composer run fast. If I switch off the VPN, it takes forever.

After searching around, I found a good solution to speed up the Composer.

Use 144.217.203.53 as your packagist.org’s IP address

Packagist (packagist.org) using two IP addresses 87.98.253.214 (located in Belgium) and 144.217.203.53 (located in Canada).

In Indonesia, packagist.org will be resolved to 87.98.253.214.

I tried to compare these two IP addresses using MTR.

tracing node to 87.98.253.214
tracing node to 144.217.203.53

From those mtr results, we could see that tracing to 144.217.203.53 is less hop than to 87.98.253.214.

I tried 144.217.203.53 by forcing resolver to this IP address by editing /etc/hosts (I’m on OSX).

144.217.203.53 packagist.org

After confirming that the packagist.org is resolved to 144.217.203.53 using ping, I use composer update --profile to see if it makes changes.

I use this composer.json for the test.

{
"name": "matriphe/test",
"description": "Composer test",
"type": "project",
"require": {
"monolog/monolog": "^1.23",
"doctrine/instantiator": "^1.1",
"symfony/console": "^4.0",
"symfony/debug": "^4.0",
"doctrine/annotations": "^1.6",
"doctrine/dbal": "^2.7",
"doctrine/inflector": "^1.3",
"guzzlehttp/guzzle": "^6.3",
"sebastian/exporter": "^3.1",
"symfony/process": "^4.0",
"doctrine/cache": "^1.7",
"symfony/routing": "^4.0",
"symfony/var-dumper": "^4.0",
"nesbot/carbon": "^1.26"
},
"require-dev": {
"phpunit/phpunit": "^7.1",
"fzaninotto/faker": "^1.7"
},
"license": "MIT",
"authors": [
{
"name": "Muhammad Zamroni",
"email": "halo@matriphe.com"
}
],
"config": {
"sort-packages": true
}
}

Here’s the result. Using 87.98.253.214 took 205.68 seconds and using 144.217.203.53 took only 42.5 seconds! It’s almost 5 times faster!

If you feel your composer is very slow, try to use 144.217.203.53 as your packagist.org IP address.

This result, of course depends on where you are and the internet connection quality, but for my case, it really helped.

Updated: Use Prestissimo

I got this package from Nazieb’s response to this post. I tried the package and amazed with the result.

Using Prestissimo, it only took 7.81 seconds!

Of course, combining these two methods will speed up Composer!

Like what you read? Give Muhammad Zamroni a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.