Composer

Composer is a popular package manager for PHP. It's used to install packages (libraries/frameworks/...) rather seamlessly.

$ sudo apt install composer

Composer uses a composer.json to track which libraries have to be installed, and which versions can be downloaded/installed, based on rules defined by the user (see versions).

Downloaded packages are stored in vendor/.

To use libraries inside vendor in your script, you need to import them using the autoloader:

require_once __DIR__ . '/vendor/autoload.php;
  • To create a composer.json, use composer init.
  • To generate vendor/ from an existing composer.json
$ composer install # install all dependencies
$ composer install --no-dev # aside from dev dependencies
  • To add new packages to composer.json and install them, use
$ composer require package
$ composer require vendor/package
$ composer require vendor/package:version
$ composer require --dev [...] # save as dev dependency

Installing packages will generate a composer.json.lock. This file keeps track of the exact version of packages inside vendor/. You can use composer update to update them.

➑️ Note that packages are updated according to the ranges/rules defined in composer.json, so even if a package has updates, it may not be updated, if the update is not within the allowed range.


πŸ‘» To-do πŸ‘»

Stuff that I found, but never read/used yet.

  • composer require --dev
  • php composer.phar -v (no install required)