How to use Composer

The PHP programming language is very rapidly developing. A few years ago a huge number of libraries on all occasions could boast only Python. However, already developed a huge number of libraries for PHP and all are available for installation in just a few commands.

You can use the package Manager composer. Utility not only allows you to install third-party packages but also to update them when new versions are released, to resolve dependencies, and also very easy to create packages for their libraries. In this article we will examine how to use Composer for package management in PHP.

  • The syntax and options of the Composer
  • Install Composer
  • How to use Composer
    • 1. Project-based package
    • 2. Installing packages
    • 3. Remove packages
    • 4. Upgrade packages
    • 5. Reset startup
    • 6. Creating a package
    • 7. Installing packages from third-party repositories
  • Insights

The syntax and options of the Composer

The first thing to say, a Composer is a command line utility, it has no graphical interface, but that doesn’t make it worse. Here is its syntax:

$ composer options team

Options most of the utilities not so much. Let’s consider the most useful:

  • -h – display the help screen;
  • -q – a shortened version of the output;
  • -V – show the version of the utility;
  • -n – don’t ask interactive questions;
  • -v, -vv, -vvv – setting details of output;
  • -d – use the specified working directory.

More interesting commands that you will constantly use:

  • archive – archives the current project as a library to be sent to the Network;
  • check-platform-reqs – check whether the system requirements;
  • create-project : creates a project based on the package to the specified directory;
  • depends – displays the dependencies of a package;
  • dump-autoload – update system autoloading;
  • exec – allows you to run scripts from the installed packages;
  • init – creates an empty project in the current folder.
  • list – displays a list of available commands;
  • outdated – displays a list of packages for which updates are available;
  • prohibits – displays the package names that interfere with install the specified package;
  • search – search packages in repositories;
  • self-update – Composer update to the latest version, works only for local installations;
  • show – information about the package;
  • update – updates all packages to the latest version.

Most of these options we will examine below, in the examples of usage composer. Now let’s see how to install it.

Install Composer

Before you do anything, the utility must be installed. You can install Composer globally for the entire operating system or only in a specific folder. For global install on Ubuntu, use the command:

sudo apt install composer

In addition, the installation of utilities to the folder in which your project. To do this, first create a folder and navigate into it:

mkdir new_project && cd new_project

Then download the latest version of the utility with the command:

php -r "copy('', 'composer-setup.php');"

Installing Composer is run with the command:

php ./composer-setup.php

After installation in the directory appear file Comreg.phar, which should start to work with the utility. In Windows you can install Composer only way. In this article, I will assume that the utility installed globally in the system, but it does not change, just the different executable name:

$ php ./composer.phar command

It is important to note that the latest version requires that your system have installed the PHP version 7.0 or later. Otherwise, the utility will install, but during run-time errors are reported. If you use control panel, which has multiple versions of PHP, then you need to pass the path to the utility binary file PHP required version, for example:

$ /opt/php7.0/bin/php7.0 ./composer.phar command

The path to the binary file will vary depending on the method of installing PHP, panel and version of the interpreter.

Version of the utility installed from the official website, will, usually? much fresher than from the repositories:

php composer.phar -V

To remove the Composer, it is enough to remove files from the folder where it was installed. To update Composer to the latest version, run:

php composer.phar self-update

Now it’s time to move on to examples of working with the Composer.

How to use Composer

1. Project-based package

Most often you will deploy a Composer project based on the already existing packages. In this case utility takes the package from the repository and simply extract it into the current folder and all dependencies are already placed in the vendor folder. Use the command create-project. Let’s create the project on the basis of package slim/slim4-skeleton:

composer create-project slim/slim-skeleton ./

The second argument to a utility, it is necessary to transfer the folder in which the project will be deployed. In this case, the current folder. After installation is complete, you will receive the deployed application and you can start to modify it.

2. Installing packages

To install packages in Composer, use the command require. Utility will install the desired package in the vendor subfolder, add it in startup and in file composer.json. For example, install illuminate/eloquent:

composer require illuminate/eloquent

In addition, composer allows you to install packages that will be available only to the developer. To do this, use the option –dev:

composer require --dev phpunit/phpunit

There are cases when a package does not want to install because of incompatibility with the platform. First we need to make sure that all the necessary PHP extensions installed on the system. If your console uses a version of PHP other extensions, and the package will only be used with a web server, you can disable the checking of system requirements package with the option –ignore-platform-reqs:

composer require --ignore-platform-reqs illuminate/eloquent

Another mistake you can make is not meeting the minimum stability of package. For example:

package_name requires exeedence -> satisfiable by technioal but these conflict with your requirements or minimum-stability

All Composer packages can have two levels of stability:

  • stable – stable;
  • dev – is in development.

The stability level of the package defines its developer. This problem is slightly more complicated, it is necessary to change the minimum level of stability for your project on dev. Open the file composer.json and under the row License, add the following line:

vi composer.json

"minimum-stability": "dev",

Or, if the file already has a line of minimum-stability, it is necessary to replace it with values from stable to dev. You will then be able to install the composer package you need. Reminder that all team Somrong the same, even if Comreg on hosting, not on the local machine or a VPS server, you only need to specify the correct path to the executable file.

3. Remove packages

To remove a package you no longer need, use the option remove:

composer remove illuminate/eloquent

Similarly, you remove a package for developers:

remove composer --dev illuminate/eloquent

4. Upgrade packages

Below your packets were not vulnerabilities and old problems, it is necessary to update them regularly. The relevant command is update. Please note that updated packages only in the current directory, not the system, so will have to update each project individually:

composer update

And for packages developers:

composer update --dev

5. Reset startup

It happens that you have created a new class, or installed a package, but the classes still are not visible, and when you try to access them you get error. In this case, the cache should be updated Composer autoloading:

composer dump-autoload

After this command, everything should work fine. If no package is incompatible or if it’s your class, it does not comply with the PSR-4 standard.

6. Creating a package

Now you know how to use Composer, let’s talk a little about the creation of their packages. To create an empty project in the current folder, simply run the command:

composer init

The utility asks:

  • Package name – new package name in the format: author/name;
  • Description – the description;
  • Author – the author of the package;
  • Minimum Stability – stable package, you can choose dev or stable;
  • Package Type – the type of project such as: library, project, metapackage, composer-plugin;
  • License – license;
  • Depencies – here you can interactively find the packages, which will depend on your project.

In the last step, the utility prompts you to check if everything is correctly specified in the configuration file:

Then create a file composer.json and you will be able to install the appropriate packages and add your source files. I will not write here about the autoloading PSR-4 in composer.json, as this is beyond the scope of normal use of utilities and is more about development.

After the project was created, you can create in the project folder a git repository and upload it to GitHub or another service. Immediately after this, your package can be installed via Composer in any other project simply by adding it to the repository.

7. Installing packages from third-party repositories

First you need to add a reference to the repository in the composer.json. I assume that your repository is public, so no authorization keys do not need:

vi composer.json

"repositories": [
"type": "vcs",
"url": "link to git repository"

For example:

Section repositories need to be added to the same level as the license. other main sections. Then you can just install your package:

composer require sergiy/selenium-chrome-library

After that, the package folder will appear in the vendor sub-folder and its classes are added to the startup.


In this article we have understood how to use Composer to install various PHP packages how to update these packages and how to create your own package and use it in other projects. Can not but rejoice that PHP appeared and increased the catalog of libraries that allow to implement, if not all, very much.


(Visited 2 times, 1 visits today)