Setting up Composer locally for a PHP project

Composer is a tool for dependency management in PHP. It allows to declare the dependent libraries for a PHP project and the Composer will update or install the libraries for the project.

In this article, we will see how to integrate Composer to a PHP project locally.

Note : PHP is already setup in your machine

1. Download Composer installer

Download the Composer installer from this link and copy to the root directory of PHP project.

2. Execute the installer

We can execute the installer from terminal to setup Composer for the project with the given below command.

$php installer

This will download a file called composer.phar. This is the composer application and this can be used to manage ( install / update ) libraries to the project.

3. Define the dependent libraries to the project

There are two methods by which we can declare the dependent libraries to a project. In the given below example, we will see how to add the machine learning library called php-ml to a project

Method 1 : Using composer command

$php composer.phar require php-ai/php-ml

Method 2 : Directly create / edit the file composer.json

    "require": { 
      "php-ai/php-ml": "^0.6.2"

Note : Latest version number can be obtained at

4. Downloading the dependent library to this project

Issue the given below command at the root directory of the project via terminal.

$php composer install

Note : This will download the dependent libraries to the directory called vendor.

5. Integrating the dependent library to out project

We can integrate a library to our PHP code by the given below code :

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

If needed, we can add our code to autoload by specifying it in the composer.json as given below :

"autoload": {
        "classmap": [
"require": { 
      "php-ai/php-ml": "^0.6.2"

Note : “src” is the directory where our code resides

6. Update dependent libraries

Whenever composer.json is modified, we can run the below command to update the libraries in the project

$php composer.phar update

Comments on this post

No comments.

Leave a Reply

Your email address will not be published. Required fields are marked *

Trackbacks and Pinbacks on this post

No trackbacks.

TrackBack URL