Share your love
PHP Composer Installation Guide on Debian 12 Bookworm
In this guide, we try to provide a PHP Composer Installation Guide on Debian 12 Bookworm. As you must know, Composer is a dependency management tool for PHP. It is used to install and update libraries that are needed for the projects and manage them.
In this tutorial, you will learn to Install Composer on Debian 12 and learn to use it step by step.
Steps To PHP Composer Installation Guide on Debian 12 Bookworm
Before you start your Composer installation, you must have access to your server as a non-root user with sudo privileges and set up a basic firewall. To do this, you can visit this guide on Initial Server Setup with Debian 12 Bookworm.
Then, proceed to the following steps to complete this guide.
Step 1 – Install Required Packages for Composer
First, you must run the system update with the following command:
sudo apt update
Then, use the following command to install the required packages:
sudo apt install php-cli unzip curl -y
Step 2 – Download Composer Installer on Debian 12
In this guide, we want to use the Composer installer script which is written in PHP to install our composer. To download the installer script, run the following curl command:
# cd ~
# sudo curl -sS https://getcomposer.org/installer -o composer-setup.php
Step 3 – Install PHP Composer on Debian 12 Bookworm
At this point, you can easily install your PHP Composer globally by using the following command:
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
In your output, you should see:
Output
All settings correct for using Composer
Downloading...
Composer (version 2.6.2) successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer
Then, you can verify your installation by using the command below:
composer
You should get the following output:
Output
______
/ ____/___ ____ ___ ____ ____ ________ _____
/ / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
/_/
Composer version 2.6.2 2023-09-03 14:09:15
Usage:
command [options] [arguments]
Options:
-h, --help Display help for the given command. When no command is given display help for the list command
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi|--no-ansi Force (or disable --no-ansi) ANSI output
-n, --no-interaction Do not ask any interactive question
--profile Display timing and memory usage information
--no-plugins Whether to disable plugins.
--no-scripts Skips the execution of all scripts defined in composer.json file.
-d, --working-dir=WORKING-DIR If specified, use the given directory as working directory.
--no-cache Prevent use of the cache
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
Available commands:
about Shows a short information about Composer
archive Creates an archive of this composer package
audit Checks for security vulnerability advisories for installed packages
browse [home] Opens the package's repository URL or homepage in your browser
...
Now your composer has been successfully installed on your Debian 12 bookworm and is available system-wide.
Step 4 – How To Use Composer on Debian 12?
In your projects, if you want to use Composer, you will need to have a “composer.json” file. This file tells the composer which dependencies are needed for your project. Also, you don’t need to create the file manually. The Composer itself offers you an interactive way to create a new file based on the user’s input.
At this point, we want to use the PHP package repository called Packagist to show you how to use the composer.
Create a Test Project with Composer
First, you must visit the Packagist official site and search for the composer repository you want. Here we search for Slugify which converts strings to a slug.
As you can see, the cocur/slugify package is the best choice for this purpose.
Then, create a Composer project directory on Debian 12 Bookworm and switch to it with the following commands:
# cd ~
# mkdir slugify
# cd slugify
Next, you must use the composer require command with the repository package you want to install and create the “composer.json” file:
composer require cocur/slugify
Output
Info from https://repo.packagist.org: #StandWithUkraine
Cannot use cocur/slugify's latest version v4.4.0 as it requires ext-mbstring * which is missing from your platform.
./composer.json has been created
Running composer update cocur/slugify
Loading composer repositories with package information
Updating dependencies
Lock file operations: 1 install, 0 updates, 0 removals
- Locking cocur/slugify (v3.0)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
- Downloading cocur/slugify (v3.0)
- Installing cocur/slugify (v3.0): Extracting archive
Generating autoload files
No security vulnerability advisories found
Using version ^3.0 for cocur/slugify
As you can see in your output, Composer automatically decides which version of the package to use. Now use the following command to check your project directory files:
ls -l
Output
total 12
-rw-r--r-- 1 root root 59 Sep 13 05:56 composer.json
-rw-r--r-- 1 root root 3114 Sep 13 05:56 composer.lock
drwxr-xr-x 4 root root 4096 Sep 13 05:56 vendor
You will see that it contains two files the composer.json and the composer.lock, and a vendor directory.
The lock file is used to store information about which versions of each package are installed. The vendor directory is where the project dependencies are located.
Automatically Load Dependencies with Composer
Composer provides an autoload script that you can include in your project to get autoloading working for your project. This file is automatically generated by Composer when you add your first dependency on Debian 12 Bookworm.
You only need to include the vendor/autoload.php file in your PHP scripts before any class instantiation.
Now open a new file named test.php with your favorite text editor, We use vi editor:
vi test.php
Add the following code to the file:
<?php
require __DIR__ . '/vendor/autoload.php';
use Cocur\Slugify\Slugify;
$slugify = new Slugify();
echo $slugify->slugify('Hello World, this is a long sentence and I need to make a slug from it!');
When you are done, save and close the file.
Now you can run the script with the following command:
php test.php
In your output you will see:
Output
hello-world-this-is-a-long-sentence-and-i-need-to-make-a-slug-from-it
Update Composer Dependencies
Use the Composer update command on Debian 12 Bookworm to update your project dependencies to more recent versions:
composer update
If a new version is found and it’s compatible with the version restriction defined in the composer.json file, the Composer will replace the previous version installed.
Also, you can update one or more specific libraries with the following command:
composer update vendor/package vendor2/package2
Note: After you update your dependencies be sure to check the composer.json and the composer.lock files within your version control system. So that others can install these newer versions too.
Conclusion
At this point, you have learned the PHP Composer Installation Guide on Debian 12 Bookworm step by step and learn to use it by creating a sample project with it.
Hope you enjoy it. Also, you may be interested in these articles: