Web Development

Composer

Composer is the PHP dependency manager that downloads the libraries a project needs, chooses versions that are compatible with each other and generates the automatic loading of their classes.

In this post

Composer is the tool PHP developers use so they do not have to write everything from scratch. You declare which libraries a project needs, and Composer downloads them, checks that they are compatible with each other and leaves everything ready to use. In practice, it is the app store for PHP libraries.

Although PHP was born without it, today it shows up almost everywhere. Laravel, Symfony, Drupal and Magento rely on Composer, many WordPress extensions include it to load their libraries, and whenever the conversation turns to dependency security, vulnerabilities in libraries or supply chain attacks, this dependency manager sits right at the centre of it.

Composer is the warehouse manager of a PHP project. It knows which parts are needed, in which version and where to put them, and it never leaves one behind.

What is Composer?

Composer is a dependency manager for the PHP language. A dependency is an external library that the project needs in order to work, such as a tool for sending emails, generating PDFs or talking to an API. Before Composer, each developer downloaded those libraries by hand and copied them into their project, with the risk of lost versions and outdated code.

With Composer, the project declares what it needs in a configuration file. The manager checks a catalogue, usually Packagist, which is the main repository, downloads the libraries and the ones they in turn need, and resolves the versions so that they do not clash. The official Composer guide presents exactly that idea of declaring what is needed and letting the tool manage it.

It runs from the command line and works inside each project, not at the level of the whole system. That way two projects on the same server can use different versions of the same library without interfering with each other. Over the years, the catalogue has grown to host hundreds of thousands of packages, from tiny libraries to complete frameworks, and almost every modern PHP project depends on several of them.

Packagist, the PHP package catalogue

Packagist is the public repository from which Composer gets packages by default. A package is nothing more than a code repository with a composer.json file at its root. The author registers it once on Packagist and, from then on, every version tag they publish in their repository appears as an installable version. Companies that need private packages use paid services or run their own catalogue with tools such as Satis. Composer and Packagist were released together in March 2012, and version 2, from October 2020, greatly reduced installation time and memory usage.

Requirements and ways to install it

Composer is written in PHP and distributed as a single executable file, composer.phar. Version 2 requires PHP 7.2.5 or higher. It can be installed inside a specific project or globally on the system, which is the usual approach, and on Windows there is also a graphical installer. Once installed, it updates itself with the composer self-update command.

composer.json and composer.lock, the project files

composer.json

The composer.json file is where the developer declares what is needed. It lists the libraries and, for each one, a range of acceptable versions. It can also state the minimum PHP version, scripts to run and the autoload configuration.

composer.lock

The composer.lock file is the one the tool generates when installing. It records the exact versions that were installed of each library and of its dependencies, even the indirect ones. Thanks to it, two people or two servers installing the same project get exactly the same code.

That is why both files are kept in version control. The first expresses the intention, the second the concrete reality of an installation.

How Composer works

The difference between composer install and composer update

Composer has two core operations, install and update. Install reads the lock file and downloads exactly those versions. It is what should be run on a server, because it guarantees repeatable results. Update looks for the most recent versions allowed by the configuration file and rewrites the lock. It is done in a controlled way, testing before going live.

Version constraints and semantic versioning

The version ranges used in Composer follow semantic versioning. Requiring a version with the caret symbol accepts compatible updates, with no breaking changes, and sets a minimum. It is a balance between receiving fixes and avoiding surprises. The tilde works in a similar but stricter way, and ~1.2 accepts any 1.x version from 1.2 onwards, but not 2.0. An exact version, such as 1.2.3, freezes the library, and the asterisk accepts any version, which is risky. Composer also distinguishes the stability of each version, and by default it only installs stable versions, never development builds or release candidates.

The vendor folder

Everything downloaded is stored in the project’s vendor folder, together with the autoloader. That folder is not edited by hand or committed to version control, because Composer can regenerate it at any time from the two files. Its size can be surprising. A simple library sometimes pulls in dozens of dependencies, and the folder of a medium-sized project can take up hundreds of megabytes. That is why it is not versioned and is rebuilt on every deployment.

How Composer installs and rebuilds the vendor libraries

Autoloading with PSR-4

Composer generates a file that, once included, lets you use any class from the libraries without loading it manually. The most widespread standard is PSR-4, which maps a namespace prefix to a folder. The PSR-4 specification from the PHP-FIG group defines with examples how each prefix is translated into a folder path.

Main Composer commands

The tool is driven by a handful of commands. composer init creates the configuration file for a new project and composer require adds a library and installs it in the same step. composer install and composer update are the two operations described above. composer create-project downloads a complete project from a template, which is how Laravel or Symfony applications usually start. composer dump-autoload regenerates the class loader and composer audit checks for known vulnerabilities.

Development dependencies and scripts

The Composer configuration file separates two lists of dependencies. The require section contains what the project needs to run and require-dev what is only needed for coding and testing, such as testing frameworks or code analysers. In production only the first list is installed, so the server does not load tools that nobody uses there.

The scripts section lets you attach commands to events, for example clearing a cache after every installation or running the tests with a short command. It is a simple way to make the whole team follow the same procedures.

Composer compared with npm, pip and PEAR

Every programming language has its own dependency manager. npm fills that role in JavaScript, pip in Python and Bundler in Ruby, and Composer was directly inspired by those last two. The difference from the old PHP manager, PEAR, is fundamental. PEAR installed libraries for the whole server, so every project shared the same version. Composer installs them inside each project and pins their versions in a lock file.

Compared with npm, Composer installs a single version of each library per project. npm can place several versions of the same library at different levels of the tree, while the PHP manager forces it to find one version that satisfies everyone. It is stricter and sometimes harder to resolve, but it avoids duplicates.

Benefits and limitations of the dependency manager

The great advantage of Composer is that installations are repeatable. With both files in version control, any person or server can rebuild the same environment in seconds. Add to that autoloading, access to hundreds of thousands of packages and the ability to update libraries in a controlled way instead of copying them by hand.

Composer also has limitations that are worth knowing. Dependency resolution can use a lot of memory in large projects, the library folder grows quickly and everything depends on Packagist and the source repositories being available. On shared hosting without terminal access, moreover, running Composer on the server is not always possible and the folder has to be uploaded already prepared.

Security risks of Composer and its dependencies

Dependencies with vulnerabilities

A vulnerable library inside a plugin makes the whole website vulnerable, even if the plugin itself is well written. The problem is that those libraries do not appear in the list of plugins in the dashboard and can go years without being updated. Composer includes an audit function, the composer audit command, that compares the installed versions with known vulnerabilities.

An exposed vendor folder

If the server allows the vendor folder to be accessed from the browser, an attacker can reach test, example or utility files that contain executable code. The right approach is for it to sit outside the public directory or be blocked by the server, and for development libraries never to be uploaded to production.

Malicious or abandoned packages

Installing a library always means placing your trust in its author. An abandoned package, or one with a name similar to a popular one, can bring harmful code with it. It is wise to check who maintains what you install, pin versions and avoid inactive packages. This is the basis of what is known as supply chain security.

A simple measure is to review the changes to the lock file before deploying. If a new dependency suddenly appears that nobody asked for, or the source of a download changes, it deserves an explanation before it goes into production. Many teams include this in code review as one more of their checks.

Good practices with dependencies in production

In production it is advisable to install without development dependencies, with the --no-dev option, and to generate an optimised autoloader with --optimize-autoloader, which turns the lookup rules into a class map and speeds up every request. It also helps to declare the server’s PHP version in the configuration, so that Composer does not pick packages that would not work there. And the deployment should fail if the lock file does not match the configuration file, instead of improvising an update.

What to install in production with Composer and what to leave out

In any content management system, it is also worth asking extension authors how they handle updates to their internal libraries. A good extension updates them frequently and mentions it in its release notes. If an extension has not been updated for years, its internal libraries probably have not been either. Good WordPress maintenance includes this task as part of the regular review of the website.

Composer in WordPress

Plugins that ship their own library folder

Many professional plugins include a vendor folder with the libraries they need. It is common to find one in payment, shipping, email or integration plugins. The user does not have to do anything, but those libraries are part of the website and have to be kept up to date just like the plugin.

A well-known problem is two plugins including different versions of the same library. Since PHP can only load a class with a given name once, the second plugin ends up using the first one’s version, and that is the source of strange errors after updates. Developers avoid this by renaming the namespaces of the libraries they bundle.

Projects with every dependency managed in PHP

Professional teams manage the whole installation with Composer, including the WordPress core, plugins and themes, through a dedicated repository that mirrors the official catalogue. It allows versions to be pinned, deployments to be repeatable and changes to be reviewed, at the cost of a learning curve.

A vulnerability that threatened the PHP package catalogue

In April 2021, a researcher from the company SonarSource, Thomas Chauchefoin, discovered a command injection vulnerability in Composer, identified as CVE-2021-29472. The repository and download URLs of packages were not properly sanitised and could be interpreted as options to the system commands the manager runs, for example when downloading from Mercurial repositories.

The risk was enormous, because Packagist.org, the main catalogue, could have been the target. An attacker able to run commands on that infrastructure could have stolen maintainers’ credentials or modified downloads to inject harmful code into thousands of PHP projects.

The response from the Composer maintainers was fast. The report reached Packagist on 22 April, and the fixes to the catalogue and its private version were in production within about twelve hours. Composer released patched versions on 27 April. Packagist stated that it had no evidence of exploitation and recommended updating and reviewing lock files. The case is an example of why the tools that manage dependencies must also be treated as a critical part of security.

Frequently asked questions

Frequently asked questions about Composer

What is Composer?

It is the dependency manager for PHP. It installs in a project the third-party libraries it needs, downloads the right versions, resolves conflicts between them and generates an autoloader so they can be used without including each file by hand. It is the de facto standard of the PHP ecosystem.

What is Composer used for?

It is used to avoid reinventing the wheel. Instead of copying libraries into your project by hand, you declare them in a file and Composer takes care of downloading them, keeping them updated and loading them. It also lets you pin exact versions, so that every environment uses the same code.

What is the difference between composer.json and composer.lock in Composer?

The composer.json file declares which dependencies the project wants and within which version ranges. The composer.lock file records the exact versions that were installed. The first is written by the developer and the second is generated by Composer, and both are kept in version control.

What is the Composer autoload?

It is the automatic class loader that Composer generates in the vendor folder. By including a single file, the project can use any class from the installed libraries without loading them manually. It supports standards such as PSR-4, which maps namespaces to folders.

Which content management systems use Composer?

Drupal and Magento are officially installed and updated with Composer, and frameworks such as Laravel and Symfony depend on it. In WordPress many modern plugins use it for their libraries, and some projects manage the whole site with Composer through repositories such as WordPress Packagist. On simple websites the usual approach is not to touch it, because the extension already ships with its library folder.

What is the Composer vendor folder?

It is the folder where Composer installs all the third-party libraries and the autoload file. It should not be edited by hand, because Composer regenerates it. Nor should it be accessible from the browser in production, since it may contain code and test files that should not be public.

Is it safe to use Composer?

It is if it is used carefully. Composer downloads third-party code, so you have to trust what you install, keep dependencies updated and review security advisories. It includes an audit function that compares the installed versions with known vulnerabilities.

What is the difference between composer install and composer update?

Install uses the exact versions stored in composer.lock, and it is what is run on servers and deployment environments to get repeatable results. Update looks for the most recent versions allowed by composer.json and rewrites the lock file, and it is done in a controlled way.

Dependency and security audit for your website

I review the third-party libraries loaded by your application, your template and your extensions, detect versions with known vulnerabilities and give you a plan to update them.

About the author

Javier Afán

WordPress developer specialised in security, WooCommerce, and hacked website recovery.

I work on WordPress from a technical angle focused on security, architecture, and the decisions that really affect a project's continuity.