How to install composer on RHEL 8

Composer is a dependency management tool for php, much like cpan for perl. If you have read the tutorial about installing cpan, the architecture will be somewhat familiar. Composer, as a command line tool is the client that can fetch and update the php libraries we mark as needed, as well as the libraries these depend on, etc.

The source of these libraries is, a large public php package repository. We can browse the repository with a browser to find the packages we’d like to use, then include them in our projects with the help of Composer. And by solving the dependencies itself, Composer can make our lives much easier if we depend on external packages. In turn we can also share our libraries, so the community can access them trough Composer as well.

In this tutorial we will install Composer on Red Hat Enterprise Linux 8, and mark a package as needed dependency for our project, to see the tool working.

In this tutorial you will learn:

  • How to install Composer
  • How to define package as dependency
  • How to install dependencies with Composer

Composer's main help.

Composer’s main help.

Software Requirements and Conventions Used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Red Hat Enterprise Linux 8
Software Composer 1.8.0
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

How to install composer on Redhat 8 step by step instructions

To be able to use composer we naturally need php, with some extensions. PHP 7.2 and extensions for it are reachable after enabling subsription management repositories, as well as on the installer distributed in ISO format.

  1. First we need to install php related packages with dnf:
    dnf install php php-cli php-zip php-json
  2. Now we can download the Composer installer with php:
    # php -r "copy('', 'composer-setup.php');"
  3. To be able to access the tool from anywhere on the system, we place it on the $PATH. /usr/local/bin is included in the $PATH by default.
    # php composer-setup.php --install-dir=/usr/local/bin --filename=composer
    All settings correct for using Composer
    Composer (version 1.8.0) successfully installed to: /usr/local/bin/composer
    Use it: php /usr/local/bin/composer
  4. To test functionality, we create a working directory that is the root of our php development project:
    $ mkdir myProject

    And enter it:

    $ cd myProject

    After browsing the repository, we decide that we’ll need the zend-eventmanager package, version 3.2.1. We create a text file called composer.json with the following content in our project’s root directory:

        "require": {
            "zendframework/zend-eventmanager": "3.2.1"
  5. To install the package we defined as dependency, we can use the freshly installed composer:
    $ composer install
    Loading composer repositories with package information
    Updating dependencies (including require-dev)
    Package operations: 1 install, 0 updates, 0 removals
      - Installing zendframework/zend-eventmanager (3.2.1): Downloading (100%)         
    zendframework/zend-eventmanager suggests installing container-interop/container-interop (^1.1.0, to use the lazy listeners feature)
    zendframework/zend-eventmanager suggests installing zendframework/zend-stdlib (^2.7.3 || ^3.0, to use the FilterChain feature)
    Writing lock file
    Generating autoload files

    If we list our working directory, we’ll see a lockfile, and a vendor directory. The later is the one where composer downloaded the required packages.

    $ ls
    composer.json  composer.lock  vendor
    $ ls vendor/
    autoload.php  composer  zendframework

    Within the vendor directory is the package we needed, and an autoload.php. This is the only file we need to include in our code to use any of the libraries we installed with composer. It is regenerated on new package install/upgrade.