Scroll to top

PHP Package Management with Composer

Hi ,
Let’s find an easy way for package management. Do you remember days we use to download, extract files and paste inside our libraries folder and manually require them, that’s all old style. I will show you how to manage your packages using composer which can do autoloading classes, autoloading namespaces, downloading and requiring packages for us etc just with one command. Say you want to download Amazon Web Services SDK you can go to packagist website and search for aws it will show you package name

"aws/aws-sdk-php": "2.7.*@dev"

Paste it into our composer file and run

php composer update

and we are done. It will download aws-sdk into vendor folder and autoload for us.

Installing Composer

The composer has two main logical parts: there is a repository that stores packages, and then there’s the command-line application, which helps you find, download, update and share code.
You can download and install composer by following this steps . Once you are done create a new PHP project and create a new file name it as composer.json . Copy below lines into composer.json

    "name": "kodeinfo",
    "description": "Learning PHP / Laravel",
    "keywords": ["kodeinfo", "php", "laravel"]  
    "require": {

    "config": {
        "preferred-install": "dist"
    "minimum-stability": "stable"

Above file says our project name is kodeinfo and some description, keywords for it, the minimum-stability setting is to only get the packages which are stable and can be used in the production site.

Installing First Package

We will download our first package and will try to make it work with our PHP application. We will use faker which generates fake data for testing. Go to package and type faker you will get the package name as below

"fzaninotto/faker": "1.5.*@dev"

In your composer.json in require section paste this and run composer update

"require": {
       "fzaninotto/faker": "1.5.*@dev"

You can also install the package through command line which is recommended.

php composer require fzaninotto/faker

It will ask to enter version which you want to install, at the time of writing this post latest version was 1.5.

Our application needs to find the autoloader and run it so packages we downloaded through composer will be available for our application. Create a new file as index.php and require our autoloader

require '/vendor/autoload.php';

Create a new instance of faker class

$faker = Faker\Factory::create();

Now we will call text method which generates fake text


Our complete index.php is below


require '/vendor/autoload.php';

$faker = Faker\Factory::create();


In this way, you can easily manage your packages, if package developer is adding new features or fixing a bug in a package you can easily update your local copy by doing composer update.
You can use Composer to avoid needing to pay attention to the versions, and just run an update, and commit all the changes. In production do not push vendor directory to your version control instead just run composer update on a production machine or best solution will be to setup hooks which run composer install as soon as your new code is pushed.



Author avatar

Imran is a web developer and consultant from India. He is the founder of KodeInfo, the PHP and Laravel Community . In the meantime he follows other projects, works as a freelance backend consultant for PHP applications and studies IT Engineering . He loves to learn new things, not only about PHP or development but everything.

Post a Comment

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