Published on September 27th, 2025
Composer is PHP's dependency manager. Learn how to install it, initialize composer.json
, require popular packages like Monolog and Guzzle, autoload classes, update dependencies, and more.
When working with PHP, one of the most important tools you’ll encounter is Composer. Composer is a dependency manager for PHP, which means it helps you include and manage external libraries (called packages) in your projects. Instead of manually downloading libraries and keeping track of versions, Composer automates the process and ensures your project always uses the right versions.
In this article, we’ll go through the basics of Composer, how to install it, and how to use it to manage packages with practical examples.
Before Composer, PHP developers often had to manually include third-party libraries by downloading .zip
files, extracting them, and adding them into their project. This quickly became messy, especially when libraries depended on other libraries.
Composer solves this problem by:
On most systems, you can install Composer globally by running:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php
mv composer.phar /usr/local/bin/composer
Once installed, check the version:
composer -V
If everything is set up, you should see the Composer version printed in your terminal.
To start using Composer in your project, you need a composer.json
file. This file defines which packages your project requires. You can create it by running:
composer init
The command will ask you a few questions (project name, description, author, etc.) and then generate a composer.json
file.
Example of a basic composer.json
:
{
"name": "yourname/myproject",
"description": "A simple PHP project with Composer",
"require": {}
}
Let’s say you want to use the popular Monolog library for logging. You can install it like this:
composer require monolog/monolog
This command updates your composer.json
and creates a composer.lock
file. Composer will also download the library into a vendor/
directory.
After this, your composer.json
might look like:
{
"require": {
"monolog/monolog": "^3.0"
}
}
Composer provides an autoloader so you don’t have to manually include each library. Just add this line at the start of your script:
<?php
require __DIR__ . '/vendor/autoload.php';
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
// Create a log channel
$log = new Logger('myapp');
$log->pushHandler(new StreamHandler('app.log', Logger::DEBUG));
// Add records to the log
$log->info('This is an info message');
$log->warning('This is a warning');
Composer makes it easy to use packages in your project without worrying about file paths or includes.
Over time, new versions of packages are released. To update your packages, run:
composer update
This will fetch the latest versions allowed by your composer.json
constraints. If you want to update a specific package:
composer update monolog/monolog
To see which packages are installed:
composer show
This gives you a complete list of libraries and their versions.
Another common package is Guzzle, an HTTP client for making API requests.
composer require guzzlehttp/guzzle
Then in PHP:
<?php
require __DIR__ . '/vendor/autoload.php';
use GuzzleHttp\Client;
$client = new Client();
$response = $client->get('https://api.github.com/repos/composer/composer');
$data = json_decode($response->getBody(), true);
echo "Composer repository stars: " . $data['stargazers_count'];
With just a few lines of code, you can make powerful API calls.
Composer is an essential tool for modern PHP development. It saves you from manually managing libraries and ensures your project runs smoothly across environments. With Composer, you can:
If you’re building PHP projects today, learning Composer is a must. Start experimenting with small packages like Monolog or Guzzle, and you’ll quickly see how much time it saves you.
Besides installing external libraries, Composer can also autoload your own classes. This is especially useful when building larger applications where you want to organize your code into namespaces instead of relying on endless require
or include
statements.
The most common standard is PSR-4 autoloading. It maps namespaces to directories so Composer knows where to find your classes.
Let’s say your project structure looks like this:
myproject/
├── src/
│ └── App/
│ └── Hello.php
├── vendor/
├── composer.json
└── index.php
Inside src/App/Hello.php
, you might have:
<?php
namespace App;
class Hello
{
public function sayHi()
{
return "Hello from my custom class!";
}
}
Now, update your composer.json
to tell Composer about this namespace:
{
"require": {
"monolog/monolog": "^3.0"
},
"autoload": {
"psr-4": {
"App\\": "src/App/"
}
}
}
Then run:
composer dump-autoload
This regenerates the autoload files so Composer knows how to find your classes.
Finally, in index.php
:
<?php
require __DIR__ . '/vendor/autoload.php';
use App\Hello;
$hello = new Hello();
echo $hello->sayHi();
And just like that, your class is automatically loaded without manual includes.
Sometimes you want packages that are useful only during development (like PHPUnit for testing). Composer allows you to install dev dependencies with the --dev
flag:
composer require --dev phpunit/phpunit
These packages won’t be installed in production if you run:
composer install --no-dev
This helps keep your production environment clean.
Composer can also install tools globally so you can use them across multiple projects. For example:
composer global require laravel/installer
Make sure ~/.composer/vendor/bin
(or ~/.config/composer/vendor/bin
on some systems) is in your PATH
, and you can then run:
laravel new myapp
When you install packages, Composer generates a composer.lock
file. This file locks the exact versions of your dependencies.
Developers working on the same project should always commit composer.lock
to version control.
When someone else clones your project and runs composer install
, they will get the same versions you tested with, avoiding “works on my machine” problems.
--dev
for tools like PHPUnit.composer.lock
file for consistent environments.Composer is much more than a package installer — it’s a complete dependency and project management tool. Whether you’re adding powerful third-party libraries like Monolog or Guzzle, or autoloading your own classes with PSR-4, Composer helps you keep your projects clean, organized, and professional.
If you’re serious about PHP development, mastering Composer is a must. Start small, play with autoloading, and soon you’ll wonder how you ever coded without it.
Looking for a skilled PHP developer to bring your project to life? I specialize in creating robust and efficient PHP solutions tailored to your needs.
Contact me