Composer
Learn what Composer is, how to install packages from Packagist, and how composer.json, composer.lock, and the vendor folder work together.
Introduction
In the previous lesson, PHPMailer showed up with a single line — require 'vendor/autoload.php' — as if the library had simply appeared in the project. That "appearing" is the job of Composer, PHP's standard tool for managing external packages.
If you have used npm with JavaScript or pip with Python, Composer will feel familiar: it downloads the libraries your project depends on, tracks their exact versions, and gives every project a consistent, repeatable way to install its dependencies.
- What Composer is and the problem it solves.
- How to install Composer.
- How to initialize a project with composer init or a composer.json file.
- How to require a package with composer require.
- What the vendor folder and composer.lock file are for.
- What Packagist is, and how it relates to npm or PyPI.
What Is Composer?
Composer is PHP's standard dependency (package) manager. Instead of manually downloading a library's source files and copying them into your project, you describe what your project needs in a file called composer.json, and Composer downloads, tracks, and organizes those packages for you.
Manages Dependencies
Downloads and organizes third-party PHP libraries your project relies on.
Driven by composer.json
A single file describes exactly which packages, and which versions, your project needs.
Reproducible Installs
composer.lock pins exact versions so every developer and server installs the identical dependency set.
Enables Autoloading
Composer generates the autoloader that makes every installed class available with one require statement (covered in the next lesson).
Installing Composer
Composer is installed once per machine (not per project) and then used from the command line inside any PHP project. The official installer is available at getcomposer.org, with instructions for Windows, macOS, and Linux.
composer --versionComposer version 2.7.1 2024-02-09 15:26:28Composer itself is installed globally on your machine. After that, every individual project simply has its own composer.json describing what that particular project needs.
composer.json and composer init
Every Composer-managed project has a composer.json file at its root describing the project and its dependencies. You can create one interactively with composer init, which asks a series of questions, or write it by hand.
composer init{
"name": "programinds/demo-app",
"description": "A simple demo PHP project",
"require": {
"phpmailer/phpmailer": "^6.9"
}
}The "require" section lists each package and a version constraint. The ^6.9 constraint means "version 6.9 or a later compatible version, but not the next major version," which keeps updates safe from breaking changes.
Requiring a Package
Rather than editing composer.json by hand, the composer require command adds a package and installs it in one step, updating composer.json automatically.
composer require phpmailer/phpmailerUsing version ^6.9 for phpmailer/phpmailer
./composer.json has been updated
Running composer update phpmailer/phpmailer
Loading composer repositories with package information
Updating dependencies
Installing phpmailer/phpmailer (v6.9.1): Extracting archive
Generating autoload filesAfter this command finishes, the package's source code is available inside your project, and it can be loaded through Composer's autoloader — the exact mechanism the next lesson explains in depth.
The vendor Folder and composer.lock
Running composer require or composer install creates a vendor/ folder containing the actual downloaded source code of every dependency, plus Composer's autoloading files. Alongside it, Composer writes (or updates) a composer.lock file recording the exact version of every package that was installed.
| File / Folder | Purpose |
|---|---|
| composer.json | Describes the project and its dependency constraints (what versions are acceptable). |
| composer.lock | Records the exact versions actually installed, so installs are reproducible. |
| vendor/ | Contains the downloaded source code of every dependency, plus the autoloader. |
The vendor/ folder is normally excluded from version control (via .gitignore), since it can be fully regenerated from composer.json and composer.lock by running composer install. The lock file, on the other hand, is committed — it is what guarantees everyone gets identical dependency versions.
composer installPackagist
When you run composer require phpmailer/phpmailer, Composer looks up that package on Packagist (packagist.org), the main public package repository for PHP. Packagist is to PHP what npm is to JavaScript or PyPI is to Python — a searchable index of open-source packages anyone can publish to and install from.
Searchable
Packagist.org lets you search for packages by name or keyword before requiring them.
Versioned
Every package on Packagist publishes tagged versions that Composer's constraints (like ^6.9) resolve against.
Community-Driven
Anyone can publish an open-source package to Packagist, similar to publishing to npm or PyPI.
Common Mistakes
- Committing the vendor/ folder to version control instead of just composer.json and composer.lock.
- Deleting composer.lock, which throws away the guarantee that every environment installs the same exact versions.
- Editing files inside vendor/ directly — any changes are lost the next time dependencies are reinstalled.
- Forgetting to run composer install after cloning a project that already has a composer.json.
- Confusing composer require (add a new dependency) with composer install (install what is already listed).
Best Practices
- Commit composer.json and composer.lock to version control; exclude vendor/ via .gitignore.
- Run composer install after cloning a project to recreate its exact dependency set.
- Use composer require to add new packages rather than hand-editing composer.json when possible.
- Check a package's page on Packagist before adding it, to confirm it is maintained and widely used.
- Keep dependencies reasonably up to date with composer update, reviewed deliberately rather than blindly.
Frequently Asked Questions
Is Composer the same as PHP itself?
No. Composer is a separate command-line tool you install alongside PHP. It manages project dependencies; it does not run or compile PHP code itself.
What is the difference between composer require and composer install?
composer require adds a new package to composer.json and installs it. composer install installs the exact dependencies already listed in an existing composer.json and composer.lock, without adding anything new.
Why should I commit composer.lock but not vendor/?
composer.lock is small text describing exact versions, and it guarantees everyone gets identical dependencies. vendor/ is large, fully regenerable from that lock file, and does not belong in version control.
Is Packagist the only place Composer can install packages from?
Packagist is the default and most common source, but Composer can also install packages directly from private repositories or version control URLs when configured to do so.
Key Takeaways
- Composer is PHP's standard dependency manager, installed once per machine.
- composer.json describes a project and the packages it depends on.
- composer require adds and installs a package; composer install reproduces an existing dependency set.
- The vendor/ folder holds installed package source code; composer.lock pins exact versions for reproducibility.
- Packagist is PHP's public package repository, comparable to npm or PyPI.
Summary
Composer gives PHP projects a standard, reliable way to pull in third-party code: describe what you need in composer.json, install it with composer require, and let composer.lock guarantee everyone gets the same versions.
Installing a package is only half the story — the next lesson explains autoloading, the mechanism that makes every installed class available in your code without a mountain of manual require statements.
- You understand what Composer is and the problem it solves.
- You can initialize a project and require a package from the command line.
- You understand the roles of composer.json, composer.lock, and the vendor folder.
- You know what Packagist is and how it compares to npm or PyPI.