Introduction - Composer (2024)

  • Dependency management
  • System Requirements
  • Installation - Linux / Unix / macOS
    • Downloading the Composer Executable
      • Locally
      • Globally
  • Installation - Windows
    • Using the Installer
    • Manual Installation
  • Docker Image
  • Using Composer

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

Dependency management#

Composer is not a package manager in the same sense as Yum or Apt are. Yes,it deals with "packages" or libraries, but it manages them on a per-projectbasis, installing them in a directory (e.g. vendor) inside your project. Bydefault, it does not install anything globally. Thus, it is a dependencymanager. It does however support a "global" project for convenience via theglobal command.

This idea is not new and Composer is strongly inspired by node'snpm and ruby's bundler.

Suppose:

  1. You have a project that depends on a number of libraries.
  2. Some of those libraries depend on other libraries.

Composer:

  1. Enables you to declare the libraries you depend on.
  2. Finds out which versions of which packages can and need to be installed, andinstalls them (meaning it downloads them into your project).
  3. You can update all your dependencies in one command.

See the Basic usage chapter for more details on declaringdependencies.

System Requirements#

Composer in its latest version requires PHP 7.2.5 to run. A long-term-supportversion (2.2.x) still offers support for PHP 5.3.2+ in case you are stuck witha legacy PHP version. A few sensitive php settings and compile flags are alsorequired, but when using the installer you will be warned about anyincompatibilities.

Composer needs several supporting applications to work effectively, making theprocess of handling package dependencies more efficient. For decompressingfiles, Composer relies on tools like 7z (or 7zz), gzip, tar, unrar,unzip and xz. As for version control systems, Composer integrates seamlesslywith Fossil, Git, Mercurial, Perforce and Subversion, thereby ensuring theapplication's smooth operation and management of library repositories. Beforeusing Composer, ensure that these dependencies are correctly installed on yoursystem.

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

Installation - Linux / Unix / macOS#

Downloading the Composer Executable#

Composer offers a convenient installer that you can execute directly from thecommand line. Feel free to download this fileor review it on GitHubif you wish to know more about the inner workings of the installer. The sourceis plain PHP.

There are, in short, two ways to install Composer. Locally as part of yourproject, or globally as a system wide executable.

Locally#

To install Composer locally, run the installer in your project directory. Seethe Download page for instructions.

The installer will check a few PHP settings and then download composer.pharto your working directory. This file is the Composer binary. It is a PHAR(PHP archive), which is an archive format for PHP which can be run onthe command line, amongst other things.

Now run php composer.phar in order to run Composer.

You can install Composer to a specific directory by using the --install-diroption and additionally (re)name it as well using the --filename option. Whenrunning the installer when followingthe Download page instructions add thefollowing parameters:

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

Now run php bin/composer in order to run Composer.

Globally#

You can place the Composer PHAR anywhere you wish. If you put it in a directorythat is part of your PATH, you can access it globally. On Unix systems youcan even make it executable and invoke it without directly using the phpinterpreter.

After running the installer following the Download page instructionsyou can run this to move composer.phar to a directory that is in your path:

mv composer.phar /usr/local/bin/composer

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

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

Note: On some versions of macOS the /usr directory does not exist bydefault. If you receive the error "/usr/local/bin/composer: No such file ordirectory" then you must create the directory manually before proceeding:mkdir -p /usr/local/bin.

Note: For information on changing your PATH, please read theWikipedia article and/or useyour search engine of choice.

Now run composer in order to run Composer instead of php composer.phar.

Installation - Windows#

Using the Installer#

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

Download and runComposer-Setup.exe. It willinstall the latest Composer version and set up your PATH so that you cancall composer from any directory in your command line.

Note: Close your current terminal. Test usage with a new terminal: This isimportant since the PATH only gets loaded when the terminal starts.

Manual Installation#

Change to a directory on your PATH and run the installer followingthe Download page instructionsto download composer.phar.

Create a new composer.bat file alongside 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 your PATH environment variable if it isn't already.For information on changing your PATH variable, please seethis article and/oruse your search engine of choice.

Close your current terminal. Test usage with a new terminal:

C:\Users\username>composer -V
Composer version 2.4.0 2022-08-16 16:10:48

Docker Image#

Composer is published as Docker container in a few places, see the list in the composer/docker README.

Example usage:

docker pull composer/composerdocker run --rm -it -v "$(pwd):/app" composer/composer install

To add Composer to an existing Dockerfile you can simply copy binary file from pre-built, low-size images:

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

Read the image description for further usage information.

Note: Docker specific issues should be filed on the composer/docker repository.

Note: You may also use composer instead of composer/composer as image name above. It is shorter and is a Docker official image but is not published directly by us and thus usually receives new releases with a delay of a few days. Important: short-aliased images don't have binary-only equivalents, so for COPY --from approach it's better to use composer/composer ones.

Using Composer#

Now that you've installed Composer, you are ready to use it! Head on over to thenext chapter for a short demonstration.

Basic usage

Found a typo? Something is wrong in this documentation? Fork and edit it!

Introduction - Composer (2024)

References

Top Articles
Latest Posts
Article information

Author: Mrs. Angelic Larkin

Last Updated:

Views: 6289

Rating: 4.7 / 5 (67 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Mrs. Angelic Larkin

Birthday: 1992-06-28

Address: Apt. 413 8275 Mueller Overpass, South Magnolia, IA 99527-6023

Phone: +6824704719725

Job: District Real-Estate Facilitator

Hobby: Letterboxing, Vacation, Poi, Homebrewing, Mountain biking, Slacklining, Cabaret

Introduction: My name is Mrs. Angelic Larkin, I am a cute, charming, funny, determined, inexpensive, joyous, cheerful person who loves writing and wants to share my knowledge and understanding with you.