Managing PHP Dependencies with Composer

Updated onbyAlan Morel
Managing PHP Dependencies with Composer

Composer is a multi-platform dependency management tool for PHP. It manages your project's dependencies and allows you to easily install and update them from Packagist, the main Composer repository where public PHP packages are aggregated in one location.

Composer logo

Composer will install the dependencies of your PHP project in the vendor directory, allowing you to use them in your application.

Install Composer

You will need PHP already installed on your system. If you do not have PHP downloaded, you can download it from PHP.net.

Windows

To install Composer on windows, use their Windows installer. Simply run the Composer-Setup.exe and it will install Composer for you.

macOS

You can install Composer using the Homebrew package manager.

BASH
brew install composer

Then to enable Composer globally, run:

BASH
mv composer.phar /usr/local/bin/composer

Linux

You can install Composer in the command line by running:

BASH
curl -sS https://getcomposer.org/installer -o composer-setup.php

Then to enable Composer globally, run:

BASH
mv composer.phar /usr/local/bin/composer

Initialize Composer

Before you can use Composer, ensure you installed it correctly. You can do this by checking the Composer version:

BASH
composer --version

If that returns a number, you have successfully installed Composer.

To get started, initialize your project at the root directory:

BASH
composer init

After the prompts, you will have a composer.json file in your project root directory. This file contains the information about your project, including the project's dependencies and their versions.

Now that you have a composer.json file, you can install your dependencies.

BASH
composer install

This will download the dependencies from the Packagist repository and install them in your vendor directory. This will also create a composer.lock file. This file keeps track of the exact versions of the dependencies you have installed.

Inside your vendor directory, you will find the dependencies you have installed and a file called autoload.php, which is the auto-loader for your project.

To use the dependencies you have installed, you will need to include the autoload.php file in your project.

PHP
require_once 'vendor/autoload.php';

Your dependencies are now available in your project.

Adding a Dependency

You can add a dependency to your composer.json file by adding a new "require" key to the file or using the composer require command.

BASH
composer require vendor/package:version

For example:

BASH
composer require symfony/console:6.0.0

This will add the symfony/console package to your composer.json file.

JSON
{ "require": { "symfony/console": "6.0.0" } }

Updating Dependency Versions

Composer can search for the latest version of your dependencies and update them if they are out of date. To do so, run:

BASH
composer update

If a dependency has been updated, the new version will be shown in the terminal and the composer.lock file will be updated accordingly.

You can also update a specific dependency by specifying the dependency's name and version:

BASH
composer update symfony/console:6.0.0

This will only update the symfony/console package to version 6.0.0 and leave the other dependencies untouched.

Removing Dependencies

With Composer, you can remove dependencies from your project that you no longer need or want.

BASH
composer remove vendor/package

This will remove the vendor/package package from your composer.json file and the composer.lock file.

Composer.lock File

The composer.lock file is useful for specifying the exact versions of the dependencies you have installed. When another developer clones your project, they can use the composer.lock file to know exactly what versions of the dependencies they should install. This is why any time you run a command that modifies your dependencies, Composer will automatically update the composer.lock file.

Conclusion

Composer is a great tool for managing your PHP project's dependencies. It allows you to install and update your dependencies from a central repository, and it will keep track of the versions of the dependencies you have installed. It also allows you to remove dependencies that you no longer need.

Hopefully, you will find Composer useful for your PHP project. Thanks for reading!

Resources

Recommended Tutorial »
Copyright © 2017 - 2024 Sabe.io. All rights reserved. Made with ❤ in NY.