PHP Composer - Dependency Manager for PHP

// Web Development //

Composer: How to install and use Composer for PHP

by Dillon - Updated -

Composer is a dependency manager for PHP. First released in 2012, Composer, and its adoption by popular frameworks such as Laravel, has single-handedly driven the rise in PHP adoption in recent years. In this post, I will show you how to install and use Composer.

How to install Composer

To get started, visit the download page on the official website for Composer. To install Composer, follow the sets outlined in the documentation.

Copy the commands from the website and execute them in a terminal. To begin with, Composer will be installed in the current directory. Later, we will move the composer.phar file to a directory in your PATH so it can be used system-wide.

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '906a84df04cea2aa72f40b5f787e49f22d4c2f19492ac310e8cba5b96ac8b64115ac402c8cd292b8a03482574915d1a8') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

The hash will change, so ensure you use the commands from the official website.

Move composer.phar to bin

Next, move composer.phar to a directory in your PATH so you can call composer globally.

sudo mv composer.phar /usr/local/bin/composer

Finally, test the install worked correctly by calling composer from the terminal.

Check composer version

How to update Composer to version 2

In 2020, Composer 2.0 was released. Version 2.0 came with a number of improvements over version 1 such as:

  • Speed improvements
  • Error reporting improvements
  • Platform checks during runtime

To update Composer to version 2, run the following command in the terminal.

composer self-update --2

If you don’t have Composer in your PATH, you will need to run:

composer.phar self-update --2

How to downgrade Composer to version 1

If you encounter issues with version 2.0 or are using an older version of a framework that utilizes Composer you may need to downgrade to version 1. To downgrade to version 1 run the following in the terminal.

composer self-update --1

If you don’t have Composer in your PATH, you will need to run:

composer.phar self-update --1

How to use Composer

To use Composer in your projects all you need is a composer.json file in the root directory of your project. This file describes the dependencies required for your project.

First, head over to Packagist to find the packages you want to use in your project.

In the composer.json file, specify the require key. Within the require object, add the package name and its version constraint.

In this example, we will be checking our platform for PHP 8.0 and installing the Microsoft Graph PHP SDK.

{
    "require": {
        "php": "8.0",
        "microsoft/microsoft-graph": "^1.35",
    }
}

To install your dependencies, first you should run composer update. This creates a composer.lock file, which locks your project to the specified package versions. After creating the lock file composer will run checks and install the dependencies in the lock file to the vendors/ directory.

You should commit the lock file to your repo so any other developers who are working on the same project are also locked to the specified dependency versions.

To install other dependencies, you can either add them manually to your composer.json file or install them directly from the terminal:

composer install microsoft/microsoft-graph

The above command will install the latest version of the Microsoft Graph PHP SDK.

When working on a project that already has a composer.lock file, you should run composer install.

Check out this Composer Cheatsheet.

Related Posts

Dillon Smart

👋 Hey, I'm Dillon!

I'm a Full Stack developer from the U.K. I've been building websites and applications for 11 years! Along the way I've picked up a few things, and share them here on IKnowThatNow.com.

Feel free to reach out on Twitter!

0 Comment

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.