How to use Composer and Packagist in PHP
By Flavio
Published · Updated
Tagged: PHP
Learn how to use Composer, the PHP package manager, to install packages like Carbon with composer require and autoload them via vendor/autoload.php.
Composer is the package manager of PHP.
It allows you to easily install packages into your projects. The packages come from Packagist, the public registry Composer uses by default. When you look for a library, search Packagist to find its exact name, like nesbot/carbon.
Install it on your machine (Linux/Mac or Windows) and once you’re done you should have a composer command available on your terminal.

Install a package
Now inside your project you can run composer require <lib> and it will be installed locally, for example let’s install the Carbon library that helps us work with dates in PHP
composer require nesbot/carbon
It will do some work:

Once it’s done, you will find some new things in the folder, composer.json that lists the new configuration for the dependencies:
{
"require": {
"nesbot/carbon": "^3.15"
}
}
composer.lock which is used to “lock” the versions of the package in time, so the exact same installation you have can be replicated on another server, and the vendor folder, that contains the library just installed, and its dependencies.
Use it in your code
Now in the index.php file we can add this code at the top:
<?php
require __DIR__ . '/vendor/autoload.php';
use Carbon\Carbon;
and then we can use the library!
echo Carbon::now();

See? We didn’t have to manually download a package from the internet, install it somewhere.. it was all fast, quick, and well organized.
The require __DIR__ . '/vendor/autoload.php'; line is what enables autoloading. Remember when we talked about require_once() and include_once()? This solves all of that, we don’t need to manually search for the file to include, we just use the use keyword to import the library into our code.
__DIR__ is the folder of the current file, so the path stays correct even when you run the script from another folder. If you forget to run Composer first, or the vendor folder is missing, PHP stops with an error like this:
Warning: require(/path/to/project/vendor/autoload.php): Failed to open stream: No such file or directory
Run composer install and it’s fixed.
What ^3.15 means
Composer wrote ^3.15 in composer.json, not an exact version. That’s a version constraint. The caret allows any update that shouldn’t break your code, following semantic versioning: ^3.15 means at least 3.15 and below 4.0.
The other common one is the tilde. ~3.15 also means below 4.0, but ~3.15.1 only allows patch releases, below 3.16.0. When you need something specific, you can pass the constraint yourself:
composer require nesbot/carbon:^3.0
composer install vs composer update
This is the part that confuses people at first, and it’s the most important one.
composer installreadscomposer.lockand installs those exact versions. It doesn’t look for newer ones. Use it when you clone a project, on your server, and in CI.composer updateignores the lock file, finds the newest versions that match the constraints incomposer.json, installs them, and writes the new versions tocomposer.lock.
So commit both composer.json and composer.lock to git. Then everyone on the project and every server gets the same versions. Don’t commit the vendor folder, add it to .gitignore:
/vendor/
To update a single package instead of all of them, pass its name:
composer update nesbot/carbon
If that package needs a newer version of one of its own dependencies, add -W (--with-all-dependencies) so Composer can update those too.
Development-only packages
Some packages, like a testing framework, are only needed while you develop. Install them with --dev:
composer require --dev phpunit/phpunit
They go in a separate require-dev section of composer.json. On the production server, skip them:
composer install --no-dev --optimize-autoloader
--optimize-autoloader (or -o) builds a class map so the autoloader finds classes faster. More on setting up a server in deploying PHP applications.
Autoload your own classes
The same autoloader can load your code, so you don’t need a require for each of your files. Add an autoload section to composer.json that maps a namespace to a folder:
{
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
"require": {
"nesbot/carbon": "^3.15"
}
}
The namespace needs the double backslash at the end. This is called PSR-4: the class App\Cart lives in src/Cart.php, App\Models\User in src/Models/User.php. The file name must match the class name, including uppercase letters.
<?php
namespace App;
class Cart
{
public function total(array $prices): float
{
return array_sum($prices);
}
}
After you change the autoload section, regenerate the autoloader:
composer dump-autoload
Now index.php can use the class:
<?php
require __DIR__ . '/vendor/autoload.php';
use App\Cart;
$cart = new Cart();
echo $cart->total([10, 5.5]); // 15.5
You only need dump-autoload when you edit the autoload section. New classes you add under src/ later are found without it. If you get a “Class not found” error, check that the namespace, the folder and the file name all match.
Other commands I use
composer outdated --direct # packages with newer versions available
composer remove nesbot/carbon # uninstall a package
composer show # list installed packages and versions
composer audit # check installed packages for known security issues
composer remove also takes the package out of composer.json and composer.lock, so don’t just delete its folder in vendor.
To start a new project from scratch, composer init asks you a few questions and creates composer.json for you.
Want me to talk about your product? You can sponsor this site.