Best PHP Composer Installation Guide on Debian 12 Bookworm

Posted on

Best PHP Composer Installation Guide on Debian 12 Bookworm

In this guide, we aim to provide a comprehensive PHP Composer Installation Guide on Debian 12 Bookworm. As you likely know, Composer is an indispensable dependency management tool for PHP. It streamlines the process of installing, updating, and managing the various libraries and packages your projects rely on.

This tutorial, brought to you by Orcacore, will walk you through the process of installing Composer on Debian 12 Bookworm and demonstrate how to effectively utilize it in your PHP projects. By the end of this guide, you’ll have a solid understanding of Composer and be able to leverage its capabilities to improve your development workflow. This PHP Composer Installation Guide on Debian 12 Bookworm is a step-by-step guide to help you get started.

Before embarking on this PHP Composer Installation Guide on Debian 12 Bookworm, ensure you have the following prerequisites in place:

  • Access to a Debian 12 Bookworm server as a non-root user with sudo privileges.
  • A basic firewall configured.

If you haven’t already done so, you can refer to the Orcacore guide on Initial Server Setup with Debian 12 Bookworm for detailed instructions.

With the prerequisites addressed, let’s proceed with the following steps to complete the PHP Composer Installation Guide on Debian 12 Bookworm.

Step 1 – Install Required Packages for Composer

First, update your system’s package index:

sudo apt update

Next, install the necessary packages: php-cli, unzip, and curl. These packages are essential for running PHP scripts from the command line, extracting compressed files, and downloading files from the internet, respectively.

sudo apt install php-cli unzip curl -y

Step 2 – Download Composer Installer on Debian 12

We’ll use the official Composer installer script, written in PHP, to install Composer. Download the installer script using the following curl command:

# cd ~
# sudo curl -sS https://getcomposer.org/installer -o composer-setup.php

This command downloads the installer script from the official Composer website and saves it as composer-setup.php in your home directory. The -sS flags ensure that curl operates silently and displays errors if they occur.

Step 3 – Install PHP Composer on Debian 12 Bookworm

Now, you can install Composer globally using the following command. This command executes the composer-setup.php script using PHP and instructs it to install Composer in the /usr/local/bin directory with the filename composer. Installing Composer globally makes it accessible from any directory in your system.

sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer

You should see output similar to this:

**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

To verify that Composer is installed correctly, run the following command:

composer

This should display the Composer version and a list of available commands, indicating that Composer is properly installed and configured.

**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
...

You have now successfully completed the PHP Composer Installation Guide on Debian 12 Bookworm, making Composer available system-wide.

Step 4 – How To Use Composer on Debian 12?

To utilize Composer in your projects, you’ll need a composer.json file. This file serves as a manifest, declaring the dependencies required for your project. Composer uses this file to manage the installation and updating of these dependencies. You don’t have to create this file manually; Composer provides an interactive way to generate it based on your input.

In this step of the PHP Composer Installation Guide on Debian 12 Bookworm, we’ll demonstrate how to use Composer with the PHP package repository called Packagist.

Create a Test Project with Composer

Visit the Packagist official site and search for the desired package. In this example, we’ll search for "Slugify," a package that converts strings into slugs.

The cocur/slugify package is a popular and reliable choice for this purpose.

Create a new directory for your Composer project and navigate into it:

# cd ~
# mkdir slugify
# cd slugify

Now, use the composer require command, specifying the package you want to install (cocur/slugify). This command will also generate the composer.json file.

composer require cocur/slugify
Create a Test Project with Composer on Debian 12
PHP Composer Installation Guide on Debian 12 Bookworm – Create a Test Project

Composer automatically determines the appropriate version of the package to use. Verify the project directory contents:

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’ll see the composer.json and composer.lock files, along with a vendor directory.

The composer.lock file stores information about the specific versions of each package installed. The vendor directory is where the project dependencies are located.

Automatically Load Dependencies with Composer

Composer provides an autoload script that simplifies the process of including your project’s dependencies. This file is automatically generated when you add your first dependency.

Simply include the vendor/autoload.php file in your PHP scripts before any class instantiation.

Create a new file named test.php using your preferred text editor (e.g., vi):

vi test.php

Add the following code to the file:

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

use CocurSlugifySlugify;

$slugify = new Slugify();

echo $slugify->slugify('Hello World, this is a long sentence and I need to make a slug from it!');

Save and close the file.

Run the script:

php test.php

The output should be:

**Output**
hello-world-this-is-a-long-sentence-and-i-need-to-make-a-slug-from-it

Update Composer Dependencies

To update your project’s dependencies to newer versions, use the composer update command:

composer update

If a new version is available and compatible with the version constraints defined in the composer.json file, Composer will update the package.

You can also update specific packages:

composer update vendor/package vendor2/package2

Note: After updating dependencies, remember to commit the changes to the composer.json and composer.lock files to your version control system. This ensures that other developers working on the project will use the same versions of the dependencies.

Conclusion

You’ve now successfully completed the PHP Composer Installation Guide on Debian 12 Bookworm and learned how to use Composer to manage dependencies in your PHP projects.

We hope you found this guide helpful. You might also be interested in these articles:

Install Symfony PHP Framework on Debian 12

Install and Secure phpMyAdmin on Debian 12

Alternative Solutions for Dependency Management

While Composer is the de facto standard for PHP dependency management, alternative approaches exist, especially in specific contexts. These solutions might not offer the same breadth of features or community support as Composer, but they can be valuable in certain scenarios.

1. Using a Package Manager Like APT

While not directly designed for PHP dependency management, Debian’s APT (Advanced Package Tool) can be used to install and manage PHP extensions and some pre-packaged PHP libraries. This approach is best suited for system-level dependencies or libraries that are readily available as Debian packages.

Explanation:

APT manages software packages at the operating system level. You can use it to install PHP extensions like php-gd or php-xml. This is beneficial for dependencies that are tightly integrated with the system or when you prefer a centralized package management approach. However, it’s less flexible for project-specific dependencies and version control compared to Composer. It also requires the packages to be available in the Debian repositories. This PHP Composer Installation Guide on Debian 12 Bookworm provides an alternative with APT.

Code Example:

sudo apt install php-gd

This command installs the php-gd extension using APT. You would then need to restart your web server (e.g., Apache or Nginx) for the changes to take effect.

This method circumvents the need for a composer.json file and the vendor directory, relying on the system’s package management to resolve and manage dependencies. It’s simpler for basic needs but lacks the granularity and project-specific isolation that Composer provides.

2. Manual Dependency Management

For very small projects or in situations where Composer is not feasible (e.g., due to server limitations), you can manually manage dependencies. This involves downloading the required library files and including them directly in your project.

Explanation:

This approach involves manually downloading the necessary PHP libraries (e.g., from GitHub or the library’s website) and placing them in a directory within your project (e.g., lib/). You then use require or include statements in your PHP code to load these libraries. This is the most basic form of dependency management and is only suitable for extremely simple projects with very few dependencies. It requires meticulous tracking of versions and manual updates, making it prone to errors and difficult to maintain for larger projects. This is an alternative of the PHP Composer Installation Guide on Debian 12 Bookworm.

Code Example:

Assume you’ve downloaded a simplified version of a string manipulation library and placed it in lib/string_utils.php:

<?php
// lib/string_utils.php
function toUpperCase($string) {
    return strtoupper($string);
}
?>

In your main PHP file:

<?php
require_once 'lib/string_utils.php';

$text = "hello world";
$upperCaseText = toUpperCase($text);
echo $upperCaseText; // Output: HELLO WORLD
?>

This example demonstrates how to manually include a dependency. You are responsible for downloading, updating, and managing the library’s files. This approach is highly discouraged for anything beyond the simplest projects due to its lack of scalability and maintainability.

In conclusion, while Composer remains the recommended and most powerful tool for PHP dependency management, understanding alternative approaches like APT and manual management can be useful in specific, limited scenarios. For the vast majority of PHP projects, Composer offers the best balance of features, maintainability, and community support. This PHP Composer Installation Guide on Debian 12 Bookworm highlights the most common and efficient way to manage PHP dependencies.