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.
Make sure you meet the following prerequisites before proceeding with the steps below:
Perform the following steps on CentOS 8 to install Composer.
sudo dnf install php-cli php-json php-zip wget unzip
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.
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.
/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
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.
Perform the following steps to quickly install Composer on your CentOS 8 system:
sudo dnf install php-cli php-json php-zip curl unzip
curl -sS https://getcomposer.org/installer |php
/usr/local/bin
directory:sudo mv composer.phar /usr/local/bin/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
vendor
is the directory where the project's dependent software packages are stored.
The composer.lock
file contains a list of all installed dependent packages and their version numbers.
composer.json
is a file used to describe your PHP project. It contains PHP dependent packages and other meta-information.
You can search Composer source repository to get more PHP packages.
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.
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