-
Notifications
You must be signed in to change notification settings - Fork 0
04 Composer
Leo edited this page Oct 16, 2025
·
2 revisions
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.
https://getcomposer.org/download/
Check successful installation: composer -V
- Go into root directory of project
-
composer init
- Creates composer.json for your project and adds library (for e.g. monolog/monolog for logging)
- Adds /vendor to .gitignore
-
composer install
- Installs libraries into /vendor
- Adds autoload.php in /vendor directory which can be included in your PHP code
- Creates composer.lock which stores the exact libraries that you're using (good for deployment on other environments)
- Installs libraries into /vendor
In order to start anew deleted the following manually:
- composer.json
- composer.lock
- /vendor directory
| 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 |
<?php
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
require '../vendor/autoload.php';
// Create a logger instance
$log = new Logger('app');
...
?>