Skip to main content

How to Install a PHP Package with Composer

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Composer is PHP's package manager. composer require downloads a package into your project, records it in composer.json, and writes one file, vendor/autoload.php, that makes every installed package available to your code.

Syntax​

Syntax
composer require <vendor>/<package>[:<version>]
Syntax: in your script
require __DIR__ . '/vendor/autoload.php';
PartWhat it does
composer requireAdds a package to the project in the folder you're in, downloads it and records it.
<vendor>/<package>The package's name on Packagist, PHP's package registry, such as cocur/slugify.
:<version>Optional. A version constraint, such as :^4.8. Without it, Composer picks the newest version and writes a matching constraint.
require __DIR__ . '/vendor/autoload.php';Loads Composer's autoloader, which finds each package's classes the moment your code uses them. __DIR__ is the folder of the script itself.

The Example​

The example turns a sentence into a slug, the lowercase, hyphenated form used in web addresses, with cocur/slugify.

Prerequisites​

  • PHP and Composer. Check them with php --version and composer --version. This guide uses PHP 8.6.0 and Composer 2.11.3.

Step 1: Install the Package​

Terminal
mkdir slug-maker
cd slug-maker
composer require cocur/slugify
Output
./composer.json has been created
Running composer update cocur/slugify
Loading composer repositories with package information
Updating dependencies
Lock file operations: 1 install, 0 updates, 0 removals
- Locking cocur/slugify (v4.8.0)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
- Downloading cocur/slugify (v4.8.0)
- Installing cocur/slugify (v4.8.0): Extracting archive
Generating autoload files
No security vulnerability advisories found.
Using version ^4.8 for cocur/slugify

The commands are the same in zsh, bash and PowerShell. Reading the output from the top:

  • composer.json didn't exist, so Composer created it.
  • composer.lock, the lock file, now records the exact version, 4.8.0, so every later install gets the same one.
  • vendor, a new folder, holds the package, and vendor/autoload.php was generated.
  • No security vulnerability advisories found. Composer checked the installed packages against known security problems.

composer.json now lists the package:

composer.json
{
"require": {
"cocur/slugify": "^4.8"
}
}

^4.8 means 4.8 or any newer 4.x release, but never 5.0, which could change how the package works.

Step 2: Use It​

Create main.php:

main.php, which requires vendor/autoload.php, imports Cocur\Slugify\Slugify and echoes the slug of Hello World from PHP!

main.php
<?php

require __DIR__ . '/vendor/autoload.php';

use Cocur\Slugify\Slugify;

$slugify = new Slugify();
echo $slugify->slugify('Hello World from PHP!') . "\n";

use Cocur\Slugify\Slugify; lets the code write Slugify instead of the class's full name, and the autoloader loads the class when new Slugify() first needs it.

Terminal
php main.php
Output
hello-world-from-php

A zsh terminal where composer require cocur/slugify creates composer.json, locks and installs v4.8.0 and generates autoload files, and php main.php prints hello-world-from-php.

Common Mistakes​

MistakeWhat happensDo this instead
Leaving out require __DIR__ . '/vendor/autoload.php';PHP doesn't know the package's classesRequire the autoloader once, at the top of the script
Committing the vendor folderThe repository fills with code anyone can download againCommit composer.json and composer.lock, and add vendor/ to .gitignore
Running composer require outside the project folderComposer creates a composer.json wherever you arecd into the project first

Troubleshooting​

SymptomLikely causeFix
Class "Cocur\Slugify\Slugify" not foundThe autoloader wasn't loaded, or the package isn't installed hereRequire vendor/autoload.php, and run composer require in this project
Failed opening required '…/vendor/autoload.php'vendor doesn't exist in this projectRun composer install to recreate it from composer.lock

FAQ​

How do I install everything a project lists?​

Run composer install in the project's folder. It installs the exact versions in composer.lock, and creates vendor again on another computer.

How do I install a specific version?​

Add a constraint after a colon: composer require cocur/slugify:4.8.0 installs exactly that version.

Conclusion​

composer require <vendor>/<package> installs a package into vendor, records it in composer.json and fixes its exact version in composer.lock. One line, require __DIR__ . '/vendor/autoload.php';, makes every installed package available to your script.

Additional Resources​