1. Home

# Installing packages with Composer, Phar archives or PEAR¶

in

Let’s have a look at the different ways of installing packages in PHP. We’ll cover Composer, Phar archives and PEAR packages.

View these related posts explaining what some these tools do and how to use them.

PHP Quality Assurance tools

What is Phing (PHP build tool) used for?

Setup Jenkins with PHP, Phing, Symfony & PHPUnit

## Checking if you have the tools already installed¶

You can use the which command to tell you the location of the executable if it’s in your PATH (and installed correctly), if you have it installed it will return the full absolute location, or if it does not exist an empty blank line.

which phpunit
# /usr/local/bin/phpunit
# Installed correctly! 😀

which someNonExistentBinary
# {blank line}
# Not installed 😣


### Installation via Composer¶

Composer is pretty much the de facto standard package manager for PHP, it’s become very popular since it’s launch due it being really easy to use.

Composer allows you to define your dependencies in a composer.json file which it then reads to download and install all the required dependencies. At the same time it also creates a composer.lock file which holds information such as the exact version of the dependencies used. This allows you to deploy the application on your server knowing you’ll be using the exact same versions as installed in development.

This is the preferred way to manage packages for PHP.

You can install individual packages globally using:

composer global require 'squizlabs/php_codesniffer=*'


{
"require": {
"phpunit/phpunit": "3.7.*",
"theseer/fxsl": "1.0.*@dev",
"theseer/phpdox": "0.5.*@dev",
"squizlabs/php_codesniffer": "1.4.6",
"phpmd/phpmd": "1.5.0",
"phploc/phploc": "2.0.2",
"sebastian/phpcpd": "1.4.3",
"phpdocumentor/phpdocumentor": "v2.0.1",
"mayflower/php-codebrowser": "1.0.*@dev",
"pear/pear_exception": "dev-master",
"Phing/Phing": "2.6.1"
}
}


Execute composer update.

All of these dependencies will be installed globally on your system, by default in your ~/.composer/vendor/bin directory.

You can now use these tools (and any others you install) from the command line globally.

### Installation via Phar archives¶

You don’t have to install phar archives, you can simply use them from wherever they are (php phpunit.phar), but in the long run installing them into a directory in your system path will be the easiest way to use them across all your projects.

Phar archives contain everything you need to run that particular application. All the required files and dependencies are bundled within the Phar archive and can be executed anywhere, on any system.

# Download the Phar archive to the current directory
wget https://phar.phpunit.de/phpunit.phar

# Give it executable permissions
chmod +x phpunit.phar

# move it to our local/bin directory
sudo mv phpunit.phar /usr/local/bin/phpunit

# Check everything works!
phpunit --version
# PHPUnit 4.1.2 by Sebastian Bergmann and contributors.


You should now be able to execute PHPUnit (and any other tools you install in a similar fashion via Phar archives) from anywhere in your terminal.

### Installation via PEAR packages¶

If you’re not using Composer or Phar archives, you can also install everything via PEAR.

Installing with pear is as simple as:

sudo pear channel-discover pear.pdepend.org
sudo pear channel-discover pear.phpmd.org
sudo pear channel-discover pear.phpunit.de
sudo pear channel-discover pear.phpdoc.org
sudo pear channel-discover components.ez.no
sudo pear channel-discover pear.symfony-project.com
sudo pear channel-discover pear.Phing.info

sudo pear install pdepend/PHP_Depend
sudo pear install phpmd/PHP_PMD
sudo pear install phpunit/phpcpd
sudo pear install phpunit/phploc
sudo pear install --alldeps phpunit/PHP_CodeBrowser
sudo pear install --alldeps phpunit/PHPUnit
sudo pear install phpdoc/phpDocumentor-alpha
sudo pear install PHP_CodeSniffer
sudo pear install Phing/Phing


or individually step by step:

# Step 1: Register the channel
sudo pear channel-discover pear.phpunit.de

# Step 2: Installing a package 'phpunit' from the 'phpunit' channel
#sudo pear install channel_name/package_name
sudo pear install phpunit/phpunit

##############################
# Other useful commands
##############################

# List all packages in the phpunit channel
sudo pear remote-list -c phpunit

# List all installed packages
sudo pear list -a

# Uninstall a PEAR package (eg: phpunit/phpunit)
sudo pear uninstall phpunit/phpunit


You should now be able to execute PHPUnit (and any other tools you install via PEAR) from anywhere in your terminal.

phpunit --version
# PHPUnit 4.1.2 by Sebastian Bergmann.


### What are all these tools?¶

If you’ve followed along and installed everything as above, you can use these tools locally in your IDE (with a little configuration), or in conjunction with a continuous integration application like Jenkins.

Here are some related posts where we use some of these tools.