Configure PHP Composer for dependency management and autoloading

Intermediate 25 min May 12, 2026 49 views
Ubuntu 24.04 Debian 12 AlmaLinux 9 Rocky Linux 9

Set up PHP Composer with global configuration, PSR-4 autoloading, and dependency management workflows for modern PHP development across Ubuntu, Debian, AlmaLinux, and Rocky Linux distributions.

Prerequisites

  • Root or sudo access
  • Basic PHP knowledge
  • Command line familiarity

What this solves

PHP Composer is the standard dependency manager for PHP that handles package installation, autoloading, and version management. This tutorial configures Composer with global settings, implements PSR-4 autoloading for organized code structure, and establishes dependency management workflows for production-ready PHP applications.

Step-by-step installation

Install PHP and required extensions

Composer requires PHP with specific extensions for dependency resolution and package management functionality.

sudo apt update
sudo apt install -y php php-cli php-curl php-mbstring php-xml php-zip php-intl php-gd unzip curl
sudo dnf update -y
sudo dnf install -y php php-cli php-curl php-mbstring php-xml php-zip php-intl php-gd unzip curl

Download and install Composer

Download the official Composer installer and verify its integrity before installation.

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
sudo chmod +x /usr/local/bin/composer

Verify Composer installation

Check that Composer is properly installed and accessible from the command line.

composer --version
composer diagnose

Configure global Composer settings

Set up global configuration for optimal performance and security.

composer config --global repo.packagist composer https://packagist.org
composer config --global process-timeout 2000
composer config --global cache-ttl 86400

Enable memory optimization

Configure Composer to handle large dependency trees efficiently.

composer config --global optimize-autoloader true
composer config --global classmap-authoritative true
composer config --global apcu-autoloader true

Set up PSR-4 autoloading

Create project structure

Set up a PHP project with proper PSR-4 directory structure for organized code management.

mkdir -p /var/www/myproject/{src,tests,public,config}
cd /var/www/myproject

Initialize Composer project

Create a composer.json file with project metadata and PSR-4 autoloading configuration.

composer init --name="example/myproject" --description="Example PHP project" --type="project" --license="MIT" --no-interaction

Configure PSR-4 autoloading

Edit composer.json to define namespace mapping for automatic class loading.

{
    "name": "example/myproject",
    "description": "Example PHP project",
    "type": "project",
    "license": "MIT",
    "autoload": {
        "psr-4": {
            "App\\": "src/",
            "App\\Controllers\\": "src/Controllers/",
            "App\\Models\\": "src/Models/",
            "App\\Services\\": "src/Services/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "require": {
        "php": ">=8.1"
    },
    "require-dev": {
        "phpunit/phpunit": "^10.0"
    },
    "config": {
        "optimize-autoloader": true,
        "classmap-authoritative": true,
        "apcu-autoloader": true
    }
}

Generate autoloader files

Create the autoloader files that enable automatic class loading based on PSR-4 mapping.

composer dump-autoload --optimize

Create example classes

Create sample classes to demonstrate PSR-4 autoloading functionality.

Create model example

Add a model class to demonstrate namespace organization.

name = $name;
        $this->email = $email;
    }

    public function getName(): string
    {
        return $this->name;
    }

    public function getEmail(): string
    {
        return $this->email;
    }
}

Create bootstrap file

Set up an entry point that loads the Composer autoloader and demonstrates class usage.

index() . "\n";
echo "User: " . $user->getName() . " (" . $user->getEmail() . ")\n";

Implement dependency management

Install production dependencies

Add commonly used packages for modern PHP development.

composer require monolog/monolog vlucas/phpdotenv symfony/http-foundation

Install development dependencies

Add tools for testing, debugging, and code quality analysis.

composer require --dev phpunit/phpunit phpstan/phpstan squizlabs/php_codesniffer

Configure scripts for development workflow

Add custom scripts to composer.json for common development tasks.

{
    "scripts": {
        "test": "phpunit",
        "test-coverage": "phpunit --coverage-html coverage",
        "analyse": "phpstan analyse src --level=7",
        "cs-check": "phpcs src --standard=PSR12",
        "cs-fix": "phpcbf src --standard=PSR12",
        "dev-setup": [
            "composer install",
            "composer dump-autoload --optimize"
        ],
        "prod-deploy": [
            "composer install --no-dev --optimize-autoloader",
            "composer dump-autoload --classmap-authoritative --no-dev"
        ]
    }
}

Set up environment-specific configurations

Configure different dependency sets for development and production environments.

composer config platform.php 8.1
composer config preferred-install dist
composer config sort-packages true

Create production optimization script

Set up a deployment script for production environment optimization.

#!/bin/bash

Production deployment script

echo "Starting production deployment..."

Update dependencies

composer install --no-dev --optimize-autoloader --no-interaction

Generate optimized autoloader

composer dump-autoload --classmap-authoritative --no-dev

Clear any development caches

php -r "opcache_reset();" echo "Production deployment completed."
chmod +x deploy.sh

Configure security and performance settings

Set up secure package repositories

Configure Composer to use HTTPS and verify package signatures.

composer config --global secure-http true
composer config --global disable-tls false
composer config --global cafile /etc/ssl/certs/ca-certificates.crt

Configure cache and performance settings

Optimize Composer for better performance with caching and parallel downloads.

composer config --global cache-dir ~/.composer/cache
composer config --global cache-files-ttl 86400
composer config --global cache-repo-ttl 86400
composer config --global htaccess-protect false

Set proper file permissions

Configure secure file permissions for the project directory and Composer cache.

sudo chown -R www-data:www-data /var/www/myproject
sudo find /var/www/myproject -type d -exec chmod 755 {} \;
sudo find /var/www/myproject -type f -exec chmod 644 {} \;
sudo chmod +x /var/www/myproject/deploy.sh
Never use chmod 777. It gives every user on the system full access to your files. Instead, fix ownership with chown and use minimal permissions like 755 for directories and 644 for files.

Verify your setup

composer --version
composer validate
composer show --installed
composer diagnose
php public/index.php

Check that autoloading works correctly:

cd /var/www/myproject
composer dump-autoload --optimize
php -r "require 'vendor/autoload.php'; echo 'Autoloader works!\n';"

Common issues

SymptomCauseFix
Class not found errorsIncorrect PSR-4 mapping or missing autoload dumpcomposer dump-autoload --optimize
Memory limit exceededLarge dependency resolutionphp -d memory_limit=2G composer install
SSL certificate errorsOutdated CA certificatesUpdate system certificates and set composer config --global secure-http true
Permission denied on vendor/Incorrect directory ownershipsudo chown -R $USER:$USER vendor/
Composer commands hangNetwork timeout issuescomposer config --global process-timeout 3000
Autoloader not optimizedMissing optimization flagscomposer install --optimize-autoloader --classmap-authoritative

Next steps

Running this in production?

Want this handled for you? Setting this up once is straightforward. Keeping it patched, monitored, backed up and performant across environments is the harder part. See how we run infrastructure like this for European teams.

Automated install script

Run this to automate the entire setup

Need help?

Don't want to manage this yourself?

We handle managed cloud infrastructure for businesses that depend on uptime. From initial setup to ongoing operations.