Install Composer on Debian

Composer is one the best dependency management tool for PHP which can install and update project dependency seamlessly. When installing a package it also checks on which another package current package depends on and then it installs all the dependencies. In this tutorial, you are going o learn how to Composer on Debian 9 system.

Prerequisites

Before you start to install Composer on Debian 9 system. You must have the non-root user account on your server/desktop with sudo privileges.

1. Install Composer

First Update apt package manager index by typing.

sudo apt update

Next, you should install some dependencies for Composer, you can do so by typing

sudo apt install curl php-cli php-mbstring git unzip

Enter following commands to download Composer Setup.

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

Now, you should verify that the installer matches the SHA-384 hash for the data integrity of latest installer found on the Composer Public Keys or Signatures page by running the following command.

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

Check if installation script is corrupted by

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 should get following output:

Output
Installer Verified

If you dont see above output you may get Installer Corrupt output. If this case download composer again and check hash value until you get Installer Verified output.

Now run following command to install Composer globally inside /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 following command to confirm the installation

composer

You will see 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 above output, you have installed composer successfully.

Use Composer In PHP Project

Now Composer is globally installed on your system. To use composer you should have a root directory for your project inside that directory you will install the dependency package with Composer.

Create directory NewProject as a root directory of your project.

sudo mkdir NewProject
cd NewProject

Now install latest version of guzzlehttp/guzzle package by using following command.

composer require guzzlehttp/guzzle

After installing package you will see that Composer created three files composer.json file,composer.lock file which contains package names with version, and vendor directory.
Enter the following command to check.

ls -l
Output
Output

-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

Now, you have installed package guzzle, create a file test.php and copy following code inside that file. It will check statuscode for a URL if it loads successfully it will give 200 otherwise different
number.

test.php
<?php

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

use GuzzleHttp\Client;

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

Run above script by typing

php test.php

Output should be

statuscode : 200

If you want to update the package then you can use the following command.

composer update

Conclusion

You have successfully learned how to install Composer on Debian 9 system. If you have any queries regarding this don’t forget to comment below.

Install Composer on CentOS

Composer is one the best dependency management tool for PHP which can install and update project dependency seamlessly. When installing a package it also checks on which another package current package depends on and then it installs all the dependencies. In this tutorial, you are going to learn how to Composer on CentOS 7.

Prerequisites

Before you start to install Composer on CentOS 7. You must have the non-root user account on your server/desktop with sudo privileges.

1. Install Composer

Before you install Composer update system software packages by typing

sudo yum -y update

Now you should install some dependencies for Composer, you can do so by typing

sudo yum install php-cli php-zip wget unzip

Enter following commands to download Composer Setup.

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

Now, you should verify that the installer matches the SHA-384 hash for the data integrity of latest installer found on the Composer Public Keys or Signatures page by running the following command.

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

Check if installation script is corrupted by

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 should get following output:

Output
Installer Verified

If you don’t see above output you may get Installer Corrupt output. If this case download composer again and check hash value until you get Installer Verified output.

Now run following command to install Composer globally inside /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 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 above output, you have installed composer successfully.

Use Composer In PHP Project

Now Composer is globally installed on your system. To use composer you should have a root directory for your project inside that directory you will install the dependency package with Composer.

Create directory NewProject as a root directory of your project.

sudo mkdir NewProject
cd NewProject

Now install latest version of guzzlehttp/guzzle package by using 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

After installing package you will see that Composer created three files composer.json file,composer.lock file which contains package names with version, and vendor directory.
Enter the following command to check.

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

Now, you have installed package guzzle, create a file test.php and copy following code inside that file. It will check status code of a URL if it loads successfully it will give 200 otherwise different
number.

test.php
<?php

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

use GuzzleHttp\Client;

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

Run above script by typing

php test.php

Output should be

statuscode : 200

If you want to update the package then you can use the following command.

composer update

Conclusion

You have successfully learned how to install Composer on CentOS 7. If you have any queries regarding this don’t forget to comment below.

Install Composer on Ubuntu

Composer is one the best dependency management tool for PHP which can install and update project dependency seamlessly. When installing a package it also checks on which another package current package depends on and then it installs all the dependencies. In this tutorial, you are going to learn how to Composer on Ubuntu 18.04.

Prerequisites

Before you start to install Composer on Ubuntu 18.04. You must have the non-root user account on your server/desktop with sudo privileges.

1. Install Composer

Before you install Composer you should update apt package manager index by typing.

sudo apt update

Now you should install some dependencies for Composer, you can do so by typing

sudo apt install curl php-cli php-mbstring git unzip

Enter following commands to download Composer Setup.

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

Now, you should verify that the installer matches the SHA-384 hash for the data integrity of latest installer found on the Composer Public Keys or Signatures page by running the following command.

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

Check if installation script is corrupted by

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 should get following output:

Output
Installer Verified

If you dont see above output you may get Installer Corrupt output. If this case download composer again and check hash value until you get Installer Verified output.

Now run following command to install Composer globally inside /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 following command to confirm the installation

composer

You will see 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 above output, you have installed composer successfully.

Use Composer In PHP Project

Now Composer is globally installed on your system. To use composer you should have a root directory for your project inside that directory you will install the dependency package with Composer.

Create directory NewProject as a root directory of your project.

sudo mkdir NewProject
cd NewProject

Now install latest version of guzzlehttp/guzzle package by using following command.

composer require guzzlehttp/guzzle

After installing package you will see that Composer created three files composer.json file,composer.lock file which contains package names with version, and vendor directory.
Enter the following command to check.

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

Now, you have installed package guzzle, create a file test.php and copy following code inside that file. It will check statuscode for a URL if it loads successfully it will give 200 otherwise different
number.

test.php
<?php

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

use GuzzleHttp\Client;

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

Run above script by typing

php test.php

Output should be

statuscode : 200

If you want to update package then you can use following command.

composer update

Conclusion

You have successfully learned how to install Composer on Ubuntu 18.04. If you have any queries regarding this dont forget to comment bellow.