Skip to content
Leo edited this page Oct 16, 2025 · 2 revisions

What is Composer?

Composer is PHP’s dependency manager — similar to npm (Node.js), pip (Python), or Maven (Java). It automates the process of:

  • Installing libraries and frameworks your project depends on,
  • Resolving dependencies between those libraries,
  • Autoloading all the necessary classes automatically.

Instead of manually downloading and including PHP libraries, you define them in a file, and Composer handles everything for you.

See https://getcomposer.org/

Installing

https://getcomposer.org/download/

Check successful installation: composer -V

Setup in a project

  1. Go into root directory of project
  2. composer init
    1. Creates composer.json for your project and adds library (for e.g. monolog/monolog for logging)
    2. Adds /vendor to .gitignore
  3. composer install
    1. Installs libraries into /vendor
      1. Adds autoload.php in /vendor directory which can be included in your PHP code
    2. Creates composer.lock which stores the exact libraries that you're using (good for deployment on other environments)

Remove everything created by composer

In order to start anew deleted the following manually:

  • composer.json
  • composer.lock
  • /vendor directory

Common commands

Command Description
composer init Creates a new composer.json interactively
composer install Installs dependencies listed in composer.json
composer update Updates dependencies to their latest allowed versions
composer require vendor/package Adds a new package and installs it
composer remove vendor/package Removes a package
composer dump-autoload Rebuilds the autoloader files manually

Include library in code

<?php
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

require '../vendor/autoload.php';
// Create a logger instance
$log = new Logger('app');
...
?>
Clone this wiki locally