Composer Command Cheat Sheet

Composer Initialization and Setup

  • Initialize a New Project
    composer init
    Walks you through creating a new composer.json file.

  • Install Dependencies
    composer install
    Installs all dependencies listed in composer.lock. If composer.lock doesn’t exist, it installs from composer.json and creates composer.lock.

Adding and Removing Packages

  • Require a Package
    composer require <vendor/package>
    Installs a new package and adds it to composer.json.

  • Remove a Package
    composer remove <vendor/package>
    Uninstalls a package and removes it from composer.json.

Updating Packages

  • Update All Packages
    composer update
    Updates all packages to the latest versions allowed by composer.json and updates composer.lock.

  • Update a Specific Package
    composer update <vendor/package>
    Updates only the specified package.

Viewing Package Information

  • List Installed Packages
    composer show
    Shows all installed packages and their versions.

  • Show Direct Dependencies
    composer show --direct
    Shows only the packages directly required in composer.json.

  • View Detailed Info on a Package
    composer show <vendor/package>
    Displays detailed information about a specific package.

  • Find Which Package Depends on Another
    composer depends <vendor/package>
    Shows which packages depend on a given package.

Autoloading

  • Generate Autoload Files
    composer dump-autoload
    Rebuilds the autoload files without installing or updating packages.

  • Optimize Autoload
    composer dump-autoload --optimize
    Optimizes autoload files for better performance in production.

Lock File Management

  • Validate Composer Files
    composer validate
    Checks if composer.json and composer.lock are valid and properly formatted.

Miscellaneous Commands

  • Check for Available Package Updates
    composer outdated
    Lists packages with newer versions available.

  • Get Package Suggestions
    composer suggest
    Shows optional packages suggested by installed packages.

  • View Package Funding Information
    composer fund
    Displays funding links for installed packages (e.g., GitHub Sponsors).

  • Clear Composer Cache
    composer clear-cache
    Clears the Composer cache to free up space or fix issues with cached packages.

Using Different Environments

  • Install Without Dev Dependencies
    composer install --no-dev
    Installs only the production dependencies, skipping any require-dev packages.

  • Install With Dev Dependencies (default)
    composer install
    Installs both production and development dependencies.

Top 20 Popular Composer Packages

1. PHPUnit - Testing Framework

  • Package: phpunit/phpunit
  • Description: A popular testing framework for PHP applications.
  • Install: composer require --dev phpunit/phpunit

2. Guzzle - HTTP Client

  • Package: guzzlehttp/guzzle
  • Description: A robust PHP HTTP client for making API requests.
  • Install: composer require guzzlehttp/guzzle

3. Laravel Framework - Full-Stack PHP Framework

  • Package: laravel/framework
  • Description: A powerful MVC PHP framework for web applications.
  • Install: composer require laravel/framework

4. Symfony Console - Console Applications

  • Package: symfony/console
  • Description: A library to build command-line applications.
  • Install: composer require symfony/console

5. Carbon - Date and Time Handling

  • Package: nesbot/carbon
  • Description: A library for working with dates and times in PHP.
  • Install: composer require nesbot/carbon

6. Monolog - Logging Library

  • Package: monolog/monolog
  • Description: A comprehensive logging tool for PHP.
  • Install: composer require monolog/monolog

7. PHPMailer - Email Sending

  • Package: phpmailer/phpmailer
  • Description: A feature-rich email library for sending emails.
  • Install: composer require phpmailer/phpmailer

8. Faker - Data Generation

  • Package: fakerphp/faker
  • Description: Generates fake data for testing and seeding databases.
  • Install: composer require --dev fakerphp/faker

9. Doctrine ORM - Object-Relational Mapping

  • Package: doctrine/orm
  • Description: An ORM for PHP that provides data mapping for SQL.
  • Install: composer require doctrine/orm

10. Predis - Redis Client

  • Package: predis/predis
  • Description: A PHP client for connecting to Redis.
  • Install: composer require predis/predis

11. Intervention Image - Image Processing

  • Package: intervention/image
  • Description: A library for handling images in PHP.
  • Install: composer require intervention/image

12. SwiftMailer - Email Library

  • Package: swiftmailer/swiftmailer
  • Description: Another popular library for sending emails.
  • Install: composer require swiftmailer/swiftmailer

13. Respect Validation - Data Validation

  • Package: respect/validation
  • Description: A library for validating data using a fluent interface.
  • Install: composer require respect/validation

14. Twig - Templating Engine

  • Package: twig/twig
  • Description: A popular templating engine for rendering HTML templates.
  • Install: composer require twig/twig

15. Symfony Dotenv - Environment Variables

  • Package: symfony/dotenv
  • Description: Loads environment variables from a .env file.
  • Install: composer require symfony/dotenv

16. League OAuth2 Client - OAuth2 Authentication

  • Package: league/oauth2-client
  • Description: A client for handling OAuth2 authentication.
  • Install: composer require league/oauth2-client

17. PHP Dotenv - Environment Variables

  • Package: vlucas/phpdotenv
  • Description: A library to load environment variables from a .env file.
  • Install: composer require vlucas/phpdotenv

18. Spatie Permission - Role and Permission Management

  • Package: spatie/laravel-permission
  • Description: A library to manage user roles and permissions in Laravel.
  • Install: composer require spatie/laravel-permission

19. Barryvdh Laravel Debugbar - Debugging

  • Package: barryvdh/laravel-debugbar
  • Description: A debugging tool for Laravel applications.
  • Install: composer require barryvdh/laravel-debugbar --dev

20. PHP Markdown - Markdown Parsing

  • Package: michelf/php-markdown
  • Description: A library for parsing and rendering Markdown.
  • Install: composer require michelf/php-markdown