How to Install Composer on Debian Linux

Composer manages PHP project dependencies automatically, ensuring your applications use compatible library versions across development and production environments. Whether you need to build Laravel web applications, maintain Symfony projects, or integrate third-party packages into custom PHP code, Composer handles version resolution, autoloading, and updates through a single command-line interface. This guide shows you how to install Composer on Debian using the APT package manager or the official installer, with the ability to create projects, install packages, and manage dependencies using commands like composer require and composer update.

Update System Packages

Before installing Composer, ensure your package list is up-to-date with the following command.

sudo apt update

The below command can update any packages that are no longer current on your system.

sudo apt upgrade

Choose Your Composer Installation Method

Composer is available through two primary installation methods on Debian. The APT package manager provides automatic security updates through your system, while the official installer gives you immediate access to the latest upstream release with self-update capabilities.

MethodChannelVersionUpdatesBest For
APT Package ManagerDebian ReposDistribution stableAutomatic via apt upgradeMost users who want system integration
Official Installergetcomposer.orgLatest upstreamManual via composer self-updateUsers needing newest features immediately

For most users, the APT method is recommended because it integrates with your system package manager for automatic security updates and provides a stable, well-tested version. The official installer is best when you need cutting-edge features or want full control over version management.

Install PHP and Required Packages

Composer requires PHP to execute dependency management commands, along with several supporting packages for downloading and extracting packages. Install the prerequisites with the following command.

sudo apt install php-cli unzip curl git -y

This installs the PHP command-line interpreter, unzip for extracting package archives, curl for downloading the official installer, and git for VCS-based package installation.

If you need a specific PHP version or the latest stable release, refer to our guide on installing PHP on Debian for instructions on using the Sury repository or compiling from source.

Install Composer

Method 1: Install from Debian Repository (Recommended)

The APT package manager provides Composer directly from Debian repositories, ensuring automatic security updates and system integration. This is the simplest installation method for most users.

sudo apt install composer -y

Verify the installation by checking the Composer version.

composer --version
Composer version 2.x.x YYYY-MM-DD HH:MM:SS

The version number and date will vary based on your Debian release and whether updates have been applied.

Debian 11 and 12 disable the self-update command for APT-installed Composer. Use sudo apt install --only-upgrade composer to update to the latest packaged version. Debian 13 (Trixie) enables composer self-update for APT installations.

Method 2: Install Using Official Installer

The official Composer installer downloads the latest upstream release and provides full control over installation location and version management. This method is ideal when you need features not yet available in Debian repositories.

Download Composer Installer

Download the official installer script, which verifies integrity and handles the installation process.

This step requires curl, which may not be installed on minimal Debian systems. If the download fails, install curl first with sudo apt install curl -y.

curl -sS https://getcomposer.org/installer -o composer-setup.php

Verify Installer Integrity

Verify the installer’s SHA-384 hash to ensure the download was not corrupted or tampered with. The verification script checks the hash and removes the installer if verification fails.

HASH="$(curl -sS https://composer.github.io/installer.sig)"
php -r "if (hash_file('sha384', 'composer-setup.php') === '$HASH') { echo 'Installer verified' . PHP_EOL; } else { echo 'Installer corrupt' . PHP_EOL; unlink('composer-setup.php'); exit(1); }"
Installer verified

Install Composer Globally

Global installation places Composer in the system PATH, making it accessible to all users without requiring directory-specific configuration. Execute the installer with the following command.

sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer

The installer validates the download, extracts the archive, and places the composer binary in /usr/local/bin, which is included in the system PATH by default.

All settings correct for using Composer
Downloading...

Composer (version 2.x.x) successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer

Remove the installer script after successful installation.

rm composer-setup.php

Custom Installation Directory (Optional)

Advanced users managing multiple PHP environments may prefer installing Composer to a custom directory outside the standard PATH. This approach allows version-specific Composer binaries or project-isolated installations.

sudo php composer-setup.php --install-dir=/usr/local/composer --filename=phpcomposer

After installing to a custom directory, add that directory to your shell PATH so the composer command works from any location.

echo 'export PATH="$PATH:/usr/local/composer"' >> ~/.bashrc

Reload your shell configuration to apply the PATH change immediately.

source ~/.bashrc

Verify Composer Installation

Verify the installation by running the Composer command without arguments, which displays version information and available commands.

composer --version
Composer version 2.x.x YYYY-MM-DD HH:MM:SS
PHP version 8.x.x (/usr/bin/php8.x)
Run the "diagnose" command to get more detailed diagnostics output.

Manage Project Dependencies with Composer

Composer handles package installation, updates, and autoloading for PHP projects. The following examples demonstrate common workflows for creating projects and managing dependencies.

composer create-project --prefer-dist laravel/laravel myproject

This command creates a new Laravel project in a directory called “myproject.” Laravel is a popular PHP framework for building web applications. You can also use the composer command to install and update dependencies in an existing project. For PHP web applications, you may need a web server like Apache or Nginx to serve your application. To add a new package to your project, run the following command.

composer require <package-name>

To update a current package, you can run the following command.

composer update <package-name>

Update Composer

The update method depends on how you installed Composer. APT-installed Composer on Debian 11 and 12 does not support self-update, while manual installations and Debian 13 APT installations do.

Update APT-Installed Composer (Debian 11/12)

For APT-installed Composer on Debian 11 or 12, use the package manager to update.

sudo apt update
sudo apt install --only-upgrade composer

This upgrades Composer to the latest version available in Debian repositories, which may lag behind the upstream release.

Update Manual Installation or Debian 13

For manual installations or APT-installed Composer on Debian 13, use the self-update command to upgrade directly to the latest upstream release.

composer self-update
Upgrading to version 2.x.x (stable channel).
Downloading (100%)
Use composer self-update --rollback to return to version 2.x.x

Verify the update completed successfully.

composer --version
Composer version 2.x.x YYYY-MM-DD HH:MM:SS

If an update causes issues, roll back to the previous version with composer self-update --rollback.

Remove Composer

Removal steps depend on which installation method you used. Choose the appropriate section below based on your installation.

Remove APT-Installed Composer

If you installed Composer from the Debian repository, remove it using APT.

sudo apt remove composer
sudo apt autoremove

Verify removal by checking that the command no longer exists.

composer --version
bash: composer: command not found

Remove Manually Installed Composer

If you used the official installer, remove the Composer binary from the system PATH.

sudo rm /usr/local/bin/composer

If you installed Composer to a custom directory, adjust the removal path accordingly. For example, if you used /usr/local/composer/phpcomposer, remove it with the following command.

sudo rm /usr/local/composer/phpcomposer

Remove the custom PATH entry from your shell configuration if you added one.

nano ~/.bashrc

Delete the line containing export PATH="$PATH:/usr/local/composer", save the file, and reload your shell configuration.

source ~/.bashrc

Removing Composer does not delete project-specific vendor/ directories or composer.json files. If you want to clean up dependencies from specific projects, navigate to each project directory and remove the vendor/ folder manually.

Warning: The following command permanently deletes the vendor directory without confirmation. Ensure you are in the correct project directory before running this command.

rm -rf vendor/

Troubleshooting Composer

Command Not Found Error

If the composer command returns “command not found” after installation, the binary is not in your system PATH.

Verify the installation path matches the directory you specified during setup.

ls -la /usr/local/bin/composer
-rwxr-xr-x 1 root root XXXXXXX MMM DD HH:MM /usr/local/bin/composer

If the file exists but the command still fails, check your PATH variable.

echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

The output should include /usr/local/bin. If it does not, add it temporarily with the following command, then reload your shell configuration.

export PATH="$PATH:/usr/local/bin"

Permission Denied Errors

If Composer fails with “Permission denied” when creating or updating projects, check file ownership and permissions in your project directory.

Ensure your user account owns the project directory.

sudo chown -R $USER:$USER ~/myproject

If you installed Composer globally with sudo but need to run it without elevated privileges, verify the Composer binary has appropriate permissions.

sudo chmod 755 /usr/local/bin/composer

PHP Extension Requirements

Some Composer packages require specific PHP extensions that may not be installed by default. If Composer reports missing extensions during package installation, install them from the Debian repository.

Common extensions include php-xml, php-mbstring, and php-curl. Install them with the following command.

sudo apt install php-xml php-mbstring php-curl -y

After installing extensions, Composer should be able to install packages that previously failed. For database-driven projects, you may also need php-mysql or php-pgsql depending on your database. If you need database server setup, refer to our guides on installing MySQL on Debian or installing MariaDB on Debian. For additional PHP configuration guidance, refer to our guide on installing PHP on Debian.

Conclusion

You now have Composer managing PHP dependencies on Debian using composer require and composer update for your projects. Discover packages on Packagist, build Laravel or Symfony frameworks, or integrate Composer into production environments with WordPress on Apache or Apache and Nginx web servers.

Leave a Comment