Himanshu SharmaSoftware engineer
Let’s talk

Composer · Lesson 4 of 15

Composer require and remove: changing the dependency graph

Add and remove a local package deliberately, inspect manifest changes, and understand why indirect packages can remain installed.

12 minManaging dependenciesUpdated 14 November 2026

Awaiting technical review. This lesson is readable but not yet listed in search engines.

By the end of this lessonConfigure the shop consumer and use require and remove without editing generated dependencies.

After this lesson you can add a package to the shop, inspect the resulting dependency graph and remove it deliberately. The package is local, so you can learn the workflow without downloading an unfamiliar library.

Set up the consumer

Keep the library manifest from the previous lesson in composer-lab/book-label. Create a sibling composer-lab/shop directory and save this complete composer.json there. Run this lesson's commands inside shop, not inside the library.

json
{
  "name": "course/shop",
  "description": "A fictional bookshop consumer.",
  "license": "MIT",
  "repositories": [
    {
      "type": "path",
      "url": "../book-label",
      "options": {
        "symlink": false,
        "versions": { "course/book-label": "1.0.0" }
      }
    }
  ],
  "require": { "php": "^8.4" }
}

The repository points to a sibling directory relative to this consumer. The explicit version makes the exercise independent of Git branch names. Disabling symlinks asks Composer to mirror the package rather than connecting the installed folder directly to the working library. That distinction will matter when we edit the library later.

At this stage the library has metadata but no useful class yet. Installing it is still a valid way to study dependency files. Do not run its unfinished check script. We will add the PHP implementation in the autoloading lessons.

Require a specific package range

bash
composer require course/book-label:^1.0

Selected output captured on 2026-11; yours will differ:

text
./composer.json has been updated

The command normally adds a requirement, resolves compatible packages and installs them. It is more than a downloader. Inspect the shop manifest: its requirements now include the library. Inspect the lock: it records the selected local package version and source information. Inspect the vendor directory: it contains an installed copy and generated autoloading support.

Do not interpret a successful installation as evidence that the package's business behaviour works. Composer understands package requirements, not whether a label says the correct thing. We will verify that with a small executable check.

The caret constraint expresses permission for a range of releases. It does not instruct Composer to install every release in that range, nor does it mean that a future installation with an existing lock automatically upgrades the library. The next two lessons separate those decisions.

Remove a requirement, not a random folder

bash
composer remove course/book-label

Example output captured on 2026-11; yours will differ (selected lines):

text
./composer.json has been updated

This removes the root requirement and updates the installation accordingly. If another installed package needed the same library, that relationship would still matter. Dependency management works on a graph; deleting one edge does not necessarily remove every node that was reachable through it.

Deleting a vendor subdirectory by hand does not express your intent in the manifest. A later install can restore the package because the maintained requirement still exists. Use Composer's commands to change requirements and review the generated changes instead of treating vendor as a manually curated folder.

Restore the dependency for the following lessons by running the same require command again. The consumer should finish with course/book-label in its normal requirements. If you accidentally removed the library's own manifest, stop and restore that source file rather than trying to regenerate your package from its installed copy.

Read changes as a reviewer

The official Composer command reference documents require and remove. In your own workflow, compare three things after a change: your requested constraint, the selected lock entries and the files installed. A mismatch is a reason to investigate before committing, even when the command exits successfully.

The local package has no indirect dependencies, which keeps the first example understandable. Real packages may bring several. Review their licenses and purpose too; the fact that you did not type their names does not remove their role in your program.

Common mistakes

Running require from the library directory makes the library itself the root project. Always identify the current manifest before changing requirements. Do not create an accidental self-dependency by requiring the library inside its own source directory.

Using an unquoted complex constraint can also let the shell interpret special characters. The simple caret example works in the verified shell; later examples quote constraints to make the intent clearer across shells.

Exercise

Delete nothing manually. Remove the local library, inspect the manifest, then restore it. Explain why keeping the vendor folder while deleting the requirement would be an unreliable final state.

Show the answer

The manifest would no longer declare the dependency, even if a leftover file happened to make today's program run. A fresh install could omit it. Restore the requirement using require and verify that the lock and installed packages match the declared dependency.

Continue learning

Previous: Composer manifests and locks: intent versus resolution

Next: Composer install versus update: choosing a deliberate change

Related: Composer PSR-4: loading the bookshop library

Related: Composer packages: path repositories, Git tags and publishing

Key points

  • Require records a constraint and normally resolves and installs packages.
  • Remove changes requirements and removes packages no longer needed.
  • A local path repository makes this experiment independent of public downloads.

Check yourself

1. What does require normally change?
2. Why can an indirect package remain after removing one requirement?
3. Which folder should contain the consumer manifest?