• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

How to Install Composer on Debian 11

Written by Admin, Updated On January 19, 2023
composer, debian, php
How to Install Composer on Debian 11

Composer is a popular dependency manager for PHP. Composer can install, update and pull in all the required PHP packages to your project directory. At the time of installing package, composer will check for dependencies of package and if dependent package are there then it will also install dependencies. In this tutorial, we have described how to install and use Composer on Debian 11 Bullseye.

Prerequisites#

Before you start to install Composer, You should logged in as non-root user account with sudo privileges. As well as PHP installed on you Debian 11 system.

Installing PHP Composer#

Follow the below steps to install Composer on your Debian 11 Bullseye:

Step 1 – Update Package Index#

At first, you have to update the packages index list and upgrade system by issuing below given commands:

sudo apt update && sudo apt upgrade

Step 2 – Install Dependencies#

You need to install some of dependencies by typing:

sudo apt install curl php-cli php-mbstring git unzip

Step 3 – Download Composer Installer File#

Now execute the below command to download the composer installer file:

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

File composer-setup.php will be downloaded to current working directory.

Step 4 – Verify SHA-384 hash#

After that, check the data integrity of the script by comparing the script SHA-384 hash on the Composer Signatures page.

Here, we are going to use wget command to download the signature of the latest Composer installer and will store it in HASH variable as given below:

HASH="$(wget -q -O - https://composer.github.io/installer.sig)"

Now issue the below command to check that the installation script is not corrupted:

php -r "if (hash_file('SHA384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

You will get following output if the hashes match:

Installer verified

If the hashes don’t match you will see output like Installer corrupt. If this is case you have to download composer installation script again and check hash value until you get Installer Verified output.

Step 5 – Install Composer#

Next, issue the following command to install Composer globally inside /usr/local/bin directory:

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

It will show you output as below:

All settings correct for using Composer
Downloading...
Composer (version 2.5.1) successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer

That’s it. Composer is installed as a system-wide and it will be available for all users.

Step 6 – Check Installation#

At last, you can verify the installation by typing:

composer

The above command will print details as following:

   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 2.5.1 2023-01-12 12:30:12

Usage:
    command [options] [arguments]

Use Composer in PHP Project#

Now the Composer is installed globally on your Debian 11 system and we will show how to use in your php project.

First of all, you have to create a directory which should project root directory. Create directory my-project as a root directory of your project.

sudo mkdir my-project
cd my-project

The next step is to initialize a new composer.json using the composer require command and specify the package we want to download.

In this example, we will create a sample application to print the current time using carbon package.

Execute the below command to initialize a new composer.json and install the carbon package:

composer require nesbot/carbon

After complete the installation you can see that Composer created two files composer.json and composer.lock, and a vendor directory.

ls -l
-rw-r--r-- 1 tecnstuff users   59 Aug 11 00:50 composer.json
-rw-r--r-- 1 tecnstuff users 6820 Aug 11 00:50 composer.lock
drwxr-xr-x 4 tecnstuff users 4096 Aug 11 00:50 vendor
  • The vendor directory contains project dependencies.
  • The composer.lock contains a list of all installed packages including the version of the packages.
  • The composer.json describes the PHP project and all PHP dependencies.

Now, you have installed carbon package, create a new file named test.php and paste the following code inside the file. It will prints the current time.

<?php

require __DIR__ . '/vendor/autoload.php';

use Carbon\Carbon;

printf("Now: %s", Carbon::now());

Run above script by typing:

php test.php

The output should look like this:

Now: 2023-01-12 12:55:45

After that, if you want to update the package then you can use following command:

composer update

Conclusion#

You have successfully learned how to install Composer on your Debian 11 Bullseye. We have also described you how to use Composer to create a simple PHP project. You can get more information about Composer visit the official Composer documentation page.

If you have any question or suggestion please leave comment below.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article How to Install R on Ubuntu 22.04
Next Article   How to Install Ruby on Debian 11 Bullseye

Related Posts

  • How to Install WordPress with Nginx on Debian 11

    How to Install WordPress with Nginx on Debian 11

    March 22, 2023
  • How to Install Apache, MySQL, PHP (LAMP) on Ubuntu 22.04

    How to Install LAMP on Ubuntu 22.04

    March 20, 2023
  • How to Install LEMP Stack on Ubuntu 22.04

    How to Install LEMP Stack on Ubuntu 22.04

    March 18, 2023

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install Microsoft Edge Browser on Ubuntu 22.04
    How to Install Microsoft Edge Browser on Ubuntu 22.04 March 14, 2023
  • How to Install Ruby on Ubuntu 22.04 LTS
    How to Install Ruby on Ubuntu 22.04 LTS February 27, 2023
  • How to Install LEMP Stack on Ubuntu 22.04
    How to Install LEMP Stack on Ubuntu 22.04 March 18, 2023
  • How to Install Set Up Apache Virtual Hosts on Ubuntu 22.04
    How to Set Up Apache Virtual Hosts on Ubuntu 22.04 March 2, 2023
  • How to Install MariaDB on Debian 11 Bullseye
    How to Install MariaDB on Debian 11 Bullseye March 8, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook