Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

How to Install and Use Composer on Debian

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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 with sudo apt install php-cli php-mbstring unzip
  • Internet access to download Composer and packages

Step-by-Step

  1. Update your package list – Open a terminal and run sudo apt update to refresh the package index. This ensures you get the latest versions of any dependencies.
  2. Install required PHP extensions – Composer needs the mbstring extension and unzip to 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 need php-xml and php-curl – install those too if unsure.
  3. Download the Composer installer – Use curl to fetch the official installer script: curl -sS https://getcomposer.org/installer -o composer-setup.php. If curl isn't installed, install it with sudo apt install curl first.
  4. 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');" and php -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”.
  5. Install Composer globally – Run sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer. This places the composer executable in your system PATH, so you can run it from anywhere.
  6. Remove the installer files – Clean up with rm composer-setup.php composer-setup.sig.
  7. Verify the installation – Run composer --version. You should see output like Composer version 2.7.1 (or similar). If you see a “command not found” error, log out and back in, or run source ~/.bashrc to refresh your PATH.
  8. Create a new PHP project – Make a directory for your project: mkdir ~/myproject && cd ~/myproject. Then run composer 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 type MIT or leave it. It will also ask for dependencies – you can skip that for now by pressing Enter.
  9. Add a dependency – To install a package, use composer require. For example, to install the popular monolog/monolog logging library, run: composer require monolog/monolog. Composer will resolve the latest compatible version, download it, and update the composer.json and composer.lock files. The package files go into the vendor/ directory.
  10. 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:
    <?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";
    Run it with php index.php and you should see a log file created.
  11. Update dependencies – When you want to update all packages to their latest versions within your version constraints, run composer update. This will modify the composer.lock file. To install exactly what's in the lock file (e.g., on a production server), use composer install.

Common Mistakes

  • Forgetting to install required PHP extensions – Composer will complain about missing mbstring or zip extensions. Install them as shown in step 2.
  • Running Composer as root – Avoid using sudo composer for project commands. It can cause permission issues with the vendor directory and may execute scripts with root privileges. Use a normal user account.
  • Not using a composer.lock file – The lock file ensures that everyone on the project uses the exact same package versions. Always commit composer.lock to 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 update in production – In production, you should use composer install to install the exact versions from the lock file. Running update can 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 global vendor/bin directory is usually at ~/.config/composer/vendor/bin – add it to your PATH if needed.
  • Check for outdated packages – Run composer outdated to 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. Run composer audit regularly.
  • 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/bin directory isn't in your PATH, or you need to restart your shell. Run echo $PATH to check. If /usr/local/bin is missing, add it to your .bashrc or .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 your php.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 update or set it permanently in your php.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-certificates and update them.
  • “Failed to decode response: zlib_decode(): data error” – This is often a network issue or a corrupted cache. Run composer clear-cache and 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.