What is it?
Composer is PHP's dependency manager. It downloads third-party libraries (packages) from Packagist, manages their versions, and generates an autoloader so all classes load automatically.
Why does it matter?
Without Composer you would manually download, update, and track every library. Composer does all of this in one command, ensures version compatibility, and is used by every modern PHP project.
Learn Composer — installing packages, composer.json, composer.lock, and essential commands.
Real-World Use Cases
- 📅 Date manipulation - Install nesbot/carbon for a fluent API: Carbon::now()->addDays(7)->diffForHumans() instead of raw timestamp math.
- 🌍 HTTP client - Install guzzlehttp/guzzle for clean, reliable HTTP requests with middleware, retries, and connection pooling.
- ✅ Validation library - Install illuminate/validation (Laravel's validator) standalone for rich, readable validation rules.
- 🔑 JWT authentication - Install firebase/php-jwt to generate and verify JSON Web Tokens for stateless API authentication.
Installing Packages
# Install Composer globally (one-time setup)
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
# Install a package (saved to composer.json)
composer require nesbot/carbon
# Install only for development (not deployed to production)
composer require --dev phpunit/phpunit
Using Installed Packages
format('Y-m-d'); // 2026-04-14
echo $now->addDays(7)->format('D, d M'); // Tue, 21 Apr
echo $now->diffForHumans(Carbon::parse('2026-01-01')); // 3 months ago
Essential Composer Commands
# Install all packages listed in composer.lock (use on server deploy)
composer install
# Update packages to latest allowed versions
composer update
# Remove a package
composer remove nesbot/carbon
# Regenerate optimised autoloader (use on production deploy)
composer dump-autoload -o
Q: What is composer.lock and should it be in git?
composer.lock records the exact version of every installed package. Always commit it for applications — ensures everyone uses identical versions. Do NOT commit it for published libraries.
Comments (0)
No comments yet. Be the first!
Leave a Comment