PHP Composer: Introduction, Installation, Usage and Next Steps

    Pandurang Agjal
Listen to this article

php-composer-introductionI’m sure you’re familiar with npm and Bundler. npm is a package management tool for JavaScript runtime environment Node.js. Bundler is a dependency manager for Ruby.

Similarly, PHP Composer works as a package dependency manager for PHP projects.

PHP Composer: Introduction

As software developers, we understand the importance of code reusability. But while doing so, you might end up including several external libraries.

For example, say you wanted to add an email functionality in your project. How would you go about it? Instead of reinventing the wheel, you would of course use an existing library, such as PHPMailerYou would download the library and embed it into your project.


Well, ideally, this task would not take you more than a few minutes. But! What if your project was large, and dependent on a number of external, large libraries? 

This is exactly where PHP Composer comes into picture.

PHP Composer handles this task automatically by just adding a single line of code. It automatically downloads all libraries your project depends on, and necessary dependencies of those libraries too. It also deals with autoloading issues.

So, PHP Composer works as a Dependency Manager for your PHP project.


PHP Composer: Installation

Now, before you begin installing PHP Composer, you need to make sure your PHP version is 5.3.2 or higher. If you are using a lower version, you will need to upgrade it.

You will also need to install cURL, php5-curl, OpenSSL and Git. PHP Composer uses cURL and Git commands to fetch remote libraries to be included in your project.

If the above requirements are met, you can proceed to installing PHP Composer globally on your system by running the following commands:

curl -sS | php

This will download…Wait “phar” it 😉 … phar file of Composer into your current directory.

Now we have make it available globally on our machine. For that run:

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

Now it’s time to check if Composer is installed correctly or not. To check this, just run the following command in your terminal:


If you’re able to see Composer related information then all’s well (else post the problem in the comments section for me to help 🙂 )

Moving on, let’s see how to use PHP Composer.


PHP Composer: Basic Usage

Packagist is the PHP’s main Composer Package Repository, where you can find more than 28,000 reusable libraries. You can check Packagist before building a new library from scratch.

Now, let’s continue with our initial example. Say you wanted to embed the PHPMailer library into your project.

So, begin by creating a project in your web root.

cd /var/www/html
mkdir myproj

Inside your project directory create a file named composer.json.

cd myproj
touch composer.json

Edit the composer.json, and write the following:

"require": {
"phpmailer/phpmailer": "5.2"

This line simply tells the Composer that your project requires PHPMailer, version 5.2.

Next, you have to prompt the Composer to download the required libraries (in this case PHPMailer).

composer install

You should notice that the Composer has downloaded all the libraries (as specified in composer.json) for your project, under the vendor directory in project root.

Now, to use a library, say PHPMailer, in any of your project files, simply add the following line before the rest of your code:

require 'vendor/autoload.php';

This line loads the autoloader of the Composer. This single line includes all the files which have been downloaded by the Composer.


Next Steps

There are several blogs which cover PHP packages in detail, such as Phil Sturgeon’s blog, which can make for a great read, specifically this article: Packages: The Way Forward for PHP.

Remember, before you start any development, go through Packagist. Browse the available packages. If you do not find what you’re looking for, then go ahead and build it; and do not forget to contributing to the open source community by sharing it 🙂 .

You can always refer to PHP Composer’s official website, for more information.

But, if you have any problems or doubts, feel free to post them in the comment section below. Happy Coding!!

Pandurang Agjal

Pandurang Agjal

Leave a Reply

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

Get The Latest Updates

Subscribe to our Newsletter

A key to unlock the world of open-source. We promise not to spam your inbox.

Suggested Reads

Join our 55,000+ Subscribers

    The Wisdm Digest delivers all the latest news, and resources from the world of open-source businesses to your inbox.

    Suggested Reads