Composer in One Paragraph
Composer reads a project's composer.json, finds versions of each required library that fit together, downloads them from Packagist (the main public package repository) into vendor/, and writes the exact versions it chose into composer.lock. Frameworks such as Laravel and Symfony are installed and updated this way. Composer works per project; it does not install libraries system-wide.
composer.json and composer.lock
A small composer.json:
{
"require": {
"php": ">=8.3",
"monolog/monolog": "^3.0"
},
"autoload": {
"psr-4": { "App\\": "src/" }
}
}
- The constraint ^3.0: any 3.x release from 3.0 up, but not 4.0.
- composer.lock: the exact version of every package, including dependencies of dependencies. It makes every install of the project identical.
- What to commit: both files go into Git; the
vendor/folder does not. See how to use .gitignore.
install vs update
| Command | What it does |
|---|---|
composer install | Installs the versions in the lock file |
composer update | Picks newer allowed versions, rewrites the lock |
composer require a/b | Adds a package and installs it |
composer remove a/b | Removes a package |
Run update deliberately, in development, then test and commit the new lock file. Servers only ever run install.
Install Composer on a Server
Composer needs the PHP command-line binary, and works best with unzip and git installed; see how to install PHP on Ubuntu. There are two routes:
- Distribution package:
sudo apt install composeron Debian and Ubuntu. It is maintained by the distribution, but often older than the current release. - The project's installer: the download page on getcomposer.org gives a short set of commands. They download the installer with PHP, compare its SHA-384 hash with the hash the Composer project publishes, run the installer only if the two match, and delete it. Copy those commands from the page each time, because the hash changes with every installer release. Then move
composer.pharto/usr/local/bin/composer.
Check the result with composer --version.
Do Not Run Composer as Root
Packages can include plugins and scripts that run during install and update with your permissions. Run as root, a compromised package controls the whole server. Composer prints a warning when it runs as root. Run it as the user that owns the project folder instead:
sudo -u deploy composer install
A Production Deploy
cd /var/www/app
composer check-platform-reqs --lock --no-dev
composer install --no-dev --optimize-autoloader --no-interaction
check-platform-reqs confirms the server's PHP version and extensions meet the requirements; --lock reads them from the lock file, so it works before vendor/ exists. --no-dev skips the packages under require-dev, such as test tools, which do not belong on a live server. --optimize-autoloader builds a class map for faster loading. Run composer audit to list installed packages with known security advisories or abandoned packages.
Autoloading
One line at the start of your application loads every installed library and your own classes:
require __DIR__ . '/vendor/autoload.php';
After you change the autoload section, run composer dump-autoload.
Common Problems
- Missing PHP extension: the error names a requirement such as
ext-mbstring. Install the matching package, for examplephp-mbstring. - Lock file out of date: someone changed
composer.jsonwithout updating the lock. Fix it in development and commit the new lock. - Out of memory: large updates need more memory. Run them in development, not on a small production server.
To keep deploys repeatable, pull code with Git and run composer install afterwards; see GitHub SSH key for server access to a repository.