1
0
Fork 0
composer/doc/00-intro.md

198 lines
7.2 KiB
Markdown
Raw Normal View History

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
default, it does not install anything globally. Thus, it is a dependency
manager. It does however support a "global" project for convenience via the
[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
[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:
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.
2015-06-22 12:31:33 +00:00
Composer:
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
## 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.
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.
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.
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
2015-06-30 06:43:54 +00:00
Composer offers a convenient installer that you can execute directly from the
command line. Feel free to [download this file](https://getcomposer.org/installer)
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.
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.
#### Locally
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
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:
```shell
2016-02-10 18:41:13 +00:00
php composer-setup.php --install-dir=bin --filename=composer
```
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
#### 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
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.
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:
```shell
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 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-06-22 12:31:33 +00:00
> **Note:** For information on changing your PATH, please read the
> [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`.
## 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
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`.
2013-07-01 00:52:10 +00:00
Create a new `composer.bat` file alongside `composer.phar`:
Using cmd.exe:
```shell
C:\bin> echo @php "%~dp0composer.phar" %*>composer.bat
```
Using PowerShell:
```shell
PS C:\bin> Set-Content composer.bat '@php "%~dp0composer.phar" %*'
```
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
use your search engine of choice.
Close your current terminal. Test usage with a new terminal:
```shell
C:\Users\username>composer -V
```
```text
Composer version 2.4.0 2022-08-16 16:10:48
```
2022-06-06 08:18:22 +00:00
## Docker Image
Composer is published as Docker container in a few places, see the list in the [composer/docker README](https://github.com/composer/docker).
Example usage:
```shell
2022-06-06 08:18:22 +00:00
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 binary file from pre-built, low-size images:
2022-06-06 08:18:22 +00:00
```Dockerfile
# Latest release
COPY --from=composer/composer:latest-bin /composer /usr/bin/composer
# Specific release
COPY --from=composer/composer:2-bin /composer /usr/bin/composer
2022-06-06 08:18:22 +00:00
```
Read the [image description](https://hub.docker.com/r/composer/composer) for further usage information.
**Note:** Docker specific issues should be filed [on the composer/docker repository](https://github.com/composer/docker/issues).
**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.
2022-06-06 08:18:22 +00:00
## Using Composer
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
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) →