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
Requirements
Before you start, you’ll need:
- A GitHub account
- A Packagist.org account
- PHP installed
- Composer installed (
composer --versionto check) - Basic understanding of PHP namespaces and classes
Step 1 — Basic Package Structure
Create a folder for your project:
mkdir MyPackage
cd MyPackage
Inside it, create this structure:
MyPackage/
├── src/
│ └── Greeting.php
├── composer.json
└── README.md
Example of src/Greeting.php
<?php
namespace MyPackage;
class Greeting
{
public static function hello($name)
{
return "Hello, {$name}!";
}
}
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": {}
}
⚠️ The
"name"field must follow the patternusername/package, matching your GitHub username.
Step 3 — Test the Autoload Locally
Before publishing, test the autoload to make sure everything works.
Run:
composer install
Then create a test.php file:
<?php
require __DIR__ . '/vendor/autoload.php';
use MyPackage\Greeting;
echo Greeting::hello("MyPackage");
Run:
php test.php
Expected output:
Hello, MyPackage!
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
3. Create a version tag (Packagist uses tags to detect releases):
git tag v1.0.0
git push origin v1.0.0
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
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
And use it like this:
use MyPackage\Greeting;
echo Greeting::hello("World");
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)