On this page
- Which packages does Composer need first?
- How do you install Composer with the verified installer?
- Should you use the distribution package instead?
- What is the difference between project and global packages?
- What do version constraints and the lock file do?
- Which flags belong in a production install?
- How do you keep Composer itself up to date?
- What fixes the common Composer errors?
- Questions
Which packages does Composer need first?
Composer only needs the PHP command line interpreter plus a way to unpack archives, and Git for packages that come from source. Install them from the distribution repositories; if PHP is not on the server yet, the PHP and PHP-FPM guide walks through versions and extensions.
# Ubuntu 24.04 / 22.04, Debian 12 / 13
sudo apt update
sudo apt install -y php-cli php-zip php-curl php-mbstring php-xml unzip git curl
# Rocky Linux 9 / AlmaLinux 9
sudo dnf module list php
sudo dnf module enable -y php:8.3
sudo dnf install -y php-cli php-zip php-mbstring php-xml unzip git curlThe dnf module list line shows which PHP streams your point release offers; pick the newest one your application supports before enabling it. Confirm the interpreter works with php -v. Work as a normal sudo user, not as root: Composer runs package scripts, and those should never execute with full privileges.
How do you install Composer with the verified installer?
Fetch the installer and its expected checksum, compare the two, and only then run it. The Composer project publishes the current installer signature at a fixed address, so this script keeps working when the installer changes.
cd ~
EXPECTED="$(php -r 'copy("https://composer.github.io/installer.sig", "php://stdout");')"
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
ACTUAL="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"
if [ "$EXPECTED" != "$ACTUAL" ]; then echo "Checksum mismatch"; rm composer-setup.php; exit 1; fi
php composer-setup.php --quiet
rm composer-setup.php
sudo mv composer.phar /usr/local/bin/composer
composer --versionIf the comparison fails, do not run the file: download it again later or check your network path. A successful run prints a line such as Composer version 2.x. Run composer diagnose once; it tests HTTPS access to the package repository, checks PHP settings and warns about anything that could slow installs.
Should you use the distribution package instead?
The composer package in Ubuntu and Debian works, but it follows the release cycle of the distribution and can trail the upstream version by a long way. The self-installed binary updates itself with one command and gets fixes as soon as they ship, which matters when a new PHP release needs a newer Composer. On Rocky and Alma there is no Composer package in the base repositories at all, so the installer route is the normal one there. If you already installed the apt package, remove it with sudo apt remove composer before placing the binary in /usr/local/bin, otherwise which composer may point at the older copy.
What is the difference between project and global packages?
Project packages belong to one application: they are listed in its composer.json, pinned in composer.lock and installed into that project's vendor/ directory. Almost everything should be installed this way, because each app then carries the exact versions it was tested with.
mkdir -p ~/demo && cd ~/demo
composer init --no-interaction --name=deploy/demo
composer require monolog/monolog
composer show --treeGlobal packages are command line tools you want on every project, such as a code style fixer or the Laravel installer. They live under your user's Composer home, never under the system PHP directories.
composer global require laravel/installer
composer global config bin-dir --absolute
echo 'export PATH="$HOME/.config/composer/vendor/bin:$PATH"' >> ~/.bashrc
source ~/.bashrcUse the path printed by the bin-dir command; older setups keep it under ~/.composer/vendor/bin. Keep global installs small, because every global tool shares one dependency tree and two tools with clashing requirements will block each other.
What do version constraints and the lock file do?
Constraints in composer.json say which versions are acceptable; the lock file records the exact versions that were resolved. A caret constraint such as ^3.5 accepts any 3.x release from 3.5 up, and a tilde such as ~3.5.0 stays within 3.5.x.
composer updateresolves new versions within the constraints and rewrites the lock file. Run it on a development machine and commit the result.composer installreads the lock file and installs exactly what is listed. This is the command for servers.composer outdated --directlists your own dependencies that have newer releases.composer why vendor/packageexplains which package pulled something in.
Commit composer.lock for applications so every server gets identical code. Libraries meant for others usually leave it out.
Which flags belong in a production install?
On a live server install from the lock file without development packages and with an optimized class map. This shrinks vendor/, speeds up class loading and stops test tools from ever reaching the public machine.
cd /var/www/app.example.net
composer install --no-dev --optimize-autoloader --no-interaction --no-progress
composer check-platform-reqs --no-dev
composer audit --no-devcheck-platform-reqs confirms the server has every PHP extension and version the lock file expects, which catches a missing intl or bcmath before users do. audit compares installed packages with published security advisories. For maximum speed you can add --classmap-authoritative, which tells the autoloader to look only in the class map; leave it off if your app generates classes at runtime. Run Composer as the user who owns the code, never with sudo, so files in vendor/ do not end up owned by root.
How do you keep Composer itself up to date?
Update the binary with its built-in command and roll back if a new release misbehaves.
sudo composer self-update
composer --version
sudo composer self-update --rollbackBecause the binary sits in /usr/local/bin, the update needs sudo; it touches only that one file. Pin a major line with sudo composer self-update --2 if you want to stay on Composer 2 even when a future major release appears. Clearing the package cache with composer clear-cache frees disk space after many installs, which helps on smaller plans.
What fixes the common Composer errors?
- Killed or out of memory during
update: dependency resolution is memory hungry. Resolve on your workstation and only runinstallon the server, or add a swap file.COMPOSER_MEMORY_LIMIT=-1lifts the PHP limit for a single run. - Missing ext-something: install the matching PHP extension package, for example
php-intl, then retry. - Do not run Composer as root/super user: switch to the project owner. Plugins and scripts run with your privileges.
- Slow or failing downloads:
composer diagnoseshows whether HTTPS and DNS work; check outbound rules if you built a strict firewall. - Lock file out of date warning: someone edited
composer.jsonwithout updating. Runcomposer update --lockon a development copy and commit.
Plans that fit this guide
Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.
Questions about Composer for PHP projects
Is Composer 1 still usable?
No. Composer 1 lost access to the main package metadata, so use Composer 2. composer self-update --2 moves an old install to the current line.
Can I commit the vendor directory instead of running Composer on the server?
You can, but it bloats the repository and hides which versions are installed. Building vendor/ from the lock file on the server, or in a build step that uploads an archive, is cleaner.
Why does composer install ask for a GitHub token?
Heavy use of source downloads can hit anonymous rate limits. Create a read-only token and store it with composer config --global github-oauth.github.com <token>; it stays in your user's Composer auth file.
Which PHP extensions do most frameworks expect?
Typically mbstring, xml, curl, zip, intl, bcmath and a database driver such as pdo_mysql. Run composer check-platform-reqs to get the exact list for your project.
Does Composer need a lot of RAM?
Installing from a lock file is light. Running update on a large project can use well over 1 GB, which is why resolving locally and installing on the server is the safer routine for a 1 GB plan.