How to Install and Use Composer on Debian
In this article
Composer is the standard dependency manager for PHP. It lets you declare the libraries your project needs and installs them for you, keeping versions consistent and making updates simple. This guide walks you through installing Composer on Debian (including Ubuntu and other derivatives) and using it for a basic PHP project. You'll need a Debian-based system with sudo access and PHP already installed (PHP 7.2.5 or later is recommended for current Composer versions).
What You'll Need
- A Debian-based Linux distribution (Debian 10+, Ubuntu 20.04+, or similar)
- A user account with sudo privileges
- PHP installed (check with
php -v) – if not, install it withsudo apt install php-cli php-mbstring unzip - Internet access to download Composer and packages
Step-by-Step
- Update your package list – Open a terminal and run
sudo apt updateto refresh the package index. This ensures you get the latest versions of any dependencies. - Install required PHP extensions – Composer needs the
mbstringextension andunzipto work properly. Install them with:sudo apt install php-cli php-mbstring unzip. If you plan to use Composer with a web framework, you may also needphp-xmlandphp-curl– install those too if unsure. - Download the Composer installer – Use
curlto fetch the official installer script:curl -sS https://getcomposer.org/installer -o composer-setup.php. Ifcurlisn't installed, install it withsudo apt install curlfirst. - Verify the installer's integrity – Composer publishes a SHA-384 hash for each release. You can check it by comparing the hash of the downloaded file with the one on the Composer website. For the latest version, visit composer.github.io/pubkeys.html and copy the current hash. Then run:
php -r "copy('https://composer.github.io/installer.sig', 'composer-setup.sig');"andphp -r "echo hash_equals(file_get_contents('composer-setup.sig'), hash_file('SHA384', 'composer-setup.php')) ? 'Installer verified' : 'Installer corrupt';". You should see “Installer verified”. - Install Composer globally – Run
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer. This places thecomposerexecutable in your system PATH, so you can run it from anywhere. - Remove the installer files – Clean up with
rm composer-setup.php composer-setup.sig. - Verify the installation – Run
composer --version. You should see output likeComposer version 2.7.1(or similar). If you see a “command not found” error, log out and back in, or runsource ~/.bashrcto refresh your PATH. - Create a new PHP project – Make a directory for your project:
mkdir ~/myproject && cd ~/myproject. Then runcomposer init. This interactive command asks for project details (name, description, author, etc.). You can press Enter to accept defaults for most prompts. When it asks for “Package Type”, choose “project” or leave blank. For “License”, you can typeMITor leave it. It will also ask for dependencies – you can skip that for now by pressing Enter. - Add a dependency – To install a package, use
composer require. For example, to install the popularmonolog/monologlogging library, run:composer require monolog/monolog. Composer will resolve the latest compatible version, download it, and update thecomposer.jsonandcomposer.lockfiles. The package files go into thevendor/directory. - Use the installed package – Create a PHP file (e.g.,
index.php) and include Composer's autoloader at the top:require 'vendor/autoload.php';. Then you can use the package's classes. For Monolog, a simple example is:
Run it with<?php require 'vendor/autoload.php'; use Monolog\Logger; use Monolog\Handler\StreamHandler; $log = new Logger('name'); $log->pushHandler(new StreamHandler('app.log', Logger::WARNING)); $log->warning('Foo'); echo "Log written.\n";php index.phpand you should see a log file created. - Update dependencies – When you want to update all packages to their latest versions within your version constraints, run
composer update. This will modify thecomposer.lockfile. To install exactly what's in the lock file (e.g., on a production server), usecomposer install.
Common Mistakes
- Forgetting to install required PHP extensions – Composer will complain about missing
mbstringorzipextensions. Install them as shown in step 2. - Running Composer as root – Avoid using
sudo composerfor project commands. It can cause permission issues with thevendordirectory and may execute scripts with root privileges. Use a normal user account. - Not using a
composer.lockfile – The lock file ensures that everyone on the project uses the exact same package versions. Always commitcomposer.lockto version control. - Ignoring version constraints – When you specify a dependency like
monolog/monolog: ^2.0, Composer will only install versions that match that constraint. Be careful with wildcard constraints like*– they can lead to unexpected updates. - Using
composer updatein production – In production, you should usecomposer installto install the exact versions from the lock file. Runningupdatecan introduce breaking changes.
Tips
- Use a global Composer for tools – Some tools (like PHPUnit or Laravel Installer) are installed globally with
composer global require. This keeps them separate from project dependencies. The globalvendor/bindirectory is usually at~/.config/composer/vendor/bin– add it to your PATH if needed. - Check for outdated packages – Run
composer outdatedto see which dependencies have newer versions available. - Use
composer audit– Composer 2.4+ includes a security audit command that checks your dependencies for known vulnerabilities. Runcomposer auditregularly. - Keep Composer itself updated – To update Composer to the latest version, run
composer self-update.
Troubleshooting
- “Composer command not found” – This usually means the
/usr/local/bindirectory isn't in your PATH, or you need to restart your shell. Runecho $PATHto check. If/usr/local/binis missing, add it to your.bashrcor.profile. - “The zip extension and unzip/7z commands are missing” – Install them with
sudo apt install php-zip unzip. Make sure the extension is enabled in yourphp.ini(usually it is automatically after install). - “PHP Fatal error: Allowed memory size exhausted” – Composer can run out of memory on large projects. Increase the memory limit temporarily with
php -d memory_limit=-1 composer updateor set it permanently in yourphp.ini. - “SSL certificate problem” – If Composer fails to download packages due to SSL errors, your CA certificates may be outdated. Run
sudo apt install ca-certificatesand update them. - “Failed to decode response: zlib_decode(): data error” – This is often a network issue or a corrupted cache. Run
composer clear-cacheand try again. If it persists, check your proxy settings. - “The process has been signaled with signal 9” – This usually means the system ran out of memory. Close other applications, or increase memory limit as above.
Frequently Asked Questions
Do I need to install Composer separately for each project?
No, you install Composer once globally. Each project has its own composer.json and vendor directory, but the Composer tool itself is shared.
What is the difference between composer install and composer update?
composer install reads the composer.lock file and installs the exact versions listed there. composer update resolves dependencies again and updates the lock file to the latest versions that match your constraints. Use install for production and update when you want to upgrade.
How do I uninstall Composer?
Simply delete the executable: sudo rm /usr/local/bin/composer. You may also want to remove the global composer directory (~/.config/composer) if you don't need it.
Can I use Composer with PHP 7.0 or older?
Composer 2.x requires PHP 7.2.5 or higher. If you're on an older PHP version, you can install Composer 1.x, but it's no longer maintained. It's better to upgrade PHP.
What is the vendor directory?
It's where Composer installs all the packages (libraries) your project depends on. You should not edit files in vendor manually – they are managed by Composer and can be regenerated with composer install.
How do I require a specific version of a package?
Use composer require vendor/package:1.2.3 or specify a constraint like ^1.2 (meaning any 1.x version >= 1.2). You can also edit composer.json directly and then run composer update vendor/package.