--- title: Composer and Packagist isChild: true anchor: composer_and_packagist --- ## Composer and Packagist {#composer_and_packagist_title} Composer is the recommended dependency manager for PHP. List your project's dependencies in a `composer.json` file and, with a few simple commands, Composer will automatically download your project's dependencies and setup autoloading for you. Composer is analogous to NPM in the node.js world, or Bundler in the Ruby world. There is a plethora of PHP libraries that are compatible with Composer and ready to be used in your project. These "packages" are listed on [Packagist], the official repository for Composer-compatible PHP libraries. ### How to Install Composer The safest way to download composer is by [following the official instructions](https://getcomposer.org/download/). This will verify the installer is not corrupt or tampered with. The installer installs a `composer.phar` binary in your _current working directory_. We recommend installing Composer *globally* (e.g. a single copy in `/usr/local/bin`). To do so, run this command next: {% highlight console %} mv composer.phar /usr/local/bin/composer {% endhighlight %} **Note:** If the above fails due to permissions, prefix with `sudo`. To run a locally installed Composer you'd use `php composer.phar`, globally it's simply `composer`. #### Installing on Windows For Windows users the easiest way to get up and running is to use the [ComposerSetup] installer, which performs a global install and sets up your `$PATH` so that you can just call `composer` from any directory in your command line. ### How to Define and Install Dependencies Composer keeps track of your project's dependencies in a file called `composer.json`. You can manage it by hand if you like, or use Composer itself. The `composer require` command adds a project dependency and if you don't have a `composer.json` file, one will be created. Here's an example that adds [Twig] as a dependency of your project. {% highlight console %} composer require twig/twig:^2.0 {% endhighlight %} Alternatively, the `composer init` command will guide you through creating a full `composer.json` file for your project. Either way, once you've created your `composer.json` file you can tell Composer to download and install your dependencies into the `vendor/` directory. This also applies to projects you've downloaded that already provide a `composer.json` file: {% highlight console %} composer install {% endhighlight %} Next, add this line to your application's primary PHP file; this will tell PHP to use Composer's autoloader for your project dependencies. {% highlight php %}