Create a composer package and list on packagist

(Last Updated On: March 29, 2019)

1. Introduction

In software engineering and development, things change very quickly. We need to move forward and keep ourselves and our code up to date. Software development mostly revolves around code re-use because there is no point in writing login-signup again and again. There are ways to share our code with others and to use the code shared by others. In the PHP ecosystem, the composer is the most widely used dependency manager. Assuming that we know how the composer works, we want to explore how we can share our code or libraries that can be used by others in their projects. Let us get started.

2. Write the code

For creating a composer package, we need to follow a directory structure like below:
The src directory contains all the code and composer.json have all the information for discovery of the package.
Let us write a Calculator class which looks like:

namespace Bootsity\Calculator;

class Calculator

    public function add($a, $b)
        return $a + $b;

    public function multiply($a, $b)
        return $a * $b;


This is a really simple class having two methods: one for adding two numbers and other for returning product of two numbers.

In our code, let us add a composer.json

    "name": "bootsity/calculator",
    "description": "Package for mathematics calculations",
    "type": "package",
    "require": {
        "php": ">=5.4.0"
    "license": "mit",
    "authors": [
            "name": "Pradeep Kumar",
            "email": "",
            "homepage": ""
    "minimum-stability": "dev",
    "autoload": {
        "psr-4": {
            "Bootsity\\Calculator\\": "src/"

This composer.json specifies information about package name, description, and authors. One important key to note here is autoload. This key directs the composer to use PSR-4 standard for autoloading.

3. Host package on GitHub

Now, we have completed the writing of our code, we need to put it on some public repository from where packagist will read our code and supply it to the developer when they require it in their composer.json. GitHub, Bitbucket and Gitlab are some examples. We will use Github. Create a repo on GitHub and push your code in the same repo.
For our example here is the repository GitHubhub that we can refer –

4. Register with packagist

Now, our code is available publically and anyone can download a copy of it and re-use. But what if we released a new version of our Calculator class having one more method to find the difference between two numbers? The developers who are using my Calculator class by downloading it will not get the updates. To make the updates easy, we need to register our package with packagist so that it is discoverable and people get easy updates.
To register with packagist, go to packagist,

After submission, our package will be crawled and will be available to use through composer.

5. Use package in other projects

Now, our package is available on packagist, it can be used by anyone in their projects. Let us try to use our package in a sample project. Let’s create a blank composer project:
composer init
This command will open up an interactive prompt, which will look like:
Pradeeps-MacBook-Pro:composer-repo-user prady$ composer init

  Welcome to the Composer config generator  

This command will guide you through creating your composer.json config.

Package name (<vendor>/<name>) [prady/composer-repo-user]: 
Description []: 
Author [, n to skip]: n
Minimum Stability []: 
Package Type (e.g. library, project, metapackage, composer-plugin) []: 
License []: 

Define your dependencies.

Would you like to define your dependencies (require) interactively [yes]? n
Would you like to define your dev dependencies (require-dev) interactively [yes]? n

    "name": "prady/composer-repo-user",
    "require": {}

Do you confirm generation [yes]? y

It will create a composer.json. In the same directory, execute:

composer require bootsity/calculator
this will add the dependency in composer.json and update all the dependencies.
Let’s create an index.php having the following code:
require_once __DIR__ . '/vendor/autoload.php';

use Bootsity\Calculator\Calculator;

$instance = new Calculator();

echo $instance->add(2,6);

When we execute the above code, it prints 8 on output.

6. Summary

In this article, we learned how we can create packages and distribute them for reuse using composer. We just need to put the code on a public git repository and register it with packagist to make it discoverable – that’s all we need to create a composer package. Our package is available on packagist here – What you are planning to roll out as the a composer package?


Please enter your comment!
Please enter your name here