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 install
Use this command when:
- Setting up a project for the first time
- After cloning a repository
- When the
vendor
directory is missing - When encountering
500 Internal Server Error
due 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-autoloader
Use 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 update
Use 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/package
Use this command to update a specific package while keeping all other dependencies at their current versions.
Autoloader Commands
Basic Autoloader Regeneration
composer dump-autoload
Use this command when:
- You encounter
500 Internal Server Error
due 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 --optimize
Use 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
vendor
directory exists in your project root - If the
vendor
directory is missing, runcomposer install
- If the
vendor
directory exists but issues persist, runcomposer dump-autoload
- For production environments, use the optimized versions of commands:
composer install --no-dev --optimize-autoloader
composer dump-autoload --optimize
Best Practices
- Always commit your
composer.json
andcomposer.lock
files to version control - Do not commit the
vendor
directory to version control - Use
composer install
when deploying to ensure consistency across environments - Use
composer update
sparingly and with caution in production environments - Run
composer dump-autoload --optimize
after deployment to production for better performance