DEV Community

Cover image for Installing Composer with PHP
Sanskriti Harmukh for Vultr

Posted on with Aashish Chaurasiya Originally published at docs.vultr.com

Installing Composer with PHP

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 guide covers installing Composer with a specific PHP version on a Linux system — useful when working across multiple PHP projects or servers that require different runtime versions. By the end, you'll have Composer installed safely, bound to a specific PHP version such as PHP 8.4, and a reusable alias set up for scripting or interactive use.


Prerequisites

  • Access to a Linux instance.
  • SSH access to the server.
  • A non-root user with sudo privileges.
  • The server's package index already updated.

Install PHP

Install PHP, the command-line interface (CLI), and required tools such as curl and unzip using your distribution's package manager.

Install PHP on Ubuntu and Debian

1. Install the default PHP version and required utilities on APT-based systems:

$ sudo apt install -y php php-cli curl unzip
Enter fullscreen mode Exit fullscreen mode

To install a specific PHP version (such as 8.3 or 8.4), add a third-party repository — the Ondřej Surý PPA on Ubuntu, or the SURY repository on Debian. These repositories provide maintained versions of PHP not available in the default APT sources.

Install PHP on Rocky Linux and AlmaLinux

1. Install PHP and required tools on DNF-based distributions:

$ sudo dnf install -y php php-cli curl unzip
Enter fullscreen mode Exit fullscreen mode

To install a specific PHP version, enable the EPEL and Remi repositories. These repositories offer multiple PHP versions for RHEL-based systems including Rocky Linux and AlmaLinux.


Install Composer Using a Specific PHP Version

Note: This guide 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.

1. Download the Composer installer using PHP 8.4:

$ php8.4 -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
Enter fullscreen mode Exit fullscreen mode

2. Get the installer signature. This ensures the installer is authentic and has not been tampered with:

$ curl -s https://composer.github.io/installer.sig
Enter fullscreen mode Exit fullscreen mode

Copy the hash from the output. You'll need it for verification in the next step.

3. Verify the installer. Replace your_hash_here with the hash you copied:

$ php8.4 -r "if (hash_file('sha384', 'composer-setup.php') === 'your_hash_here') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
Enter fullscreen mode Exit fullscreen mode

Output:

Installer verified
Enter fullscreen mode Exit fullscreen mode

4. Run the Composer installer using PHP 8.4 and install it system-wide:

$ sudo php8.4 composer-setup.php --install-dir=/usr/local/bin --filename=composer
Enter fullscreen mode Exit fullscreen mode

5. Clean up the installer file:

$ rm composer-setup.php
Enter fullscreen mode Exit fullscreen mode

6. Verify the Composer installation:

$ composer
Enter fullscreen mode Exit fullscreen mode

Output:

    ______
   / ____/___  ____ ___  ____  ____  ________  _____
  / /    / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
 / /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
 \____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                     /_/
Composer version 2.8.8 2025-04-04 16:56:46
Enter fullscreen mode Exit fullscreen mode

Run Composer with a Specific PHP Version

To run Composer with a specific PHP version (like PHP 8.4), configure your environment as follows.

1. Confirm the path of the PHP 8.4 binary:

$ which php8.4
Enter fullscreen mode Exit fullscreen mode

Output:

/usr/bin/php8.4
Enter fullscreen mode Exit fullscreen mode

2. Run Composer using the PHP 8.4 binary:

$ php8.4 /usr/local/bin/composer
Enter fullscreen mode Exit fullscreen mode

3. Create an alias so composer always runs with PHP 8.4 (optional):

$ echo 'alias composer="php8.4 /usr/local/bin/composer"' >> ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

For portability in scripts or cron jobs, consider using a shebang like #!/usr/bin/env php instead of hardcoding a version path.

4. Reload your shell configuration to apply the alias:

$ source ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

If you're using Zsh, replace ~/.bashrc with ~/.zshrc.

5. Verify that Composer now runs with PHP 8.4:

$ composer --version
Enter fullscreen mode Exit fullscreen mode

Output:

Composer version 2.8.8 2025-04-04 16:56:46
PHP version 8.4.0 (/usr/bin/php8.4)
Run the "diagnose" command to get more detailed diagnostics output.
Enter fullscreen mode Exit fullscreen mode

For managing multiple Composer versions or isolating global dependencies per PHP version, consider tools like cgr, composer-bin, or asdf.

Test Composer Functionality (Optional)

To confirm that Composer is functional and can generate project scaffolding:

1. Run composer init in non-interactive mode to generate a minimal composer.json:

$ composer init --name=test/app --description="Test app" --author="Your Name" --require=php:^8.4 --no-interaction
Enter fullscreen mode Exit fullscreen mode

2. View the generated file to confirm success:

$ cat composer.json
Enter fullscreen mode Exit fullscreen mode

Output:

{
    "name": "test/app",
    "description": "Test app",
    "require": {
        "php": "^8.4"
    }
}
Enter fullscreen mode Exit fullscreen mode

You can now run composer install or add additional dependencies to begin building your PHP project.


Next Steps

You now have Composer installed on your Linux system, bound to a specific PHP version such as PHP 8.4, with a reusable alias for convenience. From here, you can:

  • Run composer require to add your first dependencies to a real project
  • Set up global tool isolation or multi-version workflows with cgr, composer-bin, or asdf
  • Add Composer's vendor/ directory to your .gitignore and commit composer.lock for reproducible installs
  • Read the official Composer documentation to learn more about autoloading and dependency constraints

For the full guide with additional tips, visit the original article on Vultr Docs.

Top comments (0)