Composer Overview
Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you. Composer is strongly inspired by Node.js's npm and Ruby's bundler.
Key Features
- Dependency Resolution: Automatically calculates dependencies
- Package Installation: Installs packages from Packagist and other repositories
- Autoload Generation: Creates autoloader for installed packages
- Version Constraints: Flexible version requirements
- Script Execution: Run custom scripts during lifecycle events
- Platform Packages: Handles PHP extensions and system libraries
- Global Installation: Install CLI tools globally
Installation & Setup
Installation Steps:
- Download Composer from official website
- Install PHP (Composer requires PHP 5.3.2+ to run)
- Run the installer (Windows) or move to /usr/local/bin (Linux/macOS)
- Verify installation:
composer --version - Initialize a new project:
composer init
Common Commands:
composer install
Install project dependencies
composer require
Add a dependency to composer.json
composer update
Update dependencies to latest versions
composer dump-autoload
Regenerate autoloader
Productivity Tips
Version Constraints
Use precise version constraints (^, ~) to control updates.
Private Repositories
Configure private repositories for proprietary packages.
Scripts
Use Composer scripts to automate common tasks.
Global Packages
Install frequently used tools globally with composer global require.