What’s a PHP composer? How to use PHP composer in your PHP Application? What are the benefits of using composer in your application?
PHP Composer is a dependency manager. It manages the dependency of PHP packages in your application. Using composer you can pull the required library and dependencies and manage them in one place. The concept of a composer is similar to npm in node.js and gem in Ruby.
Let’s first understand what is a dependency.
What’s a Dependency?
A dependency is an external library or a component which is required to run our web application.
For example – It might be OAuth2 library, facebook PHP-SDK etc.
Why PHP Composer
So you might be thinking, why I need to use composer for managing package dependency. What kind of problems composer is going to solve? A Composer will solve these three important problems.
- Dependency resolution for packages.
- Autoloading packages.
- Update all your existing packages
Let’s understand, what is dependency resolution.
Suppose you have downloaded any PHP framework or library. Now the framework you have downloaded depends on an external library such as guzzle and JSON-schema etc to work. Similarly, guzzle needs Symfony’s event-dispatcher and the list goes on. Composer handles these dependency resolutions automatically.
How to Install PHP Composer
I assume you have installed PHP in your system. To download and install-composer, go to the terminal and type following commands.
$ curl -s https://getcomposer.org/installer | php
If you are getting curl error then first install curl in your system.
In next step, move composer.phar to the bin directory. After moving this file to bin directory you can access composer globally on your system
$ sudo mv composer.phar /usr/local/bin/composer
Now installation is complete. You can use composer to manage and update package dependency. To check composer available commands, simply type composer in your terminal and hit enter.
It displays all the available composer commands.
about Short information about Composer-Command
archive Create an archive of this package
config Set config options
create-project Create new project from a package into given directory.
depends Shows which packages depend on the given package
diagnose Diagnoses the system to identify common errors.
dump-autoload Dumps the autoloader
dumpautoload Dumps the autoloader
global Allows running commands in the global dir ($COMPOSER_HOME).
help Displays help for a command
init Creates a basic composer.json file in current directory.
install Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json.
licenses Show information about licenses of dependencies
list Lists commands
require Adds required packages to your composer.json and installs them
run-script Run the scripts defined in composer.json.
search Search for packages
self-update Updates composer.phar to the latest version.
selfupdate Updates composer.phar to the latest version.
show Show information about packages
status Show a list of locally modified packages
update Updates your dependencies to the latest version according to composer.json, and updates the composer.lock file.
validate Validates a composer.json
How to Install PHP Composer on Windows
If you are looking installation guide for windows operating system, then check the installation manual on Composer Website.
How to Use Composer
We have installed composer successfully. Let’s pull some packages through composer.
First, create a composer.json file in the root of your project directory. Suppose, I want to download slim and twig packages for my project. I included the vendor name and version of slim and twig in a composer.json file.
After that run install command and composer will install the required dependency.
# composer install
Loading repositories with package information
Installing dependencies (including require-dev)
- Installing slim/slim (2.2.0)
- Installing twig/twig (v1.13.2)
Writing lock file
Generating autoload files
So now you are thinking from where you get the vendor name and version for composer.json file. You can get this information from Packagist .
How to Add Another Dependency on a Existing Composer File
To add a dependency on existing composer.json file, open this file and add another require statement with a dependency name. The other easiest approach is to run composer require command.
composer require filp/whoops
Using version ^2.1 for filp/whoops
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Installing filp/whoops (2.1.3)
This command will pull a dependency and also it updates your composer.json file.
How to Update Packages
The composer update command updates all the packages mentioned in your composer.json file to a newer version.
composer update --no-dev
Using Packagist to Define Dependency on Composer.json file
Packagist is the main repository. It aggregates all sorts of PHP packages that are installable with Composer.
To search the package, just type the package name. Once you click on a package , you’ll get the vendor name and version. Put the package vendor name and version in a composer.json file.
For example, I am searching a PHP framework, In our result we get a list of a framework based on the no. of downloads and rating. After choosing the framework, it will tell you how to use require inside composer.json file. For installing laravel, i need to add “laravel/framework”: “4.2.*@dev” in a composer.json file with require.
Now the package/library you require is installed. You can check vendor directory.
drwxrwxr-x 3 raj raj 4.0K Dec 27 20:03 slim/
drwxrwxr-x 3 raj raj 4.0K Dec 27 20:04 twig/
drwxrwxr-x 2 raj raj 4.0K Dec 27 20:05 composr/
-rw-rw-r-- 1 raj raj 183 Dec 27 20:05 autoload.php
To use them in your project composer-comes with Autoload file.
Once the autoload file is included, you can use the library in your project.
I hope this article helps you to understand, How to manage package dependency in PHP. Why we need dependency management tool. The main benefits of a dependency management tool is to provide consistency and ease of use.