Introduction – Composer

  • Dependency Management System Requirements
  • Installation

  • – Linux / Unix / macOS Download the Composer
    • executable
      • locally
      • Global installation:
  • Windows
    • Using the installer
    • Manual installation Docker image
  • with Composer

Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and will manage (install/update) them for you.

Dependency Management#

Composer is not a package manager in the same sense as Yum or Apt. Yes, these are “packages” or libraries, but you manage them on a per-project basis, installing them in a directory (e.g. provider) within your project. By default, it does not install anything globally. Therefore, it is a dependency manager. However, it does support a “global” project for convenience through global command.

This idea is not new and Composer is heavily inspired by the node npm and ruby package.

Suppose:

  1. You have a project that depends on multiple libraries
  2. .

  3. Some of these libraries depend on others.

Composer:

  1. Allows you to declare the libraries on which you depend.
  2. It finds out which versions of which packages can and need to be installed, and installs them (meaning you download them into your project).
  3. You can update all your dependencies in a single command.

See the Basic Use chapter for more details on declaring dependencies

.

System Requirements#

Composer in its latest version requires PHP 7.2.5 to run. A long-term support release (2.2.x) still offers support for PHP 5.3.2+ in case you’re stuck with a legacy version of PHP. Some sensitive php settings and build flags are also required, but when you use the installer, you will be warned about any incompatibility.

To install packages from sources instead of simple zip files, you will need git, svn, fossil, or hg depending on how the package version is controlled.

Composer is cross-platform and we strive to make it work equally well on Windows, Linux, and macOS.

Installation – Linux / Unix / macOS

# Downloading the Composer#

Composer executable

offers a convenient installer that you can run directly from the command line. Feel free to download this file or check it out on GitHub if you want to know more about the inner workings of the installer. The source is simple PHP.

There are, in summary, two ways to install Composer. Locally as part of your project, or globally as a system-wide executable.

Locally# To

install Composer locally, run the installer in the project directory. See the download page for instructions.

The installer will check some PHP settings and then download composer.phar to your working directory. This file is the Composer binary. It is a PHAR (PHP file), which is a file format for PHP that can be run on the command line, among other things.

Now run php composer.phar to run Composer.

You can install Composer in a specific directory using the -install-dir option, and also (re)name it using the -filename option. When running the installer, following the instructions on the download page, add the following parameters:

php composer-setup.php -install-dir=bin -filename=composer

Now run php bin/composer to run Composer

.

Globally#

You can place the Composer PHAR anywhere you want. If you place it in a directory that is part of your PATH, you can access it globally. On Unix systems you can even make it executable and invoke it without directly using the php interpreter.

After running the

installer by following the instructions on the download page

, you can run this to move composer.phar to a directory that is in your path: mv composer.phar /usr/local/bin/composer

If you want to install it just for your user and avoid requiring root permissions, you can use ~/.local/bin instead, which is available by default on some Linux distributions.

Note: If the above fails due to permissions, you may need to run it again with sudo.

Note: In some versions of macOS, the /usr directory does not exist by default. If you receive the error “/usr/local/bin/composer: No such file or directory”, you must create the directory manually before proceeding: mkdir -p /usr/local/bin.

Note: For information on how to change your PATH, read the Wikipedia article and/or use the search engine of your choice.

Now run composer

to run Composer instead of php composer.phar.

Installation – Windows# Using the

installer#

This is the easiest way to set up Composer on your machine

. Download and run

Composer-Setup

.exe. You’ll install the latest version of Composer and configure your PATH so you can call composer from any directory on your command line.

Note: Close your current terminal. Test use with a new terminal: This is important as the PATH is only loaded when the terminal starts.

Manual installation#

Change to a directory in your PATH and run the installer by following the instructions on the download page to download

composer.phar.

Create a new composer file.bat together with

composer.phar: Using

cmd.exe

:C:bin> echo @php “%~dp0composer.phar” %*>composer.bat

Using PowerShell

: PS C:bin> Set-Content composer.bat ‘@php “%~dp0composer.phar” %*’

Add the directory to the PATH environment variable if it is not already used. For information on how to change your PATH variable, refer to this article and/or use the search engine of your choice.

Close your current terminal. Test the use with a new terminal

: C:Usersusername>composer -V Composer version 2.4.0 2022-08-16 16:10:48 Docker

Image#

Composer

is published as a Docker container in some places, see the list in the

composer/docker README file. Usage example: docker pull composer/composer docker run -rm -it -v “$(pwd):/app” composer/composer install

To add Composer to an existing Dockerfile, you can simply copy the binary file from the pre-built version, Low-sized images

: # Latest COPY version -from=composer/composer:latest-bin /composer /usr/bin/composer # Specific version COPY -from=composer/composer:2-bin /composer /usr/bin/composer

Read the image description to learn more about usage

. Note:

Docker-specific issues should be archived to the composer/docker repository. note

: You can also use composer instead of composer/composer as the image name above. It is shorter and is an official Docker image but it is not published directly by us and therefore usually receives new releases with a delay of a few days. Important: images with short aliases do not have binary only equivalents, so for the COPY -from approach it is better to use the composer / composer ones.

Using Composer#

Now that you’ve installed Composer, you’re ready to go! Head to the next chapter for a brief demonstration.

Basic use →

Have you found a typo? Are there any problems with this documentation? Fork and edit it!

Contact US