How to install, remove and update one library using PHP composer for an existing project
You have a existing PHP project and you have installed lots of libraries using composer. There are composer.json and composer.lock already.
use phpunit library as example
1. To install new one (--dev flag is used to put this library in require-dev section of compose.json)
composer require --dev phpunit/phpunit ^8
2. To remove old one
compose remove phpunit/phpunit
3. To update one
//without depency update
composer require --dev phpunit/phpunit ^9
We can check phpunit version
// go to vendor/bin
./phpunit --version
//with depency update
./composer.phar -W require guzzlehttp/guzzle 7.4.5
4. To install or update for all libraries
Davey Shafik has a good explanation. Composer: It’s All About the Lock File
Version in compose.json
Tilde Version Range (~) - ~1.2.3 is equivalent to >=1.2.3 <1.3.0
Caret Version Range (^) - ^1.2.3 is equivalent to >=1.2.3 <2.0.0
Search packages
Composer detected issues in your platform
Once I got Composer detected issues in your platform: Your Composer dependencies require a PHP version ">= 7.3.0". You are running 7.2.24-0ubuntu0.18.04.9.
some libraries have platform requirement. In the vendor composer directory, there is a file called platform_check.php. This file will be updated when you install some libraies.
use this flag --ignore-platform-reqs when do compose install to avoid update that file. Also, composer will not check php version when install. Be sure you know what you are doing.
composer install --ignore-platform-reqs
./composer.phar require --ignore-platform-reqs -W google/cloud ^0.257
Another option is set a flag in the compose.json. Then that file will not be generated.
"config": {
"platform": {
"php": "8.0.22"
},
"platform-check": false
},
Clear composer cache
After a new libray is installed using composer, system seems not find the library. It may be because of cache. Can clear cache by the command below
composer clearcache
See composer tree
composer show --tree
dump-autoload
If new classess are added to classmap package, need to run the command below to update autoload. It will update vendor/composer/autoload_classmap.php
composer dump-autoload
A use case is with PHP Propel 2. In compose.json, we need to add classmap directory.
autoload": {
"classmap": ["src/lib/Propel/generated-classes/"]}
After generated class using propel model:build, we need to run composer dump-autoload. Otherwise, may not be able to auto load these generated classes.
Propel class inherit debug for class not found
step one: om
step two: convert-conf
Lock file prevent our project from updating library
I want to update aws sdk for PHP to the latest version. Because there is a lock file, I can not remove the old version of aws sdk for PHP. If I remove the lock file first and do a composer install,I can install the latest version of aws PHP sdk. However, some functions of our project do not work. The following is how I solved this problem.
- Find root cause is that we need "revenuewire/dynamodb-orm": "2.0.5" to make our project work
- Currently the compose.json has this: "revenuewire/dynamodb-orm": "^2.0". If install without the lock file, it will install revenuewire/dynamodb-orm": "2.0.6" and will break the project
- Modify compose.json. Change "revenuewire/dynamodb-orm": "^2.0" to "revenuewire/dynamodb-orm": "2.0.5". That means fix the version for revenuewire/dynamodb-orm
- remove the compose.lock
- compose install. Everything are working and aws PHP sdk is upgraded.