DebianLinuxTutorials

How To Install PHP Composer on Debian 11

Install PHP Composer on Debian 11

In this tutorial, we will show you how to install PHP Composer on Debian 11. For those of you who didn’t know, Composer is a project dependency manager for PHP programming. Composer provides a standard format for managing all dependencies of PHP software and the required libraries by downloading all the required PHP packages for your project and managing them for you. It is used by most modern PHP frameworks such as Laravel, Drupal, Magento, and Symfony.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you the step-by-step installation of PHP Composer on a Debian 11 (Bullseye).

Prerequisites

  • A server running one of the following operating systems: Debian 11 (Bullseye).
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • SSH access to the server (or just open Terminal if you’re on a desktop).
  • A non-root sudo user or access to the root user. We recommend acting as a non-root sudo user, however, as you can harm your system if you’re not careful when acting as the root.

Install PHP Composer on Debian 11 Bullseye

Step 1. Before we install any software, it’s important to make sure your system is up to date by running the following apt commands in the terminal:

sudo apt update
sudo apt upgrade
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

Step 2. Installing PHP Composer on Debian 11.

Now we install Composer on the Debian system, you have to run this single command:

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

To install Composer globally inside the /usr/local/bin directory by running the following command below:

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

Verify the installed version:

composer --version

Then, test your Composer installation with the following command:

composer

Output:

   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 2.1.8 2021-09-18 14:22:11

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

Step 3. Using PHP Composer.

First, create your Composer project directory:

mkdir ~/my-composer-project
cd ~/my-composer-project

Next, run the following command to initialize a new composer.json file and install the carbon package:

composer require nesbot/carbon

Once the installation is complete, you can see that Composer created two files composer.json and composer.lock along with a vendor directory:

ls -l

Output:

root@idroot.us:~/my-composer-project# ls -l
total 28
-rw-r--r-- 1 root root    60 Sep  6 08:21 composer.json
-rw-r--r-- 1 root root 18210 Sep  6 08:21 composer.lock
drwxr-xr-x 6 root root  4096 Sep  6 08:21 vendor

After that, now create a new file named testing.php and paste the following file:

<?php
require __DIR__ . '/vendor/autoload.php';
use Carbon\Carbon;
printf("Now: %s", Carbon::now());

Next, run the script by running the following command below:

php testing.php

Output:

root@server:~/my-composer-project# php testing.php 
Now: 2021-09-30 08:33:21

Congratulations! You have successfully installed PHP Composer. Thanks for using this tutorial for installing the latest version of PHP Composer on Debian 11 Bullseye. For additional help or useful information, we recommend you check the official Composer website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is a seasoned Linux system administrator with a wealth of experience in the field. Known for his contributions to idroot.us, r00t has authored numerous tutorials and guides, helping users navigate the complexities of Linux systems. His expertise spans across various Linux distributions, including Ubuntu, CentOS, and Debian. r00t's work is characterized by his ability to simplify complex concepts, making Linux more accessible to users of all skill levels. His dedication to the Linux community and his commitment to sharing knowledge makes him a respected figure in the field.
Back to top button