Composer is the tool that installs and manages libraries for your PHP projects. You tell it which packages your project needs in a small file called composer.json, and it downloads the right versions, sorts out their own dependencies, and gives you one file to include so every class is ready to use. In this guide we will build one tiny project together, a notes app that saves notes and logs each one with Monolog.
The project we will build
Reading about a tool is one thing. Using it on a real project is how it clicks, so this guide hangs on one small app. Our notes app does three simple jobs: it takes a note from the command line, appends it to notes.txt, and writes a log line to notes.log. Instead of writing our own logger, we will borrow Monolog, the logging library the official Composer docs use in their own getting started example.
The plan is short: install Composer, ask it for Monolog, write notes.php, run it twice, and read the files it creates. Nothing here needs a web server, a database, or a framework. If PHP itself is still new to you, our guide to what PHP is, explained simply is a good warm up.
First, get Composer onto your machine
Composer is a single program file, a PHAR archive, that runs with PHP. It needs PHP 7.2.5 or newer, and the current release is Composer 2.11.3. Check your version first:
php -vThen run the four commands from the official Composer download page. The long second line checks the installer against its published SHA-384 hash:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === 'c8c37e2f8c0204c6b378c28a7894181507c6fd0d4b2d84eea3afa2c0d53b5da2443a7b202cc6134ae01e73d1a32b93a4') { echo 'Installer verified'.PHP_EOL; } else { echo 'Installer corrupt'.PHP_EOL; unlink('composer-setup.php'); exit(1); }"
php composer-setup.php
php -r "unlink('composer-setup.php');"The installer creates composer.phar in the current folder. Run it once:
php composer.phar -VExpected output:
Composer version 2.11.3 2026-11-24 10:14:18Typing php composer.phar every time gets old, so most developers move the file onto their PATH and call it plain composer; the official Introduction chapter shows that step for each system. From here on I will write composer.
One command installs Monolog
Make a folder for the project and step inside it. Then ask Composer for the package from Packagist, the main public library of PHP packages. The name is its vendor name and project name joined with a slash:
mkdir notes-app
cd notes-app
composer require monolog/monologComposer prints a short progress report, then a line like this:
Using version ^3.13 for monolog/monologThree new things now sit in your folder. First, composer.json, the shopping list, and the only file you ever edit by hand:
{
"require": {
"monolog/monolog": "^3.13"
}
}The require key lists the packages your project needs. The version ^3.13 means any 3.x release from 3.13 upwards, but never 4.0. Second, composer.lock, the exact receipt, recording the precise version of every package installed, so a teammate who clones your project gets the identical code. Commit it to git. Third, the vendor folder, where the downloaded code lives, including vendor/autoload.php. Include that one file and every class is ready to use.
Composer or the old copy paste way?
Before Composer, PHP developers downloaded library zip files by hand and wrote long require lists at the top of every script. It worked until one library needed another, which needed a third:
| Job | Copy paste by hand | With Composer |
|---|---|---|
| Adding a library | Download the zip, unzip it, guess the folder | One command: composer require vendor/package |
| Library needs another library | You chase the chain yourself | Composer resolves it and installs the whole chain |
| Updating everything | Repeat the whole manual job | One command: composer update |
| Loading the classes | A pile of require lines in every file | One line: require vendor/autoload.php |
Now write the notes script
Create notes.php in the project folder. It uses Monolog through the autoloader, so there is no include path to fight with. If classes and objects still feel new, read our guide to PHP classes and objects explained for beginners first:
<?php
require __DIR__ . '/vendor/autoload.php';
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
$log = new Logger('notes');
$log->pushHandler(new StreamHandler(__DIR__ . '/notes.log', Logger::INFO));
$note = $argv[1] ?? null;
if ($note === null) {
echo "Usage: php notes.php \"Buy milk\"\n";
$log->warning('notes.php ran with no note text');
exit(1);
}
file_put_contents(__DIR__ . '/notes.txt', $note . PHP_EOL, FILE_APPEND);
$log->info('Note saved', ['note' => $note]);
echo "Saved: " . $note . "\n";The first line pulls in the autoloader, which makes Monolog available. The two use lines import the Logger class and the StreamHandler class, which writes log lines to a file. Then we create a logger named notes with a StreamHandler that writes to notes.log. The $argv[1] ?? null line grabs the note text from the command line, falling back to null when the user typed nothing. With no note, it prints a usage hint, logs a warning, and stops with exit code 1. Otherwise it appends the note to notes.txt, logs an info message, and prints a confirmation. That second argument to $log->info() is a small PHP array of extra context, which Monolog prints next to the message.
Run it and watch the log grow
Now run the script twice with two different notes:
php notes.php "Buy milk for chai"
php notes.php "Call Rohan about the PHP project"Expected output:
Saved: Buy milk for chai
Saved: Call Rohan about the PHP projectNow look at the two files the script created: notes.txt holds the notes, one per line, and notes.log holds the diary:
[2026-12-01T05:18:10.409273+00:00] notes.INFO: Note saved {"note":"Buy milk for chai"} []
[2026-12-01T05:18:10.999449+00:00] notes.INFO: Note saved {"note":"Call Rohan about the PHP project"} []Your dates and times will look different, and that is fine. Each line records the moment, the level, the message, and the note as context. That is the whole point of a logger: when something goes wrong at 2 am, the log tells you what the app did, in order. With no note at all, you get the usage hint instead:
php notes.php
Usage: php notes.php "Buy milk"That is a real project working end to end. The official Basic usage chapter in the Composer docs walks the same ground with more detail.
Three commands you will use every week
composer install reads composer.lock and installs the exact versions it lists, so run it after cloning a project. composer update fetches newer versions that still fit your composer.json and writes a fresh lock file. composer show lists what is installed. One extra habit: add vendor to your .gitignore file.
Common mistakes and troubleshooting
Here are the ones beginners hit first, and the fix for each.
Class not found. If PHP says a Monolog class does not exist, the autoloader is missing from your script. Check the first line is require __DIR__ . '/vendor/autoload.php'; and that you run the script from the project folder.
Could not find package. A typo in the package name gives you a blunt error: Could not find package monolog/monlog. Package names are exact, including the vendor part before the slash. Copy the name from Packagist.
Newer package, older PHP. Monolog 3 needs PHP 8.1 or newer. If your server still runs PHP 7.4, Composer will refuse the install. Either upgrade PHP, which is the right fix, or ask Composer for an older package line. Never edit version numbers inside vendor.
Frequently asked questions
Is Composer a package manager like npm?
Close, but not quite. npm installs packages for JavaScript, while Composer manages dependencies per PHP project, in the vendor folder. The idea is the same.
Do I need Composer for every PHP project?
No. A single file script with no outside libraries does not need it. The moment you use a library, a framework, or a package from Packagist, Composer becomes the sensible way.
What is the difference between composer.json and composer.lock?
composer.json is your wish list: the packages and version ranges you accept. composer.lock is the exact receipt: the precise version of every package actually installed. You edit the first one, Composer writes the second one, and you commit both to git.
Should I commit the vendor folder to git?
No. Add vendor to .gitignore. Anyone cloning your project can rebuild it with composer install and your lock file.
Your turn: make the notes app yours
You now have a working project, a real package, and a log file that proves it all works. Give the app one small upgrade today: add a command that lists all saved notes, or one that deletes a line from notes.txt and logs the deletion. Then try a second package: vlucas/phpdotenv lets you store settings in a .env file, and requiring it is the same one command you already ran. And if you are choosing what to build next, our comparison of Laravel, CodeIgniter and Core PHP compared will help you pick between a framework and plain PHP with Composer.
Leave a Comment