How to Install and Use Composer for PHP on AlmaLinux 9 / Rocky Linux 9
Install Composer on AlmaLinux 9, Rocky Linux 9 or CentOS Stream 9 with a verified installer, then add, install and update PHP dependencies per project.
Composer is the standard dependency manager for PHP. It downloads the libraries a project declares into the project's own vendor directory and generates an autoloader for them; nothing is installed system-wide. This guide installs Composer on AlmaLinux 9, Rocky Linux 9 or CentOS Stream 9 using the method documented by the Composer project, and shows the handful of commands you need day to day.
CentOS 6 reached end of life in November 2020, and the PHP 5.x builds that were used with it cannot run current Composer, which requires PHP 7.2.5 or newer. The RHEL 9 family provides PHP 8, which is what you should use for new work.
Requirements
- A server running AlmaLinux 9, Rocky Linux 9 or CentOS Stream 9.
- A regular user with
sudorights (the examples usedeploy). Run Composer itself as this user, not as root.
Step 1: Install PHP and the tools Composer uses
The default PHP in the RHEL 9 family is 8.0; newer versions are offered as module streams. List them:
sudo dnf module list php
On a server without PHP yet, enable the newest stream shown (8.3 in this example) before installing. If PHP is already installed for your application, skip this command and keep the version you have:
sudo dnf module enable php:8.3
Install the PHP command-line interpreter, the extensions most packages expect, and the tools Composer calls to download and unpack packages:
sudo dnf install php-cli php-mbstring php-xml php-pecl-zip unzip git
Confirm that PHP runs:
php -v
Step 2: Download and verify the installer
Composer is distributed as a single file, composer.phar, produced by an installer script. The installer's SHA-384 checksum changes whenever the installer is updated, so do not copy a hash from a tutorial. Fetch the current value from the Composer project and compare it with the file you downloaded:
cd ~
EXPECTED_CHECKSUM="$(php -r 'copy("https://composer.github.io/installer.sig", "php://stdout");')"
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
ACTUAL_CHECKSUM="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"
[ "$EXPECTED_CHECKSUM" = "$ACTUAL_CHECKSUM" ] && echo "Installer verified" || echo "Installer corrupt"
Continue only if the last line prints Installer verified. If it prints Installer corrupt, delete composer-setup.php and download it again. The same hash, with a ready-made copy-and-paste block, is published on the Composer download page.
Step 3: Install Composer
Globally, for all projects
Run the installer, move the resulting file into a directory on the PATH and remove the installer:
php composer-setup.php
sudo mv composer.phar /usr/local/bin/composer
php -r "unlink('composer-setup.php');"
Check the result:
composer --version
If you prefer not to use sudo, the installer can write straight into a directory you own that is on your PATH:
mkdir -p ~/.local/bin
php composer-setup.php --install-dir=$HOME/.local/bin --filename=composer
Locally, inside one project
Run the installer in the project directory and leave composer.phar there. You then call it as php composer.phar instead of composer:
mkdir -p ~/myproject
mv composer-setup.php ~/myproject/
cd ~/myproject
php composer-setup.php
php -r "unlink('composer-setup.php');"
php composer.phar --version
The rest of this guide uses the global composer command.
Step 4: Add dependencies to a project
Go to the project directory and require a package. The name has the form vendor/package, as listed on packagist.org, Composer's default repository:
mkdir -p ~/myproject
cd ~/myproject
composer require psr/log
Composer picks the newest version that fits your PHP version, writes it to composer.json, records the exact versions it installed in composer.lock and downloads the code into vendor/. The generated composer.json looks similar to this (the version constraint will reflect the current release):
{
"require": {
"psr/log": "^3.0"
}
}
You can also edit composer.json by hand, or create it interactively with composer init. Packages needed only for development, such as test frameworks, are added with --dev:
composer require --dev phpunit/phpunit
To use the installed libraries, load Composer's autoloader once at the start of your code:
<?php
require __DIR__ . '/vendor/autoload.php';
Step 5: Install and update
The two commands that matter most do different things:
composer installinstalls exactly the versions recorded incomposer.lock. Use it after cloning a project and when deploying. If no lock file exists, it resolves the dependencies and creates one.composer updatelooks for newer versions allowed bycomposer.json, installs them and rewritescomposer.lock. Use it deliberately, on a development machine, and test afterwards.
Commit both composer.json and composer.lock to version control, and keep vendor/ out of it. On a production server, skip development packages and build an optimised autoloader:
composer install --no-dev --optimize-autoloader
To update a single package rather than everything:
composer update psr/log
Step 6: Keep Composer itself up to date
Composer updates itself. If you installed it to /usr/local/bin, the file is owned by root, so the update needs sudo and the full path:
sudo /usr/local/bin/composer self-update
If a new release causes trouble, go back to the previous one:
sudo /usr/local/bin/composer self-update --rollback
Troubleshooting
- General problems:
composer diagnosechecks connectivity, PHP settings and the Composer version and reports what is wrong. - "Your requirements could not be resolved": a package needs a PHP version or extension you do not have. The message names it; install the extension with
sudo dnf install php-EXTENSIONand run the command again. - Slow installs or "failed to extract" errors: make sure
unzipandgitare installed, as in step 1. - Invalid composer.json:
composer validatepoints to the syntax error. - Warning about running as root: switch to your regular user. Package scripts and plugins run with the rights of whoever starts Composer.
Frequently asked questions
Can I install Composer with dnf instead?
The EPEL repository carries a composer package for the RHEL 9 family. It is convenient, but it is updated through dnf on EPEL's schedule and self-update is not the way to upgrade it. The installer from getcomposer.org gives you the current release directly from the project.
How do I check my dependencies for known security issues?
Run composer audit in the project directory. It compares the installed packages with published security advisories and exits with a non-zero status when it finds a match, which makes it easy to use in a deployment script. composer outdated lists packages that have newer versions.
I still have an application on PHP 5 or 7.0/7.1. Which Composer can it use?
Current Composer needs PHP 7.2.5 or later. For older PHP the project maintains a long-term-support line, 2.2, which you select with php composer-setup.php --2.2. Treat it as a stopgap while you move the application to a supported PHP version.
Where does Composer keep its cache and global settings?
In the Composer home directory of the user who runs it: $XDG_CONFIG_HOME/composer on systems that follow the XDG directory layout, otherwise ~/.composer. See the command-line reference for the environment variables that change these locations.