How to install and use Composer on CentOS 8

Composer is a PHP dependency manager (similar to Node.js's npm, or Python's pip).

Composer will pull all the PHP packages your project depends on and manage them for you. It is used by all modern PHP frameworks and platforms, such as Laravel, Symfony, Drupal, and Magento 2.

This guide will introduce the steps to install Composer on CentOS 8. If you are in a hurry and do not want to verify the integrity of the file, scroll down to the "Quick Install Composer" section.

1. Prerequisites##

Make sure you meet the following prerequisites before proceeding with the steps below:

2. Install Composer on CentOS

Perform the following steps on CentOS 8 to install Composer.

  1. Install the PHP CLI (command line interface) package and all other dependent packages:
sudo dnf install php-cli php-json php-zip wget unzip
  1. Once the PHP CLI is installed, download the Composer installation script:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

The above command will download a file named composer-setup.php in the current working directory.

  1. To verify the integrity of the file, compare the hash value of the file's SHA-384 with the hash value of the omposer Public Keys / Signatures page.

The following wget command will download the latest Composer signature from Composer's Github page and store it as a variable named HASH.

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

To verify whether the installation script is damaged, 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 hash values match, the following information will be displayed:

Installer verified

Otherwise, if the hash value does not match, then you will see Installer corrupt. Once the integrity is verified, please proceed to the next steps.

  1. Run the following command to install Composer to the /usr/local/bin directory:
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer

The above command installs composer as a system-wide command that all users can use. The output will look like this:

All settings correct for using Composer
Downloading...Composer(version 1.10.1) successfully installed to:/usr/local/bin/composer
Use it: php /usr/local/bin/composer
  1. Verify that the installation is successful by printing the version of Composer:
composer -V
Composer version 1.10.12020-03-1320:34:27

At this point, you have successfully installed Composer on your CentOS system, and you can start using it.

3. Install 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
  1. Install Composer using curl:
curl -sS https://getcomposer.org/installer |php
  1. Move the Composer file to the /usr/local/bin directory:
sudo mv composer.phar /usr/local/bin/composer

Fourth, start using Composer

Now that Composer is installed on your CentOS system, we will show you how to use Composer in a PHP project.

Start to create the project, and switch to the project root directory:

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

In this example, we will use a PHP package named carbon to create a simple application. This application is mainly used to print 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.32for nesbot/carbon
. /composer.json has been created
Loading composer repositories withpackage 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 above output, Composer created a file named composer.json and downloaded carbon and all its dependent packages.

If you list your project directory, you will see that it contains composer.json and composer.lock, as well as a vendor directory.

ls -l
- rw-rw-r--.1 vagrant vagrant    60 Mar 2718:05 composer.json
- rw-rw-r--.1 vagrant vagrant 11135 Mar 2718:06 composer.lock
drwxrwxr-x.6 vagrant vagrant    82 Mar 2718:06 vendor

Composer has the ability to load automatically, which allows you to use PHP classes without using require and include declarations.

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

<? php

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

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

Let's analyze the code line by line.

After the first line is an open PHP tag, we include the /vendor/autoload.php file, which allows all libraries to be loaded automatically.

Next, we associate Carbon\Carbon with Carbon. The last line uses Carbon to print the current time.

Enter the following command to run the script:

php testing.php

The output will look like this:

Now:2020-03-2722:12:26

Later, if you want to upgrade your PHP package, you can simply run:

composer update

In the above command, we will check whether all the installed packages have newer versions, and if there are newer versions, Composer will upgrade the packages.

Five, summary##

You have learned how to install Composer on your CentOS 8 machine. We have also shown you how to use Composer to create a basic PHP project.

To find more information about Composer, please visit Composer Official Documentation Page.

Recommended Posts

How to install and use Composer on CentOS 8
How to install and use Composer on Ubuntu 18.04
How to install and use Composer on Ubuntu 20.04
How to install and use Curl on CentOS 8
How to install and use Cockpit on CentOS 8/RHEL 8
How to install and configure Elasticsearch on CentOS 7
How to install and use Docker on Ubuntu 20.04
How to install and configure VNC on CentOS 8
How to install and use Curl on Ubuntu 18.04
How to install and use Wine on Ubuntu 18.04
How to install and configure Redis on CentOS 8
How to install Node.js and npm on CentOS 8
How to install and use BaasBox on Ubuntu 14.04
How to install and use PostgreSQL on Ubuntu 16.04
How to install jdk1.8.0_151 and mysql5.6.38 on centos7.2.1511
How to install and configure phpMyAdmin on CentOS 6
How to install and configure Owncloud on CentOS 8
How to install and uninstall tomcat on centos
How to install and use Docker on Ubuntu 16.04
How to install and configure Redmine on CentOS 8
How to install jdk1.8 on centOS7
How to install MySQL on CentOS 8
How to install Memcached on CentOS 8
How to install R on CentOS 8
How to install FFmpeg on CentOS 8
How to install Virtualbox on CentOS 8
How to install TensorFlow on CentOS 8
How to install TeamViewer on CentOS 8
How to install Perl 5 on CentOS
How to install Git on CentOS 8
How to install Gradle on CentOS 8
How to install Elasticsearch on CentOS 8
How to install Jenkins on CentOS 8
How to install Java on CentOS 8
How to install Go on CentOS 8
How to install Yarn on CentOS 8
How to install Nginx on CentOS 8
How to install Asterisk on CentOS 7
How to install Jenkins on CentOS 8
How to install Vagrant on CentOS 8
How to install Python 3.8 on CentOS 8
How to install Tomcat 9 on CentOS 8
How to install Webmin on CentOS 8
How to install Ruby on CentOS 8
How to install Skype on CentOS 8
How to install htop on CentOS 8
How to install Python on CentOS 8
How to install Elasticsearch on CentOS 8
How to install Postgresql on CentOS 8
How to install Wordpress on Centos
How to install htop on CentOS 8
How to install TeamViewer on CentOS 8
How to install MariaDB on CentOS 8
How to install MongoDB on CentOS 7
How to install Odoo 13 on CentOS 8
How to install Apache on CentOS 8
How to install OpenCV on CentOS 8
How to install PHP on CentOS 8
How to install MongoDB on CentOS 8
How to install and configure NFS server on CentOS 8
How to install and configure Postfix mail server on CentOS8