How to install Composer on CentOS 7

How to install Composer on CentOS 7

Install Composer on CentOS

Composer is one of the best dependency management tools in PHP that can seamlessly install and update project dependencies. When installing a package, also check for other packages on which the current package depends, and install any dependencies. In this tutorial, you will learn how to run Composer on CentOS 7.

Precondition

Before installing Composer on CentOS 7, you need a non-root user account with sudo privileges on the server / desktop.

1. Install Composer

Before installing the Composer Update System Software Package by typing

sudo yum -y update

You need to install some composer dependencies. Enter as follows.

sudo yum install php-cli php-zip wget unzip

Enter the following command to download the Composer setup:

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

Next, you need to run the following command to verify that the installer matches the SHA-384 hash for the data integrity of the latest installer on the Composer public key or signing page.

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

Check if the installation script is corrupted

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

You get the following output:

output

Installer Verified

If you do not see the above output, you may see corrupted output from the installer. In this case, download the composer again and check the hash value until you get the installer verification output.

Run the following command to install Composer globally internally /usr/local/bin directory.

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

You should get the following output

output

Output
All settings correct for using Composer
Downloading...

Composer (version 1.6.3) successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer

Run the following command to confirm the installation

composer

You will see the following output

output

   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ / __ `__ / __ / __ / ___/ _ / ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
____/____/_/ /_/ /_/ .___/____/____/___/_/
                    /_/
Composer 1.6.3 2018-01-31 16:28:17

Usage:
  command [options] [arguments]

Options:
  -h, --help                     Display this help message
  -q, --quiet                    Do not output any message
  -V, --version                  Display this application version
      --ansi                     Force ANSI output
      --no-ansi                  Disable ANSI output
  -n, --no-interaction           Do not ask any interactive question
      --profile                  Display timing and memory usage information
      --no-plugins               Whether to disable plugins.
  -d, --working-dir=WORKING-DIR  If specified, use the given directory as working directory.
  -v|vv|vvv, --verbose           Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

If you see the above output, composer has been successfully installed.

Using Composer in PHP projects

You have now globally installed Composer on your system. To use composer, you need the project root directory in that directory where you want to install the dependency packages with Composer.

Create directory NewProject As the project root directory.

sudo mkdir NewProject
cd NewProject

Then install the latest version guzzlehttp/guzzle Package using the following command:

composer require guzzlehttp/guzzle

output

Using version ^6.3 for guzzlehttp/guzzle
./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 guzzlehttp/promises (v1.3.1): Loading from cache
  - Installing psr/http-message (1.0.1): Loading from cache
  - Installing guzzlehttp/psr7 (1.4.2): Loading from cache
  - Installing guzzlehttp/guzzle (6.3.3): Loading from cache
guzzlehttp/guzzle suggests installing psr/log (Required for using the Log middleware)
Writing lock file
Generating autoload files

When you install the package, you can see that Composer has created three files, composer.json, a composer.lock file containing the version and package name, and a vendor directory. Confirm by entering the following command:

ls -l

output

Output
total 12
-rw-rw-r-- 1 linux4one admin   59 Nov 11 20:13 composer.json
-rw-rw-r-- 1 linux4one admin 2934 Nov 11 20:13 composer.lock
drwxrwxr-x 4 linux4one admin 4096 Nov 11 20:13 vendor

You have now installed the package guzzle, created a test.php file and copied the following code into that file. Checks the URL status code if it loads successfully, otherwise returns 200.

test.php

request('GET', 'https://api.github.com/repos/guzzle/guzzle'); 
echo "statuscode : ".$res->getStatusCode();

Enter and run the above script

php test.php

The output is

statuscode : 200

To update a package, you can use the following command:

composer update

Conclusion

You have learned how to install CompOSer on CentOS 7. If you have any questions about this, don’t forget to comment below.

Sidebar