Linux

How to Install and Use PHP Composer on Debian 10

How to Install and Use PHP Composer on Debian 10

Composer is a dependency manager for PHP (similar to npm for Node.js or pip for Python).

Composer will pull all the necessary PHP packages on which your project depends and manage them for you. It is used across all modern PHP frameworks and platforms such as Laravel, Symfony, Drupal, and Magento.

This article describes how to install Composer on a Debian 10 system. We will also cover how to use Composer to create and manage PHP projects.

Installing Composer on Debian

Before installing Composer, make sure you have installed all the required packages on your Debian system:

sudo apt update
sudo apt install wget php-cli php-zip unzip

Composer offers an installer written in PHP which we will use to install Composer.

Download the installer with wget:

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

The command above will save the file as composer-setup.php in the current working directory.

Composer is a single file CLI application that can be installed globally or as part of a project. Global installs require sudo privileges.

  • To install Composer globally as a system-wide command that will be available to all users, simply place the file in a directory present in the system’s PATH. The following command installs Composer in the / usr / local / bin directory:
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer

Output :

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

Now you can use Composer by running composer in your terminal.

  • To install composer locally, download the files in your project’s root directory:
sudo php composer-setup.php --install-dir=/path/to/project

This will download a file called composer.phar. To use Composer, go to the project directory and run php composer.phar

When a new version of Composer is available, you can update the installation using the following command:

sudo composer self-update

Getting Started with Composer

Now that you have Composer installed on your Debian system, we will show you how to create a PHP project.

Start by creating a directory that will be the project root and holding down the composer.json file. This file describes your PHP project, including PHP dependencies and other metadata.

Run the following command to create a project directory and switch to it with:

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

Next, we will initialize our new composer.json file using the composer require command and specify the package we want to download. In this example, we are going to create a sample application that will print the current time using a package named carbon.

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

composer require nesbot/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%)         
symfony/polyfill-mbstring suggests installing ext-mbstring (For best performance)
symfony/translation suggests installing symfony/config
symfony/translation suggests installing symfony/yaml
symfony/translation suggests installing psr/log-implementation (To use logging capability in translator)
Writing lock file
Generating autoload files
5 packages you are using are looking for funding.
Use the `composer fund` command to find out more

As shown in the output, Composer creates a composer.json file and downloads and installs carbon and all of its dependencies.

If you list your project directory with ls, you’ll see that it contains two composer.json and composer.lock files, and a vendor directory.

ls -l

Output :

-rw-r--r-- 1 uberhowto users   60 Aug 17 21:02 composer.json
-rw-r--r-- 1 uberhowto users 6851 Aug 17 21:02 composer.lock
drwxr-xr-x 5 uberhowto users 4096 Aug 17 21:02 vendor
  • vendor – directory where project dependencies are stored.
  • Composer.lock – file that lists all installed packages including package versions.
  • composer.json – file describing the PHP project and all PHP dependencies.

Composer provides auto-load capabilities that let you use PHP classes without needing or including files.

Create a file called testing.php and paste the following code:

<?php

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

use Carbon\Carbon;

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

Let’s analyze the code line by line.

In the first line after the opening php tag, we include the vendor / autoload.php file which Composer generates automatically. This file will automatically load all necessary libraries.

Next, we’re aliasing Carbon \ Carbon as Carbon, and in the last line, we’re printing the current time using the Carbon now method.

Run the script by typing:

php testing.php

The output will look something like below:

Output :
Now: 2020-08-17 21:08:45

Later, if you need to update the project package, enter:

composer update

The command above will check for a newer version of the installed package, and if a newer version is found and the version limitation matches the one specified in composer.json, Composer will update the package.

Conclusion

We’ve shown you how to install Composer on Debian 10 and how to use it to create basic PHP projects.

For more information on Composer, visit the official documentation page.

If you have any questions, feel free to leave a comment below.

Related posts

How to Grep Way for Various Strings and Patterns

Linux

How to Manage Startup Programs on Ubuntu Linux

Linux

How to set up NFS Server on CentOS

Linux

How to integrate the ONLYOFFICE editor with the Nuxeo Platform

Linux

How to Install Drupal 9 with Nginx and Let’s Encrypt SSL on Debian 10

Linux

How to Install Android Studio on Ubuntu 20.04

Linux

How to install Lightworks on Ubuntu

Linux

How to Install Apache Cassandra on Ubuntu 20.04

Linux

How to Install and Configure Fail2ban on Ubuntu 20.04

Linux