How to Install and Use PHP Composer on Debian 10

PHP Composer is a powerful dependency and package management tool for PHP. Composer for PHP is similar to npm for node js and pip for Python. You can easily install and use PHP Composer on Debian 10 to manage your PHP projects.

Composer pulls all the required PHP packages that your project might need. Furthermore, it also manages the versions and updates the requirements of the existing PHP packages in your project. Most modern PHP frameworks such as Laravel, Magento, Symfony, and Drupal use PHP Composer.

You might want to install PHP Composer on your terminal for your PHP projects. This article demonstrates how you can install and use PHP Composer on Debian 10 and how you can create, use and update PHP projects using PHP Composer.

What You’ll Need

The only requirement for this project is a machine with Debian 10 installed on it. It is available for free, and you can easily download it from their website. You can either use an installation image or a cloud image. Also, you should log in as a non-root user with sudo privileges.

Install PHP Composer on Debian 10

To use PHP Composer on Debian 10, you will have to install it first. This process involves updating the system cache repositories, updating the installed packages, and then installing PHP Composer using its installer.

1. To update the system cache repositories, execute the following command:

sudo apt update
Installation on Debian 10

2. Secondly, after updating the cache, update the installed packages index by running this command:

sudo apt upgrade
Upgrade before installing

3. The next step is to install PHP. You can install this by executing the following command:

sudo apt install php-cli wget unzip php-zip
Install PHP Composer package

4. Now that you have installed all the necessary packages, it is time to install the Composer. The Composer has a package that is written in PHP, so we will use that package. Download the Composer installer using the wget command-line utility:

wget -O composer-setup.php
Set up and Install PHP Composer

Additionally, you can also use the following command to install Composer:

php -r "copy('', 'composer-setup.php');"

This command will save the PHP Composer setup as ‘composer-setup.php‘ in the current working directory. There are two methods to install this application: globally or locally. 

Install Globally

To install it globally, you will need sudo privileges. Installing PHP Composer globally will make it available for all the users in the system. Execute the following command to install it in the /usr/local/bin directory.

To run Composer, just type “composer in the Terminal; the terminal will warn you not to run Composer as a root user. However, if you wish to run it as a root user, type “yes” and press “Enter.” PHP Composer will start running.

Install Locally

To install PHP Composer locally, execute the following command:

sudo php composer-setup.php --install-dir=/project
Install PHP Composer

This will install the composer.phar package in your project directory. You can run it using the php command:

sudo php composer.phar

Use PHP Composer on Debian 10

Now that you have installed PHP Composer on your system, it is time to use it in your project. 

1. Firstly, create a root directory for your project and navigate into that directory:

sudo mkdir ~/my-project-directory

cd ~/my-project-directory

2. Now that the project directory is ready, it is time to create a sample PHP project. For this, you will need a composer.json file. To initialize the composer.json file, you will have to install the carbon package by executing the command given below:

composer require nesbot/carbon

It will give similar output as shown in the image below:

Install PHP Composer - Carbon Output
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 5 installs, 0 updates, 0 removals
  - Installing symfony/translation-contracts (v2.1.3): Downloading (100%)         
  - Installing symfony/polyfill-php80 (v1.18.1): Downloading (100%)         
  - Installing symfony/polyfill-mbstring (v1.18.1): Downloading (100%)         
  - Installing symfony/translation (v5.1.3): Downloading (100%)         
  - Installing nesbot/carbon (2.38.0): Downloading (100%)         
5 packages you are using are looking for funding.
Use the `composer fund` command to find out more.

This command will install three items in your system: composer.json, composer. lock, and a vendor directory. Additionally, you can check these files using the ls -l command. Let’s look at what each file contains:

  • The vendor directory consists of project dependencies.
  • Composer.lock contains the list of all installed packages with their versions.
  • Composer.json enlists the PHP projects along with their dependencies.

If you run ls -l command, you will get something like this:

-rw-r--r-- 1 sidrah users 59 Aug 28 00:50 composer.json
-rw-r--r-- 1 sidrah users 6820 Aug 28 00:50 composer.lock
drwxr-xr-x 4 sidrah users 4096 Aug 28 00:50 vendor

3. Now create a new file sample.php, and write a sample code inside this file. The sample code in this tutorial shows the current time of the system; the script is written below:

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

Let’s look at the description of the code:

  • /vendor/autoload.php will load all the required libraries.
  • Carbon\Carbon is aliasing Carbon package as Carbon.
  • Carbon::now() is using Carbon’s method now() to print the current system time.

4. Finally, run the above PHP script by typing the command given below:

php sample.php

The output would be similar to this:

Now: 2021-08-28 11:04:12

Finally, you have run a PHP script on Debian 10 using PHP Composer!

Update PHP Composer on Debian 10

There are additional tasks that you can do using PHP Composer, such as updating the project dependencies. If there is a new version of PHP Composer and you want to update it on your Debian 10 machine, run the command given below: 

sudo composer update

Executing will update all the dependencies to the latest version. Composer.lock will also be updated to reflect the changes. 

In this tutorial, you have learned how to install essential packages required for PHP Composer. You also have learned how to install PHP Composer – locally and globally – on Debian 10 systems. 

This tutorial explained how to install essential packages, PHP and PHP Composer on Debian 10.02 systems. Furthermore, this tutorial has also explained how to use Composer with PHP projects. To get more information on PHP Composer, you can visit its official documentation page. 

So, that’s a wrap for today! Let us know if you have any comments or suggestions down below. 

If this guide helped you, please share it.

Related Posts