Composer is a dependency manager for PHP (similar to npm for Node.js or pip for Python).
Composer will link all the required PHP packages that your project depends on and manage them for you. It is used in all modern PHP frameworks and platforms such as Laravel, Symfony, Drupal and Magento.
This article explains how to install Composer on Debian 10 systems. We also show you how to use Composer to create and manage PHP projects.
Installing Composer on Debian
Before installing Composer, make sure you have all the required packages installed on your Debian system:
sudo apt update sudo apt install wget php-cli php-zip unzip
Composer offers an installer written in PHP that we will use to install Composer.
Download the installer using wget:
wget -O composer-setup.php https://getcomposer.org/installer
The above command will save the composer-setup.php file in the current working directory.
Composer is a single file CLI application that can be installed either globally or as part of a project. Global installation requires 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 that is on your PATH. The following command installs Composer into the / usr / local / bin directory:
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
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
You can now use Composer by running its composer in your terminal.
- To install composer locally, upload the file to your project’s root directory:
sudo php composer-setup.php --install-dir=/path/to/project
This command will download a file named composer.phar. To use Composer go to your project directory and run php composer.phar
When a new version of Composer is available, you can update your installation using the following command:
sudo composer self-update
Getting Started with Composer
Now that Composer is installed on your Debian system, we will show you how to create a PHP project.
Start by creating a directory that will be the root of the project and contain the composer.json file. This file describes your PHP project, including PHP dependencies and other metadata.
Run the following commands to create and switch to the project directory using:
mkdir ~/my-first-composer-projectcd ~/my-first-composer-project
We then initialize a new composer.json file with the command composer require
Run the following command to initialize a new composer.json file and install the carbon package:
composer require nesbot/carbon
./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, downloads and installs carbon and all of its dependencies.
If you list your project directory with the ls command, you will see that it contains two files composer.json and composer.lock, the vendor directory.
-rw-r--r-- 1 andreyex users 60 Aug 18 20:02 composer.json -rw-r--r-- 1 andreyex users 6851 Aug 18 20:02 composer.lock drwxr-xr-x 5 andreyex users 4096 Aug 18 20:02 vendor
- vendor is the directory where project dependencies are stored.
- composer.lock is a file containing a list of all installed packages, including package version.
- composer.json – a file with a description of the PHP project and all PHP dependencies.
You can search for PHP packages in the Composer Packagist repository.
Composer provides autoloading capabilities that allow you to use PHP classes without the need for require or include files.
Create a file called testing.php and paste the following code:
<?php require __DIR__ . '/vendor/autoload.php'; use CarbonCarbon; printf("Сейчас: %s", Carbon::now());
Let’s analyze the code line by line.
On the first line after the opening php tag, we include the vendor / autoload.php file, which was automatically generated by Composer. This file will automatically download all required libraries.
Next, we change Carbon Carbon to Carbon and on the last line we print the current time using the now command.
Run the script by typing:
The result should look something like this:
Сейчас: 2020-08-18 20:08:45
Later, if you need to update the project packages, enter:
The above command will check for newer versions of the packages installed, and if a newer version is found and the version limit matches the limit specified in composer.json, Composer will update the package.
We showed you how to install Composer on Debian 10 and how to use it to create a basic PHP project.
For more information on Composer visit the official documentation page.
If you have any questions, please leave a comment below.