Keywords: Composer | Package Management | Version Viewing | PHP Dependencies | Development Tools
Abstract: This article provides a comprehensive guide on various methods to view installed packages and their versions in Composer, with detailed analysis of the composer show command usage and parameter options. Through practical case studies, it demonstrates how to quickly obtain package version information in local development environments, resolve dependency conflicts, and explores advanced usage and best practices of related commands.
Overview of Composer Package Management
As the most crucial dependency management tool in the PHP ecosystem, Composer's package version management capabilities are essential for project maintenance. In practical development, developers frequently need to quickly view all installed packages and their specific version information in the current environment, especially when dealing with dependency conflicts or environment migration.
Core Command Analysis
The most direct and effective method to view installed packages and their version information is using the composer show command. This command displays all installed packages and their version information in the current project by default. In earlier versions of Composer, composer show -i (short for --installed) could achieve the same result, but this option has been marked as deprecated in the latest versions.
The basic syntax of the command is as follows:
composer show
After executing this command, Composer outputs information in a format similar to:
symfony/symfony v2.1.0 The Symfony PHP framework
doctrine/orm v2.2.3 Doctrine ORM
twig/extensions v1.0.0 Common additional features for Twig
Global Package Management
For globally installed packages, use the composer global show command. This is particularly useful when managing tool packages shared across projects, such as code formatters, static analysis tools, etc.
Global package viewing command:
composer global show
Command Parameter Details
The composer show command supports various parameters to meet different viewing requirements:
--installedor-i: Show installed packages (default behavior, deprecated)--latestor-l: Show installed packages and their latest available versions--outdatedor-o: Show only packages with available updates--director-D: Show only directly dependent packages--treeor-t: Display dependency relationships in a tree structure
Practical Application Scenarios
Consider a typical development scenario: A developer uses Symfony 2.1 for development in a local environment, and the project runs normally. When deploying the project to a server, dependency conflicts occur due to changes in package versions. At this point, the developer needs to quickly obtain accurate version information for all packages in the local environment.
By executing the composer show command, developers can:
- Obtain a complete list of all installed packages and their versions
- Identify package versions that may cause conflicts
- Reproduce the same dependency configuration in the server environment
- Generate accurate version constraints for
composer.json
Output Format Customization
Composer provides flexible output to meet different needs. Use the --format parameter to specify the output format:
composer show --format=json
JSON format output facilitates script processing and automation tool integration, particularly suitable for version checks in CI/CD pipelines.
Version Comparison and Update Checking
To check which packages have available updates, use:
composer show -o
This command displays results with color coding: green indicates the package is at the latest version, yellow indicates backward-incompatible updates are available, and red indicates backward-compatible updates are available.
Detailed Package Information Viewing
For specific packages, more detailed information can be viewed:
composer show symfony/symfony
This displays complete package information, including description, keywords, license, homepage, source repository, etc.
Platform Package Management
Composer can also manage platform packages (PHP and its extensions):
composer show -p
This is particularly useful for verifying whether the server environment meets project requirements.
Best Practice Recommendations
In daily development, it is recommended to:
- Regularly run
composer show -oto check for available updates - Use
composer showto verify environment consistency before deployment - Use
composer show --directto focus on project direct dependencies - Integrate version checking steps in CI/CD processes
Common Problem Resolution
When encountering dependency conflicts, the composer show command combined with other Composer commands can provide complete solutions:
- Use
composer showto obtain current versions - Use
composer dependsto analyze dependency relationships - Use
composer why-notto identify conflict causes - Adjust version constraints in
composer.json
By mastering the composer show command and its related parameters, developers can more effectively manage project dependencies, ensure development environment consistency, and quickly resolve dependency conflict issues.