How to Install & Use PHP Composer on Debian 11 Bullseye

Composer is an application-level package manager for the PHP programming language similar to NPM for Node.Js or PIP for Python. Composer provides a standard format for managing all dependencies of PHP software and the required libraries by downloading all the required PHP packages of your project and managing them for you. It is used by most modern PHP frameworks such as Laravel, Drupal, Magento, and Symfony.

In the following tutorial, you will learn how to download and install composer, along with some essential how to work with composer on Debian 11 Bullseye.

Prerequisites

  • Recommended OS: Debian 11 Bullseye
  • User account: A user account with sudo privilages or root access (su command).
  • Required Packages: wget, PHP 5.3 or higher

Updating Operating System

Update your Debian 11 operating system to make sure all existing packages are up to date:

sudo apt update && sudo apt upgrade

Root or Sudo Access

By default, when you create your account at startup with Debian compared to other distributions, it does not automatically receive sudoers status. You must either have access to the root password to use the su command or visit our tutorial on How to Add a User to Sudoers on Debian.

Install Dependencies

The following packages are required to download composer and for future use. Note, if using a specific version of PHP, you will need to adjust the PHP packages below to suit the version. For example, installing PHP 8.0 will be php8.0-mbstring.

sudo apt install curl wget php-common php-cli php-gd php-mysql php-curl php-intl php-mbstring php-bcmath php-imap php-xml php-zip git unzip

Install PHP Composer

The Composer team has made an official PHP script to install and configure PHP Composer on your system. You can download this by visiting the download page or opening your Debian terminal and executing the following.

PHP Method:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" 

WGET Method:

wget -O composer-setup.php https://getcomposer.org/installer

Now with the setup.php downloaded, it is time to install composer. This can be in two different ways, one in which you can install composer globally, or the second alternative which is to install per PHP application basis.

To install globally:

php composer-setup.php --install-dir=/usr/local/bin --filename=composer
chmod +x /usr/local/bin/composer

To install per application:

cd /example-project/php-application && mkdir -p bin 
php composer-setup.php --install-dir=bin --filename=composer
chmod +x bin/composer

As per above, you will need to navigate to the PHP project directory and create a bin directory for the PHP composer to be installed.

Once done, confirm composer is installed and which build and version it is.

composer --version
Composer version 2.1.8 2021-09-15 13:55:14

Now test your composer installation with the following command:

composer

Example output:

   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 2.1.8 2021-09-15 13:55:14

Usage:
  command [options] [arguments]

Options:
  -h, --help                     Display this help message
  -q, --quiet                    Do not output any message
  -V, --version                  Display this application version
      --ansi                     Force ANSI output
      --no-ansi                  Disable ANSI output
  -n, --no-interaction           Do not ask any interactive question
      --profile                  Display timing and memory usage information
      --no-plugins               Whether to disable plugins.
  -d, --working-dir=WORKING-DIR  If specified, use the given directory as working directory.
      --no-cache                 Prevent use of the cache
  -v|vv|vvv, --verbose           Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:
  about                Shows a short information about Composer.
  archive              Creates an archive of this composer package.
  browse               Opens the package's repository URL or homepage in your browser.
  cc                   Clears composer's internal package cache.
  check-platform-reqs  Check that platform requirements are satisfied.
  clear-cache          Clears composer's internal package cache.
  clearcache           Clears composer's internal package cache.
  config               Sets config options.
  create-project       Creates new project from a package into given directory.
  depends              Shows which packages cause the given package to be installed.
  diagnose             Diagnoses the system to identify common errors.
  dump-autoload        Dumps the autoloader.
  dumpautoload         Dumps the autoloader.
  exec                 Executes a vendored binary/script.
  fund                 Discover how to help fund the maintenance of your dependencies.
  global               Allows running commands in the global composer dir ($COMPOSER_HOME).
  help                 Displays help for a command
  home                 Opens the package's repository URL or homepage in your browser.
  i                    Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json.
  info                 Shows information about packages.
  init                 Creates a basic composer.json file in current directory.
  install              Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json.
  licenses             Shows information about licenses of dependencies.
  list                 Lists commands
  outdated             Shows a list of installed packages that have updates available, including their latest version.
  prohibits            Shows which packages prevent the given package from being installed.
  reinstall            Uninstalls and reinstalls the given package names
  remove               Removes a package from the require or require-dev.
  require              Adds required packages to your composer.json and installs them.
  run                  Runs the scripts defined in composer.json.
  run-script           Runs the scripts defined in composer.json.
  search               Searches for packages.
  self-update          Updates composer.phar to the latest version.
  selfupdate           Updates composer.phar to the latest version.
  show                 Shows information about packages.
  status               Shows a list of locally modified packages.
  suggests             Shows package suggestions.
  u                    Upgrades your dependencies to the latest version according to composer.json, and updates the composer.lock file.
  update               Upgrades your dependencies to the latest version according to composer.json, and updates the composer.lock file.
  upgrade              Upgrades your dependencies to the latest version according to composer.json, and updates the composer.lock file.
  validate             Validates a composer.json and composer.lock.
  why                  Shows which packages cause the given package to be installed.
  why-not              Shows which packages prevent the given package from being installed.

How to work with PHP Composer

The tutorial will show you some essential functions when the PHP composer is installed to test its operation.

First, create your project directory:

mkdir ~/composer-test
cd ~/composer-test

Next, find a package from Packagist, or alternative use the tutorial’s example and run the following command to initialize a new composer.json file and install the carbon package:

composer require psr/log

Example output:

Using version ^1.1 for psr/log
./composer.json has been created
Running composer update psr/log
Loading composer repositories with package information
Updating dependencies
Lock file operations: 1 install, 0 updates, 0 removals
  - Locking psr/log (1.1.4)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Downloading psr/log (1.1.4)
  - Installing psr/log (1.1.4): Extracting archive
Generating autoload files

As above, the composer will automatically create and update the composer.json file along with the dependencies required. Alternatively, you can install and or downgrade existing packages to a specific version number.

Example:

composer require psr/log=1.0

In the project directory, you can list the files and directories within it using the ls command:

ls -l

Example output:

./composer.json has been updated
Running composer update psr/log
Loading composer repositories with package information
Updating dependencies
Lock file operations: 0 installs, 1 update, 0 removals
  - Downgrading psr/log (1.1.4 => 1.0.0)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 0 installs, 1 update, 0 removals
  - Downloading psr/log (1.0.0)
  - Downgrading psr/log (1.1.4 => 1.0.0): Extracting archive
Generating autoload files

A breakdown of the terms and the files listed.

  • composer.json – File created for project and all PHP dependencies.
  • composer.lock – File containing list of all packages and versions.
  • vendor – Directory where the dependencies are downloaded and installed.

With your project, you may want or require to update the dependencies, and this can be done running the following command:

composer update

Example output:

Loading composer repositories with package information
Updating dependencies
Nothing to modify in lock file
Installing dependencies from lock file (including require-dev)
Nothing to install, update or remove
Generating autoload files

As you can see above, no dependencies require updating. If there were, the process would begin.

If you no longer need the module, you can remove it with the composer remove command:

composer remove psr/log

Example output:

./composer.json has been updated
Running composer update psr/log
Loading composer repositories with package information
Updating dependencies
Lock file operations: 0 installs, 0 updates, 1 removal
  - Removing psr/log (1.0.0)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 0 installs, 0 updates, 1 removal
  - Removing psr/log (1.0.0)
Generating autoload files

When removing packages, as with installing, removing will automatically update the composer.json file. You can verify this using the cat command.

cat composer.json

Example before removal of psr/log package:

{
    "require": {
        "psr/log": "1.0"
    }
}

Example after removal of psr/log package:

{
}

How to Upgrade PHP Composer

A handy feature of PHP composer is it can self upgrade to the latest stable version available. Run the following command to upgrade.

composer self-update

Example output:

You are already using the latest available Composer version 2.1.8 (stable channel).

As mentioned above, the PHP composer is already at the latest version. However, if an upgrade is available, you will be prompted to upgrade.

Comments and Conclusion

You have learned how to install PHP composer on your Debian 11 Bullseye system and some functions in the tutorial. Overall, the composer is a pretty popular tool, and no doubt you will encounter this as a developer. One of the better advantages of having a composer is that it deals with PHP dependencies on a per-project basis. You can have multiple projects on the same machine that depend on having various versions of PHP installed.



Follow LinuxCapable.com!

Like to get automatic updates? Follow us on one of our social media accounts!