1080*80 ad

How to Install PHP Composer on Rocky Linux 8

How to Install PHP Composer on Rocky Linux 8: A Step-by-Step Guide

In modern PHP development, managing project dependencies is crucial for building robust and maintainable applications. Composer stands as the industry-standard dependency manager for PHP, simplifying the process of including libraries and managing packages. If you’re working on a Rocky Linux 8 system, installing Composer is a foundational step for any serious PHP project.

This guide provides a clear, step-by-step walkthrough to get Composer installed and running globally on your Rocky Linux 8 server, empowering you to streamline your development workflow.

Step 1: Update Your System and Install Prerequisites

Before installing Composer, it’s essential to ensure your system is up-to-date and has PHP installed. Composer is a command-line tool, so you will need the PHP Command Line Interface (php-cli).

First, update your system’s package index:

sudo dnf update -y

Next, install PHP-CLI and other necessary extensions. Composer requires a few specific PHP extensions to function correctly. You can install them all with a single command.

sudo dnf install php-cli php-json php-mbstring wget unzip
  • php-cli: Allows you to execute PHP scripts from the terminal.
  • php-json: Required by Composer for handling JSON files, like composer.json.
  • php-mbstring: Provides functions for handling multi-byte strings.
  • wget: A utility to download files from the web.

Once the installation is complete, you can verify that PHP is installed correctly by checking its version:

php -v

Step 2: Download the Composer Installer

With PHP ready, the next step is to download the official Composer installer script. It’s best practice to navigate to a temporary directory, like /tmp, to keep your home directory clean.

cd /tmp
wget https://getcomposer.org/installer

This command uses wget to download the installer script from the official Composer repository and saves it in your current directory (/tmp).

Step 3: Verify the Installer’s Integrity

This is a critical security measure that should never be skipped. Before executing any script from the internet, you must verify that it has not been tampered with. The Composer team provides a SHA-384 hash for the latest installer on their website.

First, you need to get the latest signature hash from the Composer public keys/signatures page. You can use wget to grab it and store it in a variable.

HASH=$(wget -q -O - https://composer.github.io/installer.sig)

Next, generate the hash of the installer file you downloaded and compare it with the official hash. The following script automates this check for you:

php -r "if (hash_file('SHA384', 'installer') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('installer'); } echo PHP_EOL;"

If the output is Installer verified, you can safely proceed. If it says Installer corrupt, the file was compromised or the download was incomplete. Do not proceed. Delete the file and try the download process again.

Step 4: Install Composer Globally

Now that the installer is verified, you can run it to download the composer.phar file, which is the actual Composer program.

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

Let’s break down this command:

  • sudo php installer: Executes the installer script with administrative privileges.
  • --install-dir=/usr/local/bin: This is the most important part for a global installation. It tells the installer to place the Composer executable in the /usr/local/bin directory. This directory is in the system’s PATH, which means you can run the composer command from any location on your server.
  • --filename=composer: This renames the file from composer.phar to just composer, making it easier and more conventional to execute.

By installing Composer globally, you avoid having to specify the full path to composer.phar every time you want to use it. This makes Composer accessible system-wide for any user.

After the installation is complete, the installer script is no longer needed and can be removed.

rm installer

Step 5: Verify the Installation

To confirm that Composer has been installed correctly and is accessible from anywhere on your system, simply run the following command:

composer --version

If the installation was successful, you will see output displaying the installed Composer version, similar to this:

Composer version 2.x.x 2023-XX-XX XX:XX:XX

This confirmation means you have successfully installed Composer on your Rocky Linux 8 system. You are now ready to manage dependencies for your PHP projects.

Getting Started with Composer

With Composer installed, you can now use it to manage your project’s libraries. Here’s a quick example:

  1. Create a new directory for your project and navigate into it:
    bash
    mkdir my-php-project && cd my-php-project
  2. Use Composer to add a package, such as the popular logging library Monolog:
    bash
    composer require monolog/monolog

Composer will create two new files, composer.json and composer.lock, and a vendor directory containing the downloaded package. Your project is now set up to use Monolog, and managing future dependencies is as simple as running a single command.

Source: https://kifarunix.com/install-php-composer-on-rocky-linux-8/

900*80 ad

      1080*80 ad