Composer is the de facto dependency manager for PHP, used by virtually every modern PHP framework and library including Laravel, Symfony, and Guzzle. It resolves and installs packages from Packagist, handles autoloading, and tracks version constraints so your project dependencies stay consistent across environments. This tutorial covers installing Composer on RHEL 8 using the official installer script, verifying the cryptographic hash, and making the binary globally accessible. You will also learn the essential Composer commands you will use every day.
Prerequisites
- PHP 8.0 or newer installed on RHEL 8 (including
php-cliandphp-mbstring) - The
curlorwgetutility available on the system - A user account with
sudoprivileges or root access - Internet access to reach
getcomposer.organdpackagist.org
Step 1 — Ensure PHP CLI Is Available
Composer runs as a PHP archive, so you need a working PHP CLI before downloading the installer. Confirm your PHP version and check that the required extensions are present.
php --version
php -m | grep -E "openssl|mbstring|curl|json|phar"
All five extensions — openssl, mbstring, curl, json, and phar — must appear in the output. Install any missing ones with sudo dnf install php-mbstring php-openssl.
Step 2 — Download the Composer Installer
Composer provides an official PHP installer script that builds the composer.phar archive. Download it using PHP itself.
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
This creates composer-setup.php in your current directory. The file is a PHP script that builds and self-verifies the Composer archive during installation.
Step 3 — Verify the Installer Hash
Always verify the installer’s SHA-384 hash against the value published on the Composer website to ensure the file was not tampered with in transit.
HASH="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"
EXPECTED="$(curl -sS https://composer.github.io/installer.sig)"
if [ "$HASH" = "$EXPECTED" ]; then
echo "Installer verified successfully."
else
echo "Installer corrupt — hash mismatch. Aborting."
rm composer-setup.php
fi
Only proceed if the script prints Installer verified successfully. If the hashes differ, delete the file and download it again. Never skip this step on a production system.
Step 4 — Run the Installer and Install Globally
Execute the installer to produce composer.phar, then move it to /usr/local/bin/composer so it is accessible system-wide without a path prefix.
php composer-setup.php
sudo mv composer.phar /usr/local/bin/composer
rm composer-setup.php
composer --version
The composer --version command should print the installed version and release date. You can now run composer from any directory as any user who can reach /usr/local/bin.
Step 5 — Essential Composer Commands
These are the core commands you will use during day-to-day PHP development on RHEL 8.
# Initialise a new project interactively
composer init
# Add a package to your project
composer require monolog/monolog
# Install all packages listed in composer.json
composer install
# Update all packages to their latest allowed versions
composer update
# Regenerate the autoloader (run after adding files)
composer dump-autoload -o
The -o flag on dump-autoload generates an optimised classmap autoloader, which is important for production performance.
Step 6 — Install Packages Globally
Composer can install tools globally for the current user, placing binaries in ~/.config/composer/vendor/bin. Add that directory to your PATH to run global tools by name.
composer global require friendsofphp/php-cs-fixer
# Add Composer's global bin directory to your PATH
echo 'export PATH="$HOME/.config/composer/vendor/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
php-cs-fixer --version
Global packages are useful for development tools like code formatters, static analysers, and framework installers that you want available across multiple projects.
Conclusion
You have installed Composer globally on RHEL 8, verified the installer’s cryptographic hash, and learned the essential commands for managing PHP dependencies. With Composer in place, you can start creating PHP projects that leverage the vast Packagist ecosystem. Your RHEL 8 server is now ready for modern PHP framework installation.
Next steps: How to Install Laravel on RHEL 8, How to Install Symfony on RHEL 8, and How to Configure PHP-FPM with Nginx on RHEL 8.