PHP • Composer

Published on September 27th, 2025

Composer for Packages: A Complete Guide for PHP Developers

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.

Contact me

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.

Why Composer?

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:

  • Automatically downloading and installing packages.
  • Managing versions so your project doesn’t break.
  • Handling dependencies of dependencies.
  • Providing autoloading for your libraries.

Installing Composer

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.

Initializing Composer in a Project

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": {}
}

Installing Packages

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"
    }
}

Using Installed Packages

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.

Updating Packages

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

Listing Installed Packages

To see which packages are installed:

composer show

This gives you a complete list of libraries and their versions.

Example: Using Guzzle for HTTP Requests

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.

Conclusion

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:

  • Quickly install new libraries.
  • Automatically manage dependencies.
  • Keep your codebase clean and maintainable.

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.

Advanced Composer Tips: Autoloading Your Own Code

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.

Using PSR-4 Autoloading

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.

Development Dependencies

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.

Global Packages

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

Lock File and Consistency

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.

Summary of Advanced Usage

  • Use PSR-4 autoloading to manage your own project classes.
  • Add dev dependencies with --dev for tools like PHPUnit.
  • Install tools globally if you use them across projects.
  • Commit your composer.lock file for consistent environments.

Final Thoughts

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.