Difficulty: Beginner
Estimated Time: 1 hour

Composer Tutorial

A walk through of various Composer tasks.

Composer is a dependency manager for PHP. It downloads public PHP packages (projects, libraries, etc) from Packagist.org.

Each step in this tutorial goes over basic Composer usage. The original tutorial for this in the following git repo: https://github.com/david-hernandez/composer-tutorial


The steps in this tutorial are based on building a Drupal 8 project with Composer, since that was the original motivation for the tutorial. However, most of what is explained is specific to Composer, not Drupal, so the information should still be relevant to any PHP project. And since the Drupal 8 use-case is more complex than simple "Hello, World!" functionality, it shows some of Composer's possibilities.

Before you begin

You may see the environment performing some setup tasks. It is installing Composer for you. If it is still working as you start step 1, just let it finish. It should only take a moment.

As you progress through the steps, the environment does not reset. You will be working with the same project directory and files. Feel free to do whatever you want, but you will need to delete the composer.lock file and sometimes the contents of the project directory, to clean up from previous steps. Just use the rm command. This is an Ubuntu system.

Also, sometimes when you run the install command the system may take a while. Just let it run. There is nothing I can do about it.

Composer Tutorial

Step 1 of 13

Using the Init Command


Everything Composer does it based on a composer.json file sitting in the root of your project. You can create this file yourself if you want, but the init command can get you started. It will walk you through creating basic parts of the file, like the description and initial requirements.

$ composer init

Welcome to the Composer config generator  

This command will guide you through creating your composer.json config.

This generator will prompt you for basic information to create your initial composer.json file.

Package name (<vendor>/<name>) [root/www]: david-hernandez/composer-tutorial

I used my Github username and a made up project name. This isn't required for your local project purposes, but is if you want to make your project public and create an account on Packagist.org.

Description []: My fancy new composer project.

Add a helpful description for the project.

Author [, n to skip]: David Hernandez <[email protected]>

The author isn't required, but helpful. It provides contact information.

Minimum Stability []: dev

Minimum stability dictates what versions of packages you consider acceptable. Setting to dev means it is ok to download development versions of packages. Whether a version is considered dev, alpha, stable, etc, is dictated by the package Composer retrieves, not you.


Package Type (e.g. library, project, metapackage, composer-plugin) []: project

Set the package type. For a website or application, project is likely the type.

License []:

If you want to define the open source license your project will use, like GPL, put it here. This is optional.

Define your dependencies.

Would you like to define your dependencies (require) interactively [yes]? no

The generator lets you define the packages your projects requires at this step. These can be added later, so it is not necessary to do it now.

Would you like to define your dev dependencies (require-dev) interactively [yes]? no

Development dependencies are grouped separately. We'll get to this later.

    "name": "david-hernandez/composer-tutorial",
    "description": "My fancy new composer project.",
    "type": "project",
    "authors": [
            "name": "David Hernandez",
            "email": "[email protected]"
    "minimum-stability": "dev",
    "require": {}

When the generator is done, it will display what the resultant composer.json file will look like.

Do you confirm generation [yes]? yes

The file will not be written unless you say yes to the confirmation prompt.

Once written, you should see the file written to your project directory in the editor window up above. Open the file and take a look.