January 26, 2022
How to install PHP Composer on Debian 11

How to Install and Use PHP Composer on Debian 11

On this tutorial we will learn how to install PHP Composer version 2.1.14 (Latest version until this tutirial made) on Debian 11 operating system.

Introduction

Composer is a tool for dependency management in PHP (similar to npm for Node.js or pip for Python ). It allows us to declare the libraries of our project depends on and it will manage the installation or update. PHP Composer is used in all modern PHP frameworks and platforms such as Laravel, Symfony, Drupal, and Magento. On this article we will learn how to install PHP Composer on Debian 11 operating system.

Composer Installation On Debian 11

System Requirements

There are several system requirements to install Composer on Linux Debiab 11 system.

  • Composer requires PHP 5.3.2+ to run. A few sensitive php settings and compile flags are also required, but when using the installer you will be warned about any incompatibilities.
  • To install packages from sources instead of plain zip archives, we will need git, svn, fossil or hg depending on how the package is version-controlled.
  • Composer is multi-platform and we strive to make it run equally well on Windows, Linux and macOS.

There are two ways to install Composer: globally as a system wide executable and locally as part of the project. On this tutorial, we will do installation in both options. Before installing Composer, we have to ensure if we already have all the necessary packages installed on our Debian 11 system.

Update Debian 11 System

By updating our Debian 11 system, it will refresh the local list of available packages software to the newer stable version. To update Debian 11 BullEye repositories, we will use the command line:

$ sudo apt-get update
$ sudo apt-get upgrade -y

Install Necessary Packages

We will install necessary packages installed on our Debian 11 system, by submitting command lines below :

$ sudo apt-get install php-cli
$ sudo apt-get install php-zip
$ sudo apt-get install php-mbstring -y

Installing Composer Globally

By installing Composer globally, it will be available for all users, as a system-wide command. To be Globally option, it is simply to be done. By placing the file in a system directory (/usr/local/bin) . The following command installs Composer in the /usr/local/bin directory.

  1. Download Composer package file by using wget command lines:
$ wget -O composer-setup.php https://getcomposer.org/installer
ramansah@otodiginet:~$ wget -O composer-setup.php https://getcomposer.org/installer
--2021-12-16 04:55:01--  https://getcomposer.org/installer
Resolving getcomposer.org (getcomposer.org)... 54.36.53.46, 2001:41d0:302:1100::8:104f
Connecting to getcomposer.org (getcomposer.org)|54.36.53.46|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 57721 (56K) [application/octet-stream]
Saving to: ‘composer-setup.php’

composer-setup.php       100%[================================>]  56.37K   110KB/s    in 0.5s    

2021-12-16 04:55:03 (110 KB/s) - ‘composer-setup.php’ saved [57721/57721]

2. To be Globally installation, we will place the file in a system directory . The following command installs Composer in the /usr/local/bin directory:

$ sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
ramansah@otodiginet:~$ sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
[sudo] password for ramansah:
All settings correct for using Composer
Downloading...

Composer (version 2.1.14) successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer

After all are set, we have Composer installed globally. By using composer command line, we will verify itu, as shown below.

ramansah@otodiginet:~$ composer
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 2.1.14 2021-11-30 10:51:43

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.
PHP Compose version 2.1.14 has been installed on the system (Golbally Installation)
PHP Compose version 2.1.14 has been installed on the system (Golbally Installation)

Installing PHP Composer Locally

To install PHP Composer locally, we should download the file in our project root directory, using command line as shown below :

$ sudo php composer-setup.php --install-dir=/path/to/project

On our example, we will create an new directory called as '/home/ramansah/testcompPHP‘ and we will use it as a project’s root directory. The locally installation will be shown below.

ramansah@otodiginet:~$ sudo php composer-setup.php --install-dir=/home/ramansah/testcompPHP
All settings correct for using Composer
Downloading...

Composer (version 2.1.14) successfully installed to: /home/ramansah/testcompPHP/composer.phar
Use it: php /home/ramansah/testcompPHP/composer.phar

After Composer available on our root directory, then we will update it by uisng command line :

$ sudo composer self-update
ramansah@otodiginet:~$ sudo php composer-setup.php --install-dir=/home/ramansah/testcompPHP
All settings correct for using Composer
Downloading...

Composer (version 2.1.14) successfully installed to: /home/ramansah/testcompPHP/composer.phar
Use it: php /home/ramansah/testcompPHP/composer.phar

ramansah@otodiginet:~$ sudo composer self-update
You are already using the latest available Composer version 2.1.14 (stable channel).
Install Composer Locally
Install Composer Locally

Trying The Composer

Currently, we have Composer installed on our Debian system. On this section, now we will create a PHP project based on Composer. We have a scenario to create a simple project called as Hello World. The project will be done by following tasks : We will start by creating a directory that will be the project root and hold the composer.json file. The following is an example of the steps in using PHP Composer :

  • Create a directory that will be the project root and hold the composer .json file.
$ mkdir testcompPHP
$ cd testcompPHP/

Will be as follow :

ramansah@otodiginet:~$ mkdir testcompPHP
ramansah@otodiginet:~$ cd testcompPHP/
ramansah@otodiginet:~/testcompPHP$ pwd
/home/ramansah/testcompPHP
  • Initialize a new composer.json file using the composer require command, on this example we will create a sample application that will print the current time using a package named carbon.
$ composer require nesbot/carbon
ramansah@otodiginet:~/testcompPHP$ composer require nesbot/carbon
Using version ^2.55 for nesbot/carbon
./composer.json has been created
Running composer update nesbot/carbon
Loading composer repositories with package information
Updating dependencies
Lock file operations: 6 installs, 0 updates, 0 removals
  - Locking nesbot/carbon (2.55.2)
  - Locking symfony/deprecation-contracts (v2.5.0)
  - Locking symfony/polyfill-mbstring (v1.23.1)
  - Locking symfony/polyfill-php80 (v1.23.1)
  - Locking symfony/translation (v5.4.1)
  - Locking symfony/translation-contracts (v2.5.0)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 6 installs, 0 updates, 0 removals
  - Downloading symfony/translation-contracts (v2.5.0)
  - Downloading symfony/polyfill-php80 (v1.23.1)
  - Downloading symfony/polyfill-mbstring (v1.23.1)
  - Downloading symfony/deprecation-contracts (v2.5.0)
  - Downloading symfony/translation (v5.4.1)
  - Downloading nesbot/carbon (2.55.2)
  - Installing symfony/translation-contracts (v2.5.0): Extracting archive
  - Installing symfony/polyfill-php80 (v1.23.1): Extracting archive
  - Installing symfony/polyfill-mbstring (v1.23.1): Extracting archive
  - Installing symfony/deprecation-contracts (v2.5.0): Extracting archive
  - Installing symfony/translation (v5.4.1): Extracting archive
  - Installing nesbot/carbon (2.55.2): Extracting archive
3 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating autoload files
6 packages you are using are looking for funding.
Use the `composer fund` command to find out more!

The file list inside the directory will be :

ramansah@otodiginet:~/testcompPHP$ ls -ltr
total 2268
-rwxr-xr-x 1 root     root     2291189 Dec 16 22:21 composer.phar
-rw-r--r-- 1 ramansah ramansah      60 Dec 16 22:26 composer.json
-rw-r--r-- 1 ramansah ramansah   18462 Dec 16 22:26 composer.lock
drwxr-xr-x 6 ramansah ramansah    4096 Dec 16 22:27 vendor

The description of all files are :

  1. vendor – the directory where the project dependencies are stored.
  2. composer.lock – a file containing a list of all installed packages including the version of the packages.
  3. composer.json – a file describing the PHP project and all PHP dependencies.
  • Create a file named testing.php

Then we will creata a new file, called as ototest1.php. We will use vi command line for this task. Inside the file is as follow :

ramansah@otodiginet:~/testcompPHP$ vi ototest1.php
<?php
require __DIR__ . '/vendor/autoload.php';
use Carbon\Carbon;
printf("Saat ini jam: %s", Carbon::now());

~     
  • Run the script
$ php ototest1.php

We will run the script by using PHP compiler, the output will be :

ramansah@otodiginet:~/testcompPHP$ php ototest1.php
Saat ini jam: 2021-12-16 22:31:07

Conclusion

On this tutorial, we have learn how to install PHP Composer Globally and Locally on Debian 11 operating system, and then we also created a simple project for displaying current times from the system.

Share this article via :

Leave a Reply

Your email address will not be published. Required fields are marked *