Composer Commands Documentation
Composer is a dependency manager for PHP that allows you to declare and manage the libraries your project depends on. This document outlines common Composer commands and their usage scenarios.
Installation Commands
Basic Installation
composer installUse this command when:
- Setting up a project for the first time
- After cloning a repository
- When the
vendordirectory is missing - When encountering
500 Internal Server Errordue to missing packages
This command reads the composer.json and composer.lock files and installs all dependencies as specified in the lock file.
Production Installation
composer install --no-dev --optimize-autoloaderUse this command in production environments to:
- Install only production dependencies (skips development dependencies)
- Generate an optimized autoloader for better performance
Update Commands
Update All Packages
composer updateUse this command when:
- You want to update all dependencies to their latest versions according to the version constraints in
composer.json - After adding new modules programmatically or through a web interface
- After manually uploading packages that need to be recognized by the system
Update Specific Package
composer update vendor/packageUse this command to update a specific package while keeping all other dependencies at their current versions.
Autoloader Commands
Basic Autoloader Regeneration
composer dump-autoloadUse this command when:
- You encounter
500 Internal Server Errordue to autoloading issues - After adding new classes that need to be autoloaded
- After making changes to the PSR-4 or classmap autoloading in your project
Optimized Autoloader Regeneration
composer dump-autoload --optimizeUse this command in production environments to:
- Generate a more efficient autoloader by converting PSR-0/PSR-4 autoloading to classmap
- Improve application performance by reducing file lookups
Troubleshooting
If you encounter a 500 Internal Server Error or your site crashes, follow these steps:
- Check if the
vendordirectory exists in your project root - If the
vendordirectory is missing, runcomposer install - If the
vendordirectory exists but issues persist, runcomposer dump-autoload - For production environments, use the optimized versions of commands:
composer install --no-dev --optimize-autoloadercomposer dump-autoload --optimize
Best Practices
- Always commit your
composer.jsonandcomposer.lockfiles to version control - Do not commit the
vendordirectory to version control - Use
composer installwhen deploying to ensure consistency across environments - Use
composer updatesparingly and with caution in production environments - Run
composer dump-autoload --optimizeafter deployment to production for better performance