Composer for PHP: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is Composer and why is it needed?



Composer is a dependency manager for the PHP programming language. If you have worked with npm for JavaScript or pip for Python, then the principle of Composer will be familiar to you. It allows you to declare libraries that your project depends on and automatically manage their installation, updating, and removal.



Why is Composer needed? In modern PHP development, people rarely write everything from scratch. Usually, ready-made libraries are used: for working with databases (Doctrine, Eloquent), HTTP requests (Guzzle), template engines (Twig), logging (Monolog), etc. Previously, developers downloaded ZIP archives, manually copied files, and included them via require. This was inconvenient, made version management difficult, and updating dependencies was a hassle. Composer solves all these problems:



  • Automatic loading (autoloading): Composer generates an autoload file that automatically includes all the necessary classes. You don't need to write dozens of require_once statements.
  • Version management: You specify exact library versions (or ranges), and Composer ensures they do not conflict with each other.
  • Centralized repository: All libraries are stored on Packagist.org — the official Composer repository.
  • Reproducibility: The composer.lock file records the exact versions of all installed packages, guaranteeing the same environment on all machines (development, testing, production).


Installing Composer



Installation on Linux / macOS

The easiest way is to use the official installation script. Open a terminal and run:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"php -r "if (hash_file('sha384', 'composer-setup.php') === 'dac665fdc30fdd8ec78b38b9800061b4150413ff2e3b6f88543c636f7cd84f6db9189d43a81e5503cda447da73c7e5b6') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"php composer-setup.phpphp -r "unlink('composer-setup.php');"

After this, you will get the composer.phar file. To make the composer command global, move it:

sudo mv composer.phar /usr/local/bin/composer


Installation on Windows

Download and run the installer from the official website. It will automatically add Composer to the system PATH variable.



Verifying the installation

After installation, run in the terminal:

composer --version

You should see something like: Composer version 2.7.1 2024-03-11 16:15:07.



Quick start with a code example



Let's create a new project and install a library for working with dates — nesbot/carbon.



  1. Create a project directory:
    mkdir my_projectcd my_project
  2. Initialize Composer:
    composer init

    You will be asked to enter the project name, description, author, etc. You can simply press Enter to accept the default values. This will create a composer.json file.

  3. Install the Carbon library:
    composer require nesbot/carbon

    Composer will download the library and all its dependencies into the vendor/ folder, and will also create a composer.lock file and update composer.json.

  4. Create an index.php file:
    <?phprequire_once __DIR__ . '/vendor/autoload.php';

    use Carbon\\Carbon;

    echo

Recommendations