How to Install and Use PHP Composer on CentOS 8 - MS TV Life.COM

How to Install and Use PHP Composer on CentOS 8

How to Install and Use PHP Composer on CentOS 8

Composer is a dependency manager for PHP (as npm is to Node.js or pip is to Python).

Arranger will pull in all the required PHP bundles your undertaking relies upon and oversees them for you. It is utilized in all cutting edge PHP structures and stages, for example, Laravel, Symfony, Drupal, and Magento 2.

Find out how to Setup FTP Server with VSFTPD on CentOS 8

This instructional exercise experiences the means of introducing Composer on CentOS 8. On the off chance that you are in a rush and you would prefer not to check the document honesty, look down to the Installing Composer – Quick Way area.

Prerequisites #

Guarantee that you have met the accompanying necessities before proceeding:

Installing Composer on CentOS #

Play out the accompanying strides to introduce Composer on CentOS 8.

  1. Install the PHP CLI (command-line interface) package and all other dependencies with:sudo dnf install php-cli php-json php-zip wget unzip
  2. Once PHP CLI is installed, download the Composer installer script:php -r "copy('', 'composer-setup.php');"The command above downloads a file named composer-setup.php in the current working directory.
  3. Verify the data integrity by comparing the file’s SHA-384 hash with the hash found on the Composer Public Keys / Signatures page.The following wget command downloads the signature of the latest Composer installer from the Composer’s Github page, and store it in a variable named HASH:HASH="$(wget -q -O -"To verify that the installation script is not corrupted run the following command:php -r "if (hash_file('SHA384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"If the hashes match, the following message will be shown:Installer verifiedOtherwise, if the hashes don’t match, you will see Installer corrupt. Once the integrity is verified, continue with the next step.
  4. Run the following command to install Composer in the /usr/local/bin directory:sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composerThe command above installs composer as a system-wide command and available for all users. The output will look something like thisAll settings correct for using Composer Downloading... Composer (version 1.10.1) successfully installed to: /usr/local/bin/composer Use it: php /usr/local/bin/composerThe command above installs composer as a system-wide command and available for all users.
  5. Verify the installation by printing the Composer’s version:composer -VComposer version 1.10.1 2020-03-13 20:34:27

Now, you have effectively introduced Composer on your CentOS framework, and you can begin utilizing it

Installing Composer [Quick Way] #

Perform the following steps to quickly install Composer on your CentOS 8 system:

  1. Install PHP CLI and Zip:sudo dnf install php-cli php-json php-zip curl unzip
  2. Download Composer with curl:curl -sS |php
  3. Move the Composer file to /usr/local/bin directory:sudo mv composer.phar /usr/local/bin/composer

How to use Wget Command in Linux with Examples

Getting Started with Composer #

Since Composer is introduced on your CentOS framework, we will tell you the best way to utilize Composer in a PHP venture

Start by creating the project root directory and navigate to it:

mkdir ~/my-first-composer-projectcd ~/my-first-composer-project

In this example, we’ll use a PHP package called carbon to create a sample application that prints the current time.

Run the following command to initialize a new Composer project and install the carbon package:

composer require nesbot/carbon
sing version ^2.32 for nesbot/carbon
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 4 installs, 0 updates, 0  removals
  - Installing symfony/translation-contracts (v2.0.1): Downloading (connecting..Downloading (100%)         
  - Installing symfony/polyfill-mbstring (v1.15.0): Downloading (100%)         
  - Installing symfony/translation (v5.0.6): Downloading (100%)         
  - Installing nesbot/carbon (2.32.1): 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
3 packages you are using are looking for funding.
Use the `composer fund` command to find out more!

As you can see from the output above, Composer creates a file named composer.json and downloads carbon and all its dependencies.

If you list your project’s directory, you will see that it contains two files composer.json and composer.lock, and a vendor directory.

ls -l
-rw-rw-r--. 1 vagrant vagrant    60 Mar 27 18:05 composer.json
-rw-rw-r--. 1 vagrant vagrant 11135 Mar 27 18:06 composer.lock
drwxrwxr-x. 6 vagrant vagrant    82 Mar 27 18:06 vendor
  • vendor is the directory where the project dependencies are stored.
  • The composer.lock file contains a list of all installed packages, including their versions.
  • composer.json is a file that describes your PHP project, including the PHP dependencies and other metadata.

You can search the Composer repository Packagist for PHP packages.

Composer has autoload capabilities, that allow you to use PHP classes without the need of the require or include statements.

Create a file named testing.php and add the following code:


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

use CarbonCarbon;

printf("Now: %s", Carbon::now());

Let’s analyze the code line by line.

In the first line after the opening php tag we include the vendor/autoload.php file that autoloads all of the libraries.

Next, we are aliasing CarbonCarbon as Carbon. The last line prints the current time using the Carbon now method.

Run the script by typing:

php testing.php

The output should look something like this:

Now: 2020-03-27 22:12:26

Later on, if you want to update your PHP packages you can simply run:

composer update

The command above will check for newer versions of the installed packages, and if a newer version is found and the version constraint match with the one specified in the composer.json, Composer will update the package.

How to Find Files Using Linux Command Line

Conclusion #

You have figured out how to introduce Composer on your CentOS 8 machine. We have likewise told you the best way to utilize Composer to make an essential PHP venture.

To find more information about Composer, visit the official Composer documentation page.

If you have any questions, please leave a comment below.

Copy link
Powered by Social Snap