Blog de programación, errores, soluciones

Chose Language:
Author: Admin/Publisher |finished | checked

Composer -the PHP dependency manager


¿What is composer?

Let’s allow composer creators to define their program, I Thought is the best way to understand it in a few lines what it is.

Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.

¿What is a dependency?

Is an application or library created by us or third parties(other creators) that we can add to our project with composer.

Let’s see an example of this, if we want to send emails in our app we probably use PHPMailer and dbal for that we can manage this with composer. With this, we are not coping and pasting part of libraries to make it work. When you are using composer, all these dependencies go to a folder called vendor that will have all the necessary to run these dependencies or modules.



To install composer on Windows, it’s simple, you need to download the installation archive from composer page and execute it. The installer will download composer and defines a route for the environment that will allow us to use composer command globally.

Using Chocolatey

Another way to install composer on Windows is via Chocolatey, you may do this with the following command:

choco install composer

Also you can install it with Chocolatey GUI

If you have not installed Chocolatey you can see how to install it here: ‘Chocolatey’ a package manage for windows


To install it on Linux, you need to use your terminal. Depending on what type of installation locally or globally are the steps you need to follow.

Locally: to install it locally, you need to run the commands in the composer page in the folder of our project.

This will download a file called composer.phar in our directory or folder, then we can run composer with

 php composer.phar

I especially recommend installing composer globally, you can use it in your PHP projects without installing everytime you need it, and it is easier to write composer commands when it’s globally installed.

Globally, when you are installing composer globally you can run the commands shown in everywhere and then move the generated file composer.phar to /usr/local/bin/composer

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

You can know what commands you can use running this command

composer list

How to use Composer?

Create a file with the name composer.json in our project, this file describes the dependencies of the project and other information(metadata) for example license, author, version description, etc.

This is an example of a Laravel project composer.json

"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"require": {
"php": ">=5.6.4",
"laravel/framework": "5.4.*",
"laravel/tinker": "~1.0"
"require-dev": {
"fzaninotto/faker": "~1.4",
"mockery/mockery": "0.9.*",
"phpunit/phpunit": "~5.7"
"autoload": {
"classmap": [
"psr-4": {
"App\\": "app/"
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
"scripts": {
"post-root-package-install": [
"php -r \"file_exists('.env') || copy('.env.example', '.env');\""
"post-create-project-cmd": [
"php artisan key:generate"
"post-install-cmd": [
"php artisan optimize"
"post-update-cmd": [
"php artisan optimize"
"config": {
"preferred-install": "dist",
"sort-packages": true

With require and require-dev we are specifying what packages the project require then you can run:

php composer.phar install


composer install

After this, our project will install all packages that are needed.

Searching packages for our project

At this moment probably you are thinking about how I obtain packages for my project and don’t reinvent the wheel, With this in mind exist Packagist

You can see an example of composer use here

Category: en-php
Something wrong? If you found an error or mistake in the content you can contact me on Twitter | @luisg2249_luis.
Last 4 post in same category