---
title: How to Install Composer with PHP
url: https://docs.vultr.com/how-to-install-composer-with-php
description: Learn how to install Composer with PHP on your system with our step-by-step guide. Get your PHP dependency management tool up and running quickly.
publish_date: 2025-05-22T15:16:26.035697Z
last_updated: 2025-05-22T15:16:26.035697Z
author: Darshan  Siroya
header_image: https://docs.vultr.com/public/doc-assets/2221/b318db31-d835-4b71-9b63-77d641d40ffb.png
---

> Index of every Vultr Docs page in markdown: https://docs.vultr.com/llms.txt

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.

## Prerequisites

Before you begin, ensure you have:

* Access to a [Linux instance](https://www.vultr.com/servers).
* [SSH access to the server](https://docs.vultr.com/how-to-use-ssh-with-vultr-servers).
* A non-root user with `sudo` privileges.
* [Updated the server](https://docs.vultr.com/how-to-update-a-vultr-cloud-server).

## Install PHP

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

???+ note "Install PHP on Ubuntu and Debian"

    Run the following command to install the default PHP version and required utilities on APT-based systems.

    ```console
    $ sudo apt install -y php php-cli curl unzip
    ```

    To install a specific PHP version (such as 8.3 or 8.4), add a third-party repository:

    * On **Ubuntu**, use the [Ondřej Surý PPA](https://docs.vultr.com/how-to-install-php-and-php-fpm-on-ubuntu-24-04#install-multiple-php-versions).
    * On **Debian**, use the [SURY repository](https://docs.vultr.com/how-to-install-php-and-php-fpm-on-debian-12#install-php-on-debian-12).

    These repositories provide maintained versions of PHP not available in the default APT sources.

??? note "Install PHP on Rocky Linux and AlmaLinux"

    On DNF-based distributions, install PHP and required tools with:

    ```console
    $ sudo dnf install -y php php-cli curl unzip
    ```

    To install a specific PHP version, [enable the EPEL and Remi repositories](https://docs.vultr.com/how-to-install-php-8-on-rocky-linux-9#install-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 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.

1. Download the Composer installer using PHP 8.4.

    ```console
    $ php8.4 -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
    ```

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

    ```console
    $ curl -s https://composer.github.io/installer.sig
    ```

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

1. Verify the installer. Replace `your_hash_here` with the hash you copied:

    ```console
    $ 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;"
    ```

    Output.

    ```
    Installer verified
    ```

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

    ```console
    $ sudo php8.4 composer-setup.php --install-dir=/usr/local/bin --filename=composer
    ```

    Output.

    ```
    All settings correct for using Composer
    Downloading…
    
    Composer (version 2.8.8) successfully installed to: /usr/local/bin/composer
    Use it: php /usr/local/bin/composer
    ```

1. Clean up the installer file.

    ```console
    $ rm composer-setup.php
    ```

1. Verify the Composer installation.

    ```console
    $ composer
    ```

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

## Run Composer with a Specific PHP Version

To run Composer with a specific PHP version (like PHP 8.4), follow these steps to configure your environment.

1. Confirm the path of the PHP 8.4 binary.

    ```console
    $ which php8.4
    ```

    Output.

    ```
    /usr/bin/php8.4
    ```

1. Run Composer using the PHP 8.4 binary.

    ```console
    $ php8.4 /usr/local/bin/composer
    ```

1. Create an alias so `composer` always runs with PHP 8.4. (Optional)

    ```console
    $ echo 'alias composer="php8.4 /usr/local/bin/composer"' >> ~/.bashrc
    ```

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

1. Reload your shell configuration to apply the alias.

    ```console
    $ source ~/.bashrc
    ```

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

1. Verify that Composer now runs with PHP 8.4.

    ```console
    $ composer --version
    ```

    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.
    ```

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

??? note "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`.

        ```console
        $ composer init --name=test/app --description="Test app" --author="Your Name" --require=php:^8.4 --no-interaction
        ```

    1. View the generated file to confirm success.

        ```console
        $ cat composer.json
        ```

        Output.

        ```
        {
            "name": "test/app",
            "description": "Test app",
            "require": {
                "php": "^8.4"
            }
        }
        ```

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

## Conclusion

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](https://asdf-vm.com/).

To learn more, visit the [official Composer documentation](https://getcomposer.org/doc/).
