Get started with 33% off your first certification using code: 33OFFNEW

How to publish a package on Composer

3 min read
Published on 8th May 2024

Composer is the de facto package manager for PHP, facilitating the management of dependencies in PHP projects. If you've developed a PHP library or a project that could benefit others, sharing it on Composer can significantly enhance its visibility and ease of use. Publishing a package on Composer involves several steps, from preparing your package correctly to submitting it to Packagist, the primary package repository for Composer. This article guides you through each step, ensuring your package is ready for the wider PHP community.

Step 1: Create Your PHP Package

1.1. Structure Your Package

  • Create the Directory Structure: A typical PHP package includes source files in a src directory, tests in a tests directory, and a vendor directory created by Composer.
  • Example Structure:
    /my-package
    ├── src/
    ├── tests/
    ├── vendor/
    ├── composer.json
    └── README.md
    

1.2. Initialize Composer in Your Project

  • Navigate to your project directory and run:
    composer init
    
  • Follow the prompts to define your package name, description, author, and minimum stability. Composer will create a composer.json file based on your input.

Step 2: Prepare Your composer.json File

The composer.json file is crucial as it defines your package's dependencies and metadata. Here’s what it should typically include:

  • Name: The format should be vendor/package. For example, johndoe/my-package.
  • Description: A brief description of your package.
  • Type: Usually library.
  • Require: Any dependencies your package needs.
  • Autoload: Define PSR-4 autoloading for your package.
  • Example composer.json:
    {
      "name": "johndoe/my-package",
      "description": "A useful PHP package",
      "type": "library",
      "require": {
        "php": "^7.4|^8.0"
      },
      "autoload": {
        "psr-4": {"JohnDoe\\MyPackage\\": "src/"}
      },
      "authors": [
        {
          "name": "John Doe",
          "email": "[email protected]"
        }
      ],
      "minimum-stability": "stable"
    }
    

Step 3: Version Your Package

Before publishing, you must tag your package with a version number. Semantic Versioning (SemVer) is the most common strategy:

  • Major version: Incompatible API changes.
  • Minor version: Add functionality in a backwards-compatible manner.
  • Patch version: Backwards-compatible bug fixes.

Tagging a Release in Git:

  • Commit your changes if you haven't already:
    git add .
    git commit -m "Initial commit"
    
  • Tag your release:
    git tag 1.0.0
    
  • Push the tag to GitHub:
    git push --tags
    

Step 4: Publish Your Package on Packagist

Packagist is the main repository for Composer packages. To submit your package:

  • Create an Account: Sign up on Packagist.
  • Submit Your Package: Go to the "Submit" link. Enter the URL of your package's VCS repository (e.g., GitHub, Bitbucket).
  • Integration: For automatic updating, integrate Packagist with your VCS (e.g., GitHub) through service hooks which Packagist will guide you to set up.

Step 5: Maintain Your Package

After publishing your package, maintenance is key. This involves:

  • Keep your package compatible with the latest versions of dependencies.
  • Respond to issues and pull requests from users.
  • Keep your README.md and any other documentation up to date.

Publishing a package on Composer can significantly contribute to the PHP community while enhancing your visibility as a developer. By following the steps outlined above, from package creation to submission on Packagist, you ensure your package is useful, discoverable, and well-maintained. Remember, a successful open-source project is not just about code quality but also about good documentation and active maintenance.