
Composer is the standard tool for managing PHP dependencies. It automates library installation, enforces version constraints, and helps organize autoloading, making it an essential part of modern PHP development. Whether you're building with frameworks like Laravel, Symfony, or Slim, Composer ensures consistency across environments and simplifies dependency resolution.
This article explains how to install Composer with a specific PHP version on a Linux system. This setup is useful when working across multiple PHP projects or servers that require different runtime versions. You'll learn how to install Composer safely, bind it to a specific PHP version such as PHP 8.4, and create reusable aliases for scripting or interactive use.
Before you begin, ensure you have:
sudo privileges.Install PHP, the command-line interface (CLI), and required tools such as curl and unzip using your distribution's package manager.
Run the following command to install the default PHP version and required utilities on APT-based systems.
To install a specific PHP version (such as 8.3 or 8.4), add a third-party repository:
These repositories provide maintained versions of PHP not available in the default APT sources.
This article uses PHP 8.4 as the example version. On DNF-based distributions (like Rocky Linux or AlmaLinux), the system uses php as the command unless update-alternatives is configured. If php8.4 doesn't work, use php instead.
Follow these steps to install Composer using PHP 8.4.
Download the Composer installer using PHP 8.4.
Get the installer signature. This ensures the installer is authentic and has not been tampered with.
Copy the hash from the output. You’ll need it for verification in the next step.
Verify the installer. Replace your_hash_here with the hash you copied:
Output.
Run the Composer installer using PHP 8.4 and install it system-wide.
Output.
Clean up the installer file.
Verify the Composer installation.
Output.
To run Composer with a specific PHP version (like PHP 8.4), follow these steps to configure your environment.
Confirm the path of the PHP 8.4 binary.
Output.
Run Composer using the PHP 8.4 binary.
Create an alias so composer always runs with PHP 8.4. (Optional)
For portability in scripts or cron jobs, consider using a shebang like #!/usr/bin/env php instead of hardcoding a version path.
Reload your shell configuration to apply the alias.
If you're using the Zsh, replace ~/.bashrc with ~/.zshrc.
Verify that Composer now runs with PHP 8.4.
Output.
For managing multiple Composer versions or isolating global dependencies per PHP version, consider tools like cgr, composer-bin, or asdf.
In this article, you installed Composer on a Linux system and configured it to run with a specific PHP version such as PHP 8.4. You learned how to safely verify the installer, install Composer system-wide, and run it using the correct PHP binary. You also created a reusable alias for convenience and confirmed that Composer works by initializing a basic project.
With this setup, you're now equipped to manage dependencies across multiple PHP projects with varying version requirements. For advanced use cases, such as global tool isolation or multi-version workflows, consider tools like cgr, composer-bin, or asdf.
To learn more, visit the official Composer documentation.
0 Comments
Be the first to comment and share your perspective with the community.