2012-02-18 12:34:07 +00:00
|
|
|
# Introduction
|
|
|
|
|
|
|
|
Composer is a tool for dependency management in PHP. It allows you to declare
|
2015-06-22 12:31:33 +00:00
|
|
|
the libraries your project depends on and it will manage (install/update) them
|
|
|
|
for you.
|
2012-02-18 12:34:07 +00:00
|
|
|
|
|
|
|
## Dependency management
|
|
|
|
|
2015-06-22 12:31:33 +00:00
|
|
|
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-project
|
|
|
|
basis, installing them in a directory (e.g. `vendor`) inside your project. By
|
2020-06-30 15:39:56 +00:00
|
|
|
default, it does not install anything globally. Thus, it is a dependency
|
2020-06-03 07:57:32 +00:00
|
|
|
manager. It does however support a "global" project for convenience via the
|
2016-01-09 19:08:22 +00:00
|
|
|
[global](03-cli.md#global) command.
|
2012-02-18 12:34:07 +00:00
|
|
|
|
2015-06-22 12:31:33 +00:00
|
|
|
This idea is not new and Composer is strongly inspired by node's
|
2017-12-29 10:37:42 +00:00
|
|
|
[npm](https://www.npmjs.com/) and ruby's [bundler](https://bundler.io/).
|
2012-02-18 12:34:07 +00:00
|
|
|
|
2015-06-22 12:31:33 +00:00
|
|
|
Suppose:
|
2012-03-14 03:07:22 +00:00
|
|
|
|
2016-04-19 14:15:03 +00:00
|
|
|
1. You have a project that depends on a number of libraries.
|
2020-02-14 02:11:33 +00:00
|
|
|
2. Some of those libraries depend on other libraries.
|
2012-03-14 03:07:22 +00:00
|
|
|
|
2015-06-22 12:31:33 +00:00
|
|
|
Composer:
|
2012-03-14 03:07:22 +00:00
|
|
|
|
2016-04-19 14:15:03 +00:00
|
|
|
1. Enables you to declare the libraries you depend on.
|
2020-02-14 02:11:33 +00:00
|
|
|
2. Finds out which versions of which packages can and need to be installed, and
|
2015-06-22 12:31:33 +00:00
|
|
|
installs them (meaning it downloads them into your project).
|
2020-02-14 07:43:03 +00:00
|
|
|
3. You can update all your dependencies in one command.
|
2012-02-18 12:34:07 +00:00
|
|
|
|
2015-06-22 12:31:33 +00:00
|
|
|
See the [Basic usage](01-basic-usage.md) chapter for more details on declaring
|
|
|
|
dependencies.
|
2012-02-18 12:34:07 +00:00
|
|
|
|
2013-01-11 21:57:28 +00:00
|
|
|
## System Requirements
|
|
|
|
|
2022-02-23 13:33:14 +00:00
|
|
|
Composer in its latest version requires PHP 7.2.5 to run. A long-term-support
|
|
|
|
version (2.2.x) still offers support for PHP 5.3.2+ in case you are stuck with
|
|
|
|
a legacy PHP version. A few sensitive php settings and compile flags are also
|
|
|
|
required, but when using the installer you will be warned about any
|
|
|
|
incompatibilities.
|
2013-01-11 21:57:28 +00:00
|
|
|
|
2020-10-23 19:52:05 +00:00
|
|
|
To install packages from sources instead of plain zip archives, you will need
|
2016-06-22 05:19:29 +00:00
|
|
|
git, svn, fossil or hg depending on how the package is version-controlled.
|
2013-01-11 21:57:28 +00:00
|
|
|
|
|
|
|
Composer is multi-platform and we strive to make it run equally well on Windows,
|
2018-09-27 11:00:10 +00:00
|
|
|
Linux and macOS.
|
2013-01-11 21:57:28 +00:00
|
|
|
|
2018-09-27 11:00:10 +00:00
|
|
|
## Installation - Linux / Unix / macOS
|
2012-02-18 12:34:07 +00:00
|
|
|
|
2012-03-20 11:50:23 +00:00
|
|
|
### Downloading the Composer Executable
|
2012-03-14 03:07:22 +00:00
|
|
|
|
2015-06-30 06:43:54 +00:00
|
|
|
Composer offers a convenient installer that you can execute directly from the
|
2019-03-09 06:27:34 +00:00
|
|
|
command line. Feel free to [download this file](https://getcomposer.org/installer)
|
2021-10-17 11:51:42 +00:00
|
|
|
or review it on [GitHub](https://github.com/composer/getcomposer.org/blob/main/web/installer)
|
2015-06-30 06:43:54 +00:00
|
|
|
if you wish to know more about the inner workings of the installer. The source
|
|
|
|
is plain PHP.
|
|
|
|
|
2021-12-19 14:15:21 +00:00
|
|
|
There are, in short, two ways to install Composer. Locally as part of your
|
2014-12-13 17:59:14 +00:00
|
|
|
project, or globally as a system wide executable.
|
|
|
|
|
2012-06-13 16:30:51 +00:00
|
|
|
#### Locally
|
|
|
|
|
2020-06-03 07:57:32 +00:00
|
|
|
To install Composer locally, run the installer in your project directory. See
|
2018-03-23 10:37:33 +00:00
|
|
|
[the Download page](https://getcomposer.org/download/) for instructions.
|
2012-02-18 12:34:07 +00:00
|
|
|
|
2018-03-23 10:37:33 +00:00
|
|
|
The installer will check a few PHP settings and then download `composer.phar`
|
|
|
|
to 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 on
|
2015-06-22 12:31:33 +00:00
|
|
|
the command line, amongst other things.
|
2012-02-18 12:34:07 +00:00
|
|
|
|
2018-03-23 10:37:33 +00:00
|
|
|
Now run `php composer.phar` in order to run Composer.
|
2012-02-18 12:34:07 +00:00
|
|
|
|
2012-03-15 12:53:34 +00:00
|
|
|
You can install Composer to a specific directory by using the `--install-dir`
|
2016-02-10 18:41:13 +00:00
|
|
|
option and additionally (re)name it as well using the `--filename` option. When
|
|
|
|
running the installer when following
|
|
|
|
[the Download page instructions](https://getcomposer.org/download/) add the
|
|
|
|
following parameters:
|
2012-03-08 05:41:29 +00:00
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
```sh
|
2016-02-10 18:41:13 +00:00
|
|
|
php composer-setup.php --install-dir=bin --filename=composer
|
2014-05-19 10:17:07 +00:00
|
|
|
```
|
2012-03-08 05:41:29 +00:00
|
|
|
|
2018-03-23 10:37:33 +00:00
|
|
|
Now run `php bin/composer` in order to run Composer.
|
2015-06-22 12:31:33 +00:00
|
|
|
|
2012-06-13 16:30:51 +00:00
|
|
|
#### Globally
|
|
|
|
|
2015-06-22 12:31:33 +00:00
|
|
|
You can place the Composer PHAR anywhere you wish. If you put it in a directory
|
2019-03-09 06:27:34 +00:00
|
|
|
that is part of your `PATH`, you can access it globally. On Unix systems you
|
2015-06-22 12:31:33 +00:00
|
|
|
can even make it executable and invoke it without directly using the `php`
|
|
|
|
interpreter.
|
2012-03-14 03:07:22 +00:00
|
|
|
|
2016-02-10 18:41:13 +00:00
|
|
|
After running the installer following [the Download page instructions](https://getcomposer.org/download/)
|
|
|
|
you can run this to move composer.phar to a directory that is in your path:
|
2012-06-13 16:30:51 +00:00
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
```sh
|
|
|
|
mv composer.phar /usr/local/bin/composer
|
|
|
|
```
|
2012-06-13 16:30:51 +00:00
|
|
|
|
2018-04-12 17:06:24 +00:00
|
|
|
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 some
|
|
|
|
Linux distributions.
|
|
|
|
|
2016-02-10 18:41:13 +00:00
|
|
|
> **Note:** If the above fails due to permissions, you may need to run it again
|
2021-12-19 09:59:13 +00:00
|
|
|
> with `sudo`.
|
2013-04-17 17:21:44 +00:00
|
|
|
|
2018-09-27 11:00:10 +00:00
|
|
|
> **Note:** On some versions of macOS the `/usr` directory does not exist by
|
2015-06-22 12:31:33 +00:00
|
|
|
> default. If you receive the error "/usr/local/bin/composer: No such file or
|
|
|
|
> directory" then you must create the directory manually before proceeding:
|
|
|
|
> `mkdir -p /usr/local/bin`.
|
2015-01-25 22:05:21 +00:00
|
|
|
|
2015-06-22 12:31:33 +00:00
|
|
|
> **Note:** For information on changing your PATH, please read the
|
2019-10-19 18:20:45 +00:00
|
|
|
> [Wikipedia article](https://en.wikipedia.org/wiki/PATH_(variable)) and/or use
|
|
|
|
> your search engine of choice.
|
2015-06-22 12:31:33 +00:00
|
|
|
|
2018-03-23 10:37:33 +00:00
|
|
|
Now run `composer` in order to run Composer instead of `php composer.phar`.
|
2013-10-20 01:54:36 +00:00
|
|
|
|
2012-08-23 16:09:20 +00:00
|
|
|
## Installation - Windows
|
|
|
|
|
2012-10-25 13:17:35 +00:00
|
|
|
### Using the Installer
|
|
|
|
|
|
|
|
This is the easiest way to get Composer set up on your machine.
|
|
|
|
|
2015-06-22 12:31:33 +00:00
|
|
|
Download and run
|
|
|
|
[Composer-Setup.exe](https://getcomposer.org/Composer-Setup.exe). It will
|
2018-03-23 10:37:33 +00:00
|
|
|
install the latest Composer version and set up your PATH so that you can
|
2015-06-22 12:31:33 +00:00
|
|
|
call `composer` from any directory in your command line.
|
2012-10-25 13:17:35 +00:00
|
|
|
|
2015-06-22 12:31:33 +00:00
|
|
|
> **Note:** Close your current terminal. Test usage with a new terminal: This is
|
|
|
|
> important since the PATH only gets loaded when the terminal starts.
|
2014-11-25 16:01:07 +00:00
|
|
|
|
2012-10-25 13:17:35 +00:00
|
|
|
### Manual Installation
|
2012-08-23 16:09:20 +00:00
|
|
|
|
2016-02-10 18:41:13 +00:00
|
|
|
Change to a directory on your `PATH` and run the installer following
|
|
|
|
[the Download page instructions](https://getcomposer.org/download/)
|
|
|
|
to download `composer.phar`.
|
2012-08-23 16:09:20 +00:00
|
|
|
|
2013-07-01 00:52:10 +00:00
|
|
|
Create a new `composer.bat` file alongside `composer.phar`:
|
2012-08-23 16:09:20 +00:00
|
|
|
|
2020-06-03 07:57:32 +00:00
|
|
|
Using cmd.exe:
|
|
|
|
|
|
|
|
```sh
|
|
|
|
C:\bin> echo @php "%~dp0composer.phar" %*>composer.bat
|
|
|
|
```
|
|
|
|
|
|
|
|
Using PowerShell:
|
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
```sh
|
2020-06-03 07:57:32 +00:00
|
|
|
PS C:\bin> Set-Content composer.bat '@php "%~dp0composer.phar" %*'
|
2014-05-19 10:17:07 +00:00
|
|
|
```
|
2012-10-25 13:17:35 +00:00
|
|
|
|
2015-06-02 13:47:52 +00:00
|
|
|
Add the directory to your PATH environment variable if it isn't already.
|
2016-02-10 18:41:13 +00:00
|
|
|
For information on changing your PATH variable, please see
|
2017-12-29 10:33:19 +00:00
|
|
|
[this article](https://www.computerhope.com/issues/ch000549.htm) and/or
|
2019-10-19 18:20:45 +00:00
|
|
|
use your search engine of choice.
|
2014-09-03 15:38:53 +00:00
|
|
|
|
2013-01-29 02:22:09 +00:00
|
|
|
Close your current terminal. Test usage with a new terminal:
|
2012-08-23 16:09:20 +00:00
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
```sh
|
|
|
|
C:\Users\username>composer -V
|
2021-04-09 17:44:29 +00:00
|
|
|
Composer version 2.0.12 2021-04-01 10:14:59
|
2014-05-19 10:17:07 +00:00
|
|
|
```
|
2012-08-23 16:09:20 +00:00
|
|
|
|
2013-01-05 17:46:42 +00:00
|
|
|
## Using Composer
|
2013-01-03 23:05:08 +00:00
|
|
|
|
2015-06-22 12:31:33 +00:00
|
|
|
Now that you've installed Composer, you are ready to use it! Head on over to the
|
2020-10-23 19:52:05 +00:00
|
|
|
next chapter for a short demonstration.
|
2012-03-07 16:35:53 +00:00
|
|
|
|
2015-06-22 12:31:33 +00:00
|
|
|
[Basic usage](01-basic-usage.md) →
|