---
title: Installing PHP Composer on Ubuntu Focal Fossa (20.04 LTS)
description: Learn how to install PHP Composer on Ubuntu 20.04 LTS (Focal Fossa) with this step-by-step guide. Set up PHP Composer quickly and efficiently on your Scaleway server for seamless dependency management.
tags: PHP-Composer Ubuntu Focal-Fossa symfony
products:
  - instances
hero: assets/scaleway_php_composer.webp
dates:
  validation: 2024-05-27
  posted: 2018-01-29
  validation_frequency: 24
difficulty: beginner
usecase:
  - deploy-external-software
ecosystem:
  - third-party
---
import Requirements from '@macros/iam/requirements.mdx'


PHP Composer is a package dependency management tool for PHP similar to NPM for Node.js and a 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.

<Requirements />

- A Scaleway account logged into the [console](https://console.scaleway.com)
- [Owner](/iam/concepts/#owner) status or [IAM permissions](/iam/concepts/#permission) allowing you to perform actions in the intended Organization
- An [SSH key](/organizations-and-projects/how-to/create-ssh-key/)
- An [Instance](/instances/how-to/create-an-instance/) running on Ubuntu Focal Fossa
- Installed PHP 7.4 or higher on your Instance

## 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](https://composer.github.io/pubkeys.html). 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:
    ```
    composer
    ```

    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](https://carbon.nesbot.com/docs/). 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.

    <Message type="note">
      You can search the Composer repository [Packagist](https://packagist.org/) for PHP packages.
    </Message>
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:

    ```
    <?php

    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 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
    ```

<Message type="note">
  If you want to update your PHP packages you can simply run `composer update`.
</Message>

To learn more about Composer visit the [official documentation](https://getcomposer.org/doc/).