LinuxCommandLibrary

composer

Manage PHP dependencies

TLDR

Interactively create a composer.json file

$ composer init
copy

Add a package as a dependency for this project, adding an entry to composer.json
$ composer require [user/package]
copy

Install all the dependencies in this project's composer.json and create composer.lock
$ composer install
copy

Uninstall a package from this project, removing it as a dependency from composer.json and composer.lock
$ composer remove [user/package]
copy

Update all the dependencies in this project's composer.json and note new versions in composer.lock file
$ composer update
copy

Update only composer.lock after updating composer.json manually
$ composer update --lock
copy

Learn more about why a dependency can't be installed
$ composer why-not [user/package]
copy

Update composer to its latest version
$ composer self-update
copy

SYNOPSIS

composer command [options] [arguments]

PARAMETERS

--help (-h)
    Displays help for the given command. When no command is specified, it shows the list of available commands.

--version (-V)
    Displays this application version.

--quiet (-q)
    Do not output any message.

--verbose (-v, -vv, -vvv)
    Increases the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug.

--no-interaction (-n)
    Do not ask any interactive question.

--profile
    Display timing and memory usage information.

--working-dir (-d)
    If specified, use the given directory as working directory.

--no-dev
    Disables installation of require-dev packages. Commonly used with install or update.

--optimize-autoloader (-o)
    Convert PSR-0/4 autoloading to classmap to get a faster autoloader. Commonly used with install or update.

--dry-run
    Simulates a command without actually performing any changes. Used to see what would happen.

DESCRIPTION

Composer is an application-level package manager for the PHP programming language. It provides a standard format for managing dependencies of PHP software and required libraries. Unlike system package managers (like apt or yum), Composer focuses on a project's dependencies and installs them locally within the project directory (typically vendor/).

It works by reading a composer.json file, which declares the libraries your project depends on. Composer then finds the correct versions of these libraries, downloads them, and installs them into your project. It also generates an autoloader script, making it easy to use the installed libraries without manual `require` statements. Composer is widely adopted in the PHP ecosystem, facilitating collaborative development, maintaining consistent environments, and streamlining the inclusion of third-party packages from its primary repository, Packagist.

CAVEATS

Composer requires PHP to be installed on your system. It relies heavily on network connectivity to download packages from Packagist or other repositories. For large projects with many dependencies, operations like `composer install` or `composer update` can consume significant time and memory. It's crucial to be mindful of the source of your packages to avoid security vulnerabilities, as malicious packages could compromise your system.

<B>COMPOSER.JSON</B>

The primary configuration file for Composer. It's a JSON object that defines project metadata, required packages, autoloading rules, scripts, and other configurations. It's typically located in the root of your project directory.

<B>COMPOSER.LOCK</B>

This file is automatically generated after a successful `composer install` or `composer update`. It records the exact versions of all installed packages, including their transitive dependencies. This ensures that everyone working on the project, and production environments, uses the identical set of dependencies, promoting consistency and reproducibility.

<B>PACKAGIST</B>

The main package repository for Composer. It's where Composer looks for packages by default. Developers publish their PHP libraries to Packagist, making them discoverable and installable via Composer.

<B>VENDOR/</B> DIRECTORY

By default, Composer installs all project dependencies into this directory. It's recommended to add this directory to your version control's ignore list (e.g., .gitignore) and regenerate it using `composer install` when cloning a project.

AUTOLOADING

Composer generates a vendor/autoload.php file. Including this single file in your project automatically makes all classes from your installed dependencies, as well as your own project's classes (if configured in composer.json), available without requiring manual `require` or `include` statements for each file.

HISTORY

Composer was created by Nils Adermann and Jordi Boggiano and first released in March 2012. Its development was largely inspired by similar tools in other language ecosystems, notably Node.js's npm and Ruby's Bundler. Before Composer, PHP lacked a widely accepted standard for dependency management, leading to fragmented approaches and challenges in integrating third-party libraries. Composer quickly filled this void, becoming the de-facto standard for managing PHP project dependencies and revolutionizing how modern PHP applications are built and maintained.

SEE ALSO

php(1), git(1), npm(1), pip(1), bundle(1)

Copied to clipboard