How to require a PEAR package with Composer

How to require a PEAR package with Composer

PEAR is short for "PHP Extension and Application Repository" and is pronounced just like the fruit. The purpose of PEAR is to provide a structured library of open-source code for PHP users. With the passage of time, there were many lightweight alternatives to php PEAR classes which has made it quite unpopular e.g Packagist.

Most of the developers use Composer because it works amazingly well, and is pretty easy to use. However, a lot of useful packages aren't (it's worth to check the packagist website to see if really your package isn't available) registed in Packagist, therefore the easy workflow that composer provides (composer install vendor-name/package-name) wouldn't work without the appropriate configuration.

In this article, you will learn how to require a PEAR package in composer easily.

1. Add the PEAR as a custom repository

As first step, you will need to add the PEAR repository in the repositories property of your composer.json file. For PEAR, the repositories property will look like:

{
    "repositories": [
        {
            "type": "pear",
            "url": "https://pear.php.net"
        }
    ]
}

2. Require a PEAR package

Now that the PEAR repository is registered, you can require and install any registered PEAR package. For example, we are going to install the Numbers_Words package registered in PEAR using composer require:

composer require pear/Numbers_Words

We are going to use the pear prefix and the package name in PEAR that will be Numbers_Words.

Note

Even the PEAR packages has dependencies but don't worry about them as they will be automatically installed too. It's worth to say that only the packages of PEAR and not the PEAR2 repository.

Important

Composer decided to don't work anymore in support for PEAR (at less, not the official maintainers). If you're lucky, a lot of PEAR packages has been moved to packagist, so you will only need to search if the package that you need has been moved too. In this case, the package of Numbers_Words is available in packagist and can be installed using:

composer require pear/numbers_words 

A lot easier than configuring the PEAR repository in our composer.json, isn't ?

Happy coding !

Become a more social person