Some PHP-based applications require extra libraries or components that you need to manage. Manual management is a very time-consuming and tedious process, but Composer can help.
Composer is a dependency manager for PHP that helps developers to manage dependencies used in a PHP project. It provides an easier way to specify the set of all required libraries and dependencies of your project and install them. It is used by Symfony and Laravel to bootstrap new projects.
In this post, we will explain how to install PHP Composer on Debian 11.
Step 1 – Install PHP Composer on Debian 11
Before installing Composer, PHP and some other extensions must be installed to your system. You can install all of them by running the following command:
apt-get install php php-common php-cli php-curl php-intl php-zip git curl unzip -y
Once all the packages are installed, run the following command to install Composer:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" php composer-setup.php --install-dir=/usr/local/bin --filename=composer
You will get the following output:
All settings correct for using Composer Downloading... Composer (version 2.1.14) successfully installed to: /usr/local/bin/composer Use it: php /usr/local/bin/composer
Next, set the executable permission to the Composer binary using the command below:
chmod +x /usr/local/bin/composer
Next, verify the Composer version using the following command:
composer --version
You will get the following output:
Composer version 2.1.14 2021-11-30 10:51:43
Step 2 – How to Use PHP Composer
In this section, we will show you how to use Composer.
To list all options available with Composer, run the following command:
composer
You will get the following output:
Continue as root/super user [yes]? yes ______ / ____/___ ____ ___ ____ ____ ________ _____ / / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/ / /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ / \____/\____/_/ /_/ /_/ .___/\____/____/\___/_/ /_/ Composer version 2.1.14 2021-11-30 10:51:43 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.
Now, let’s create a project directory and install some packages.
First, create a directory named app using the following command:
mkdir app
Next, navigate to the app directory and install the carbon package using the following command:
cd app composer require nesbot/carbon
After the installation, list all files and directories created by the package using the following command:
ls -l
You will get the following output:
-rw-r--r-- 1 root root 60 Dec 17 05:22 composer.json -rw-r--r-- 1 root root 18462 Dec 17 05:22 composer.lock drwxr-xr-x 6 root root 4096 Dec 17 05:22 vendor
Now, create a sample application using the following command:
nano sample.php
Add the following lines:
<?php require __DIR__ . '/vendor/autoload.php'; use Carbon\Carbon; printf("Now: %s", Carbon::now());
Save and close the file, then run the PHP application using the following command:
php sample.php
You should see the following output:
Now: 2021-12-17 05:22:44
To update all the dependencies, run the following command:
composer update
To remove the installed package, run the following command:
composer remove nesbot/carbon
You should see the following output:
- Removing symfony/translation-contracts (v2.5.0) - Removing symfony/translation (v5.4.1) - Removing symfony/polyfill-php80 (v1.23.1) - Removing symfony/polyfill-mbstring (v1.23.1) - Removing symfony/deprecation-contracts (v2.5.0) - Removing nesbot/carbon (2.55.2) Generating autoload files
Step 3 – How to Upgrade PHP Composer
If you want to upgrade Composer to the latest version, run the following command:
composer self-update
You should see the following output:
You are already using the latest available Composer version 2.1.14 (stable channel).
As you can see, the Composer is already in the latest version. If any newer version is available, you will be prompted after running the self-update command.
Conclusion
In the above guide, you learned how to install PHP Composer on Debian 11. You also learned how to use Composer to install and manage the PHP dependencies. Try using it to manage your PHP project on dedicated hosting from Atlantic.Net!