How to Use Composer in PHP: Complete Beginner’s Guide (2026)

By Lyndon Bermoy

Updated on:

How to Use Composer in PHP: Complete Beginner's Guide (2026)

Composer is the dependency manager for PHP. It lets you declare the libraries your project depends on and manages their installation and updates. If you use Laravel, Symfony, or any modern PHP framework, you are already using Composer — it is how those frameworks are installed. Understanding how to use it directly makes you significantly more capable as a PHP developer.

What Composer Does

Before Composer (pre-2012), PHP developers manually downloaded libraries, unzipped them into project folders, and wrote custom include paths. Updating a library meant downloading the new version and hoping it did not break anything. Managing a project with ten dependencies was a maintenance nightmare.

Composer solves this with a declarative approach: you specify what packages you need and which versions are acceptable in a composer.json file. Composer downloads the exact right versions, handles their own dependencies automatically, and generates an autoloader so you can use any installed package with a simple use statement.

Step 1: Install Composer

Windows

  1. Download the Composer-Setup.exe installer from getcomposer.org
  2. Run the installer — it detects your PHP installation automatically
  3. After installation, open Command Prompt and run: composer --version
  4. You should see something like: Composer version 2.8.x

macOS

# Install via Homebrew (recommended):
brew install composer

# Verify:
composer --version

Linux

# Download and install:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php
php -r "unlink('composer-setup.php');"
sudo mv composer.phar /usr/local/bin/composer

# Verify:
composer --version

Step 2: Create Your First Project with Composer

Open a terminal, create a project folder, and initialize Composer:

mkdir my-php-project
cd my-php-project
composer init

Composer asks a series of questions:

  • Package name: yourname/my-project (lowercase, no spaces)
  • Description: A brief description
  • Author: Your name and email
  • Minimum stability: Press Enter for default (stable)
  • Package type: Press Enter (project)
  • License: Press Enter (MIT is common)
  • Dependencies: You can add these later
READ ALSO:   PHP MySQL: Build a Simple Contact Form

This creates a composer.json file in your project folder.

Step 3: Install Your First Package

Let’s install a practical package — Carbon, the popular PHP date/time library used heavily in Laravel:

composer require nesbot/carbon

Composer downloads Carbon and all its dependencies into a vendor/ folder and creates a composer.lock file that records the exact version installed.

Your project structure now looks like:

my-php-project/
├── vendor/             ← Composer downloads packages here
│   ├── autoload.php    ← The autoloader — you only need to include this
│   └── nesbot/carbon/  ← The Carbon package
├── composer.json       ← Your dependency declarations
├── composer.lock       ← Exact versions Composer installed
└── index.php           ← Your code

Step 4: Use the Installed Package

To use any Composer-installed package, include the autoloader at the top of your PHP file:

<?php
require_once __DIR__ . '/vendor/autoload.php';

use Carbon\Carbon;

$now  = Carbon::now();
$next = Carbon::now()->addDays(7);

echo "Today: " . $now->format('Y-m-d') . PHP_EOL;
echo "Next week: " . $next->format('Y-m-d') . PHP_EOL;
echo "Days until next week: " . $now->diffInDays($next) . PHP_EOL;

One require_once line — and every Composer package you have installed becomes available with use statements. No manual includes, no path management.

Understanding composer.json

{
    "name": "yourname/my-project",
    "description": "My PHP project",
    "require": {
        "php": ">=8.2",
        "nesbot/carbon": "^3.0",
        "vlucas/phpdotenv": "^5.6"
    },
    "require-dev": {
        "phpunit/phpunit": "^11.0"
    },
    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    }
}
  • require — packages needed in production
  • require-dev — packages only needed for development (testing, debugging tools)
  • autoload — maps your own PHP namespaces to folders (more on this below)
  • ^3.0 — the caret means “compatible with 3.x” (allows 3.1, 3.2 but not 4.0)

Autoloading Your Own Code

Composer’s autoloader works for your own classes too. Configure psr-4 autoloading in composer.json:

"autoload": {
    "psr-4": {
        "App\\": "src/"
    }
}

Then run:

composer dump-autoload

Now any class in your src/ folder with the App\ namespace is automatically available:

// src/Database/Connection.php
namespace App\Database;

class Connection {
    public function __construct(private string $dsn) {}
    public function connect(): \PDO {
        return new \PDO($this->dsn);
    }
}
// index.php
require_once '/home/campcode/public_html' . '/vendor/autoload.php';

use App\Database\Connection;

$conn = new Connection('mysql:host=localhost;dbname=myapp');
$pdo  = $conn->connect();

Essential Composer Commands

# Install all packages listed in composer.json:
composer install

# Install a new package and add it to composer.json:
composer require vendor/package-name

# Install a package for development only:
composer require --dev phpunit/phpunit

# Update all packages to latest compatible versions:
composer update

# Update a specific package:
composer update nesbot/carbon

# Remove a package:
composer remove nesbot/package-name

# Show all installed packages:
composer show

# Regenerate the autoloader (after editing composer.json autoload section):
composer dump-autoload

# Install without development packages (for production):
composer install --no-dev --optimize-autoloader

The composer.lock File

The composer.lock file records the exact version of every package installed. This is crucial for consistency:

  • Commit composer.lock to Git — when teammates (or your production server) run composer install, they get exactly the same package versions you used
  • Do NOT commit the vendor/ folder — add vendor/ to your .gitignore. The vendor folder can be regenerated at any time with composer install
# .gitignore
vendor/

Finding Packages

Packagist.org is the main Composer package repository. Search for any functionality and find the package name to use with composer require. Popular packages for PHP development:

  • vlucas/phpdotenv — load .env files for configuration
  • nesbot/carbon — date/time manipulation
  • guzzlehttp/guzzle — HTTP client for API calls
  • monolog/monolog — logging
  • ramsey/uuid — UUID generation
  • phpmailer/phpmailer — sending emails
  • league/flysystem — filesystem abstraction (local, S3, etc.)
READ ALSO:   PHP Headers Already Sent Error Fix

Composer on Shared Hosting

Composer requires SSH access to run. If your hosting plan does not include SSH:

  1. Run composer install locally on your machine
  2. Upload the entire vendor/ folder along with your code via FTP
  3. This is not ideal (vendor is large) but works for shared hosting without SSH

Hostinger Business, SiteGround, and A2 Hosting all include SSH access — enabling proper Composer use on the server.

Conclusion

Composer is a foundational PHP tool in 2026 — not optional for modern PHP development. It takes 10 minutes to install and immediately expands your access to thousands of production-quality packages. The workflow is simple: composer require to add a package, require_once 'vendor/autoload.php' in your PHP file, and use any package with a use statement. Commit composer.json and composer.lock to Git; add vendor/ to .gitignore. That is all you need to know to get started.

Next Step for Your Project Journey

Pick your next action to keep building: download complete source code, continue with practical tutorials, or explore more project systems.

Explore Source Code Continue Tutorials Final Year Projects

Leave a Comment