PHP Development

Getting Started with Composer for PHP Projects

Learn how to install Composer, initialize a PHP project, add packages, and understand the files Composer creates.

What is Composer?

Composer is the dependency manager used by modern PHP projects. It installs third-party libraries, resolves compatible versions, and generates an autoloader so your application can load classes without manually including every file.

Check the installation

php -v
composer --version

If Composer is not installed, follow the official installation instructions for your operating system. Make sure Composer uses the same PHP executable and extensions that your project requires.

Initialize a project

From your project directory, run:

composer init

Composer creates a composer.json file. This file describes the project and its dependencies.

Install a package

For example, to add a library:

composer require vendor/package

Composer downloads the package into vendor/ and updates composer.json and composer.lock.

Load installed classes

Include the generated autoloader once in your application bootstrap:

require __DIR__ . '/vendor/autoload.php';

Important project files

  • composer.json: dependency requirements and project metadata
  • composer.lock: exact dependency versions selected for this project
  • vendor/: installed packages and generated autoload files

Commit composer.json and composer.lock to version control. Do not commit the vendor/ directory in most application projects; install dependencies during deployment with Composer.