OSDN Git Service

Regular updates
[twpd/master.git] / composer.md
1 ---
2 title: composer
3 category: CLI
4 layout: 2017/sheet
5 weight: -1
6 authors:
7   - github: benolot
8 updated: 2020-02-23
9 description: |
10   Basic guide on how to use Composer, the PHP Package manager.
11 ---
12
13 All composer commands, depending on your install, may need to use `php composer.phar` in the install folder for composer, instead of global/plain `composer`.
14
15 ### Installing dependencies
16     
17 | Command                                | Description                                                  |
18 | ---                                    | ---                                                          |
19 | `composer install`                     | Downloads and installs all the libraries and dependencies outlined in the `composer.lock` file. If the file does not exist it will look for composer.json and do the same, creating a `composer.lock` file.                          |
20 | ---                                    | ---                                                          |
21 | `composer install --dry-run`           | Simulates the install without installing anything            |
22
23 This command doesn't change any file. If `composer.lock` is not present, it will create it.
24
25 `composer.lock` **should always** be committed to the repository. It has all the information needed to bring the 
26 local dependencies to the last committed state. If that file is modified on the repository, you will need to run 
27 `composer install` again after fetching the changes to update your local dependencies to those on that file.
28
29 ### Updating packages
30
31 | Command                                       | Description                     |
32 | ---                                           | ---                             |
33 | `composer update`                             | Updates all packages             |
34 | `composer update --with-dependencies`         | Updates all packages and its dependencies             |
35 | ---                                           | ---                             |
36 | `composer update vendor/package`              | Updates a certain `package` from `vendor`        |
37 | `composer update vendor/*`                    | Updates all packages from `vendor` |
38 | `composer update --lock`                      | Updates `composer.lock` hash without updating any packages |
39
40 This command changes only the `composer.lock` file.
41
42 ### Updating autoloader
43
44 | Command                    | Description                        |
45 | ---                        | ---                                |
46 | `composer dumpautoload -o` | Generates optimized autoload files |
47
48 ### Adding packages
49
50 | Command                          | Description                                                 |
51 | ---                              | ---                                                         |
52 | `composer require vendor/package`.      | Adds `package` from `vendor` to composer.json's `require` section and installs it             |
53 | ---                              | ---                                                         |
54 | `composer require vendor/package --dev` | Adds `package` from `vendor` to composer.json's `require-dev` section and installs it.            |
55
56 This command changes both the `composer.json` and `composer.lock` files.
57
58 ### Passing versions
59
60 | Command                                         | Description                              |
61 | ----------------------------------------------- | ---------------------------------------- |
62 | `composer require vendor/pkg "1.3.2"`           | Installs `1.3.2`                         |
63 | `composer require vendor/pkg ">=1.3.2"`         | Above or equal `1.3.2`                   |
64 | `composer require vendor/pkg "<1.3.2"`          | Below `1.3.2`                            |
65 | `composer require vendor/pkg "1.3.*"`           | Latest of `>=1.3.0 <1.4.0`               |
66 | `composer require vendor/pkg "~1.3.2"`          | Latest of `>=1.3.2 <1.4.0`               |
67 | `composer require vendor/pkg "~1.3"`            | Latest of `>=1.3.0 <2.0.0`               |
68 | `composer require vendor/pkg "^1.3.2"`          | Latest of `>=1.3.2 <2.0.0`               |
69 | `composer require vendor/pkg "^1.3"`            | Latest of `>=1.3.0 <2.0.0`               |
70 | `composer require vendor/pkg "^0.3.2"`          | Latest of `>=0.3.0 <0.4.0` (for pre-1.0) |
71 | `composer require vendor/pkg "dev-BRANCH_NAME"` | From the branch `BRANCH_NAME`            |
72
73 ### Removing packages
74
75 | Command                   | Description                                                 |
76 | ---                       | ---                                                         |
77 | `composer remove vendor/package` | Removes `vendor/package` from composer.json and uninstalls it      |
78
79 This command changes both the `composer.json` and `composer.lock` files.
80
81 ### Verifying
82
83 | Command                      | Description                                                                |
84 | ---------------------------- | -------------------------------------------------------------------------- |
85 | `composer outdated --direct` | Show only packages that are outdated directly required by the root package |