PHP Composer is a powerful dependency manager for PHP applications, akin to npm and bower for JavaScript. By using a composer.json
file in your project’s root directory, you can manage dependencies, author information, licenses, and more efficiently.
With just a single command, you can install all the libraries you need to build a robust PHP application. This tutorial will guide you through installing Composer on Debian 11, with steps applicable to other Debian-based distributions.
Prerequisites
Before proceeding with the installation, ensure you have root access to a Linux shell, either on a native Linux installation or through a virtualized environment like VirtualBox on a Debian host.
Step 1. Updating the System
To start, open your terminal using Ctrl+Alt+T or search for terminal in the application menu. Execute the following commands to update your system:
sudo apt-get update
sudo apt-get upgrade
Once the update completes, we’ll proceed to install PHP and necessary modules that enable Composer functionality.
Execute these commands to install PHP and additional modules:
sudo apt install php -y
sudo apt install curl git unzip wget php-common php-zip php-cli php-xml php-gd php-mysql php-bcmath php-imap php-curl php-intl php-mbstring -y
Step 2. Install PHP Composer
With your system up-to-date, we can install PHP Composer. Start by downloading the Composer installer script with either curl or wget:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
Or
wget -O composer-setup.php https://getcomposer.org/installer
These commands prepare the composer-setup.php
file in your current directory. To install Composer globally, run:
php composer-setup.php --install-dir=/usr/local/bin --filename=composer
chmod +x /usr/local/bin/composer
This installation places Composer in the /usr/local/bin directory, accessible from anywhere in your terminal. Alternatively, for project-specific installations, execute:
cd /path/to/the/specific/project/php-application && mkdir -p bin
php composer-setup.php --install-dir=bin --filename=composer
chmod +x bin/composer
Replace /path/to/the/specific/project/ with your actual project path. To verify Composer installation, execute:
composer -v
You might get a warning about running Composer as root; proceed by typing ‘Y’ if needed. A successful installation outputs version information:
Step 3. Updating PHP Composer
To keep PHP Composer up-to-date, use its self-update feature:
composer self-update
If you’re already up-to-date, you’ll see a confirmation message:
Step 4. Testing the PHP Composer Installation
Let’s verify our Composer installation with a sample project. Create a new directory and navigate into it:
mkdir example-project && cd example-project
Choose a package from Packagist.org—for instance, sebastian/comparator
—and install it:
composer require sebastian/comparator
Composer downloads the package and its dependencies into the project directory:
Now, list the files in your project directory:
ls
This should display:
- composer.json – Contains project metadata.
- vendor – Houses downloaded packages and dependencies.
- composer.lock – Stores package version details.
Example output:
Conclusion
You’ve successfully installed and configured PHP Composer on Debian 11. Additionally, you learned how to update Composer. For questions or feedback, please leave a comment below.
Frequently Asked Questions
- What is PHP Composer?
Composer is a dependency manager for PHP, simplifying library management and installation. - Is Composer similar to npm?
Yes, Composer works similarly to npm for JavaScript, managing packages and dependencies. - Why should I use Composer?
It automates dependency management, ensuring your project uses compatible versions and installs updates seamlessly. - Can I install Composer locally for just one project?
Yes, use the local installation commands to set up Composer within a specific project directory. - How do I know if Composer is installed correctly?
Runcomposer -v
and verify the version output or any error messages for issues.