install php composer

Jump toUpdate content

Installing PHP Composer on Ubuntu Focal Fossa (20.04 LTS)

Reviewed on 18 January 2022 • Published on 29 January 2018
  • compute
  • PHP-Composer
  • package-manager
  • Ubuntu-Focal-Fossa

PHP Composer is a package dependency management tool for PHP similar to NPM for Node.js and bundle for Ruby. Composer facilitates the installation and updates for project dependencies. In short, Composer will pull in all the required PHP packages your project depends on and manage them for you.


Installing Composer

  1. Update the packages index and install the necessary requirements.

    apt update
    apt install wget php7.4-cli php7.4-zip unzip -y
  2. Download the composer installer. The command below downloads the composer-setup.php file in the current working directory.

    php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
  3. Verify the data integrity of the script by comparing the script SHA-384 hash with the latest installer hash found on the Composer Public Keys/Signatures page. Download the expected signature of the latest Composer installer from the Composer’s Github page and store it in a variable named HASH.

    HASH="$(wget -q -O - https://composer.github.io/installer.sig)"
  4. Verify that the installation script is not corrupted.

    php -r "if (hash_file('SHA384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

    If the hashes match, you will see the following output: Installer verified.

    If it does not match, download the Composer installation script again and double check the value of the $HASH variable with echo $HASH. Once the installer is verified, you can continue with the next step.

  5. Install Composer in the /usr/local/bin directory

    sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
  6. Verify the installation


    You should see the following output:

    root@composer:~# composer
    Do not run Composer as root/super user! See https://getcomposer.org/root for details
    / ____/___ ____ ___ ____ ____ ________ _____
    / / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
    / /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ /
    \____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
    Composer version 2.2.4 2022-01-08 12:30:42

Using Composer PHP Project

  1. Create a directory which will be the project root directory and which will hold the composer.json file. This file describes your PHP project including the PHP dependencies and other metadata.

    mkdir ~/composer-project1
    cd composer-project1
  2. Initialize a new composer.json file using the composer require <package name> command, and specify the package to download.

    In this example we will create a sample application that will print the current time using a package called carbon. Carbon can help make dealing with date/time in PHP much easier and more semantic so that our code can become more readable and maintainable.


    You can search the Composer repository Packagist for PHP packages.

  3. Run the following command to initialize a new composer.json file, and install the carbon package:

    composer require nesbot/carbon
    Using version ^2.55 for nesbot/carbon
    ./composer.json has been created
    Running composer update nesbot/carbon
    Loading composer repositories with package information
    Updating dependencies
    Lock file operations: 6 installs, 0 updates, 0 removals
    - Locking nesbot/carbon (2.55.2)
    - Locking symfony/deprecation-contracts (v2.5.0)
    - Locking symfony/polyfill-mbstring (v1.24.0)
    - Locking symfony/polyfill-php80 (v1.24.0)
    - Locking symfony/translation (v5.4.2)
    - Locking symfony/translation-contracts (v2.5.0)
    Writing lock file
    Installing dependencies from lock file (including require-dev)
    Package operations: 6 installs, 0 updates, 0 removals
    - Downloading symfony/translation-contracts (v2.5.0)
    - Downloading symfony/polyfill-php80 (v1.24.0)
    - Downloading symfony/polyfill-mbstring (v1.24.0)
    - Downloading symfony/deprecation-contracts (v2.5.0)
    - Downloading symfony/translation (v5.4.2)
    - Downloading nesbot/carbon (2.55.2)
    - Installing symfony/translation-contracts (v2.5.0): Extracting archive
    - Installing symfony/polyfill-php80 (v1.24.0): Extracting archive
    - Installing symfony/polyfill-mbstring (v1.24.0): Extracting archive
    - Installing symfony/deprecation-contracts (v2.5.0): Extracting archive
    - Installing symfony/translation (v5.4.2): Extracting archive
    - Installing nesbot/carbon (2.55.2): Extracting archive
    3 package suggestions were added by new dependencies, use `composer suggest` to see details.
    Generating autoload files
    6 packages you are using are looking for funding.
    Use the `composer fund` command to find out more!

    Composer provides autoload capabilities which allows us to use PHP classes without the need to require or include the files.

    Composer creates the composer.json. It also downloads and installs carbon and all its dependencies. If you list your project’s directory, it contains two files and a directory:

    • The vendor directory is the directory where the project dependencies are stored.
    • The composer.lock contains a list of all installed packages including the exact package version.
    • The composer.json describes the PHP project and all PHP dependencies.
  4. Create a file named testing.php and open it in a text editor (for example: nano):

    touch testing.php
    nano testing.php

    Then, add the following code to the file, save it, and exit the text editor:


    require __DIR__ . '/vendor/autoload.php';

    use Carbon\Carbon;

    printf("Now: %s", Carbon::now());


    In the first line after the opening php tag, we include the vendor/autoload.php, automatically generated by Composer. It will autoload all of the libraries.

    Then, we alias Carbon\Carbon as Carbon and with the last line we are printing the current time using the Carbon now method.

  5. Run the script:

    php testing.php
    Now: 2020-09-01 09:19:23

If you want to update your PHP packages you can simply run composer update.

To learn more about Composer visit the official documentation.