Composer is a dependency manager for PHP, similar to npm for JavaScript. In this tutorial, we will show how to setup composer to install PHP library in a project.

1. Download Official Installer in project folder

Execute following command in terminal:

$ cd path/to/project
$ curl -sS https://getcomposer.org/installer | php

2. Download the PHP library required

$ php composer.phar require <library>

3. Include library in PHP code

After adding following above the code, we can start using the library.

include_once(__DIR__.'/vendor/autoload.php');
use <library>;

4. Add composer generated files to .gitignore

composer.phar
/vendor/

This is done so that these files are untracked and will not be pushed to remote origin branch (e.g. Github).

5. Command to install all dependencies in composer.json

$ php composer.phar install

Conclusion

Hopefully you are able to successfully use Composer in your PHP project now. 😃
Please let me know how it goes in the comments below. Thank you for reading! 😊

References:

  1. MailGun Documentation
    https://documentation.mailgun.com/en/latest/libraries.html#php