DEV Community

Cover image for How to create and publish your own packages on Packagist
Antonio Silva
Antonio Silva

Posted on

How to create and publish your own packages on Packagist

What Is Packagist?

Packagist.org is the official package repository for Composer, PHP’s dependency manager. It’s where packages are published so anyone can install them easily using:

composer require yourusername/yourpackage
Enter fullscreen mode Exit fullscreen mode

Requirements

Before you start, you’ll need:

  • A GitHub account
  • A Packagist.org account
  • PHP installed
  • Composer installed (composer --version to check)
  • Basic understanding of PHP namespaces and classes

Step 1 — Basic Package Structure

Create a folder for your project:

mkdir MyPackage
cd MyPackage
Enter fullscreen mode Exit fullscreen mode

Inside it, create this structure:

MyPackage/
├── src/
   └── Greeting.php
├── composer.json
└── README.md
Enter fullscreen mode Exit fullscreen mode

Example of src/Greeting.php

<?php

namespace MyPackage;

class Greeting
{
    public static function hello($name)
    {
        return "Hello, {$name}!";
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 2 — Create the composer.json File

In the root directory, create a file named composer.json with this content:

{
    "name": "yourusername/my-package",
    "description": "A simple greeting package in PHP",
    "type": "library",
    "license": "MIT",
    "autoload": {
        "psr-4": {
            "MyPackage\\": "src/"
        }
    },
    "authors": [
        {
            "name": "Your Name",
            "email": "youremail@example.com"
        }
    ],
    "require": {}
}
Enter fullscreen mode Exit fullscreen mode

⚠️ The "name" field must follow the pattern username/package, matching your GitHub username.

Step 3 — Test the Autoload Locally

Before publishing, test the autoload to make sure everything works.

Run:

composer install
Enter fullscreen mode Exit fullscreen mode

Then create a test.php file:

<?php

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

use MyPackage\Greeting;

echo Greeting::hello("MyPackage");
Enter fullscreen mode Exit fullscreen mode

Run:

php test.php
Enter fullscreen mode Exit fullscreen mode

Expected output:

Hello, MyPackage!
Enter fullscreen mode Exit fullscreen mode

Step 4 — Push the Project to GitHub

1. Create a public repository on GitHub (e.g., my-package).

2. Initialize the local repository:

git init
git add .
git commit -m "First version of the package"
git branch -M main
git remote add origin https://github.com/yourusername/my-package.git
git push -u origin main
Enter fullscreen mode Exit fullscreen mode

3. Create a version tag (Packagist uses tags to detect releases):

git tag v1.0.0
git push origin v1.0.0
Enter fullscreen mode Exit fullscreen mode

Step 5 — Publish on Packagist

1. Go to https://packagist.org/packages/submit

2. Paste your GitHub repository URL, for example:

https://github.com/yourusername/my-package
Enter fullscreen mode Exit fullscreen mode

3. Click Check, then Submit.

Done 🎉 — your package is now public on Packagist!

Step 6 — Enable Automatic Updates via GitHub Hook

After publishing, you can enable automatic updates:

  • Go to your package page on Packagist.
  • Click “Maintain”.
  • Under “GitHub Hook”, click “Connect” to allow Packagist to auto-update your package whenever you push changes to GitHub.

Step 7 — Using Your Package in Another Project

In any other PHP project, just run:

composer require yourusername/my-package
Enter fullscreen mode Exit fullscreen mode

And use it like this:

use MyPackage\Greeting;

echo Greeting::hello("World");
Enter fullscreen mode Exit fullscreen mode

Final Tips

  • Use semantic versioning (semver) (v1.0.0, v1.1.0, v2.0.0, etc.)
  • Keep a clear README.md explaining usage.
  • Add unit tests with PHPUnit.
  • Use GitHub Actions for automated testing.

Top comments (0)