install php composer in ubuntu

How to Install PHP Composer in Ubuntu

PHP composer is a useful tool that tracks package dependencies for PHP projects, downloads and installs them for you. It provides a standard format for managing dependencies of PHP software & libraries. In this article, we will learn how to install PHP composer in Ubuntu.


How to Install PHP Composer in Ubuntu

Here are the steps to install PHP composer in Ubuntu. You will need to have installed PHP on your system. If you have not installed PHP yet, then follow these steps to install PHP on your system.


1. Update Local Repository

Open terminal and run the following command to update local repository.

$ sudo apt-get update


2. Download Composer Installer

Run the following command to download the installer for PHP composer.

$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"


3. Verify Integrity

Next, we need to ensure that we have indeed downloaded the right file. For that purpose, visit Composer’s public keys page. Copy the Installer signature (SHA-384).

Set the code shell variable on your system, as

$ COMPOSER=48e3236262b34d30969dca3c37281b3b4bbe3221bda826ac6a9a62d6444cdb0dcd0615698a5cbe587c3f0fe57a54d8f5

Run the script below, on your system, to check the integrity of your downloaded file.

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

It will tell you that your file is verified, or that it is corrupt. If it is corrupt, then download the file again.


4. Install PHP Composer

In order to install PHP composer, we need a few utilities such as curl, unzip, etc. Run the following command to install these utilities on your system.

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

We will install PHP Composer such that it is accessible from your whole system. For that purpose, we will install it in /usr/local/bin folder using the following command.

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

If all goes well, you will see the following output.

All settings correct for using Composer
Downloading...

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

Verify the installation with the following command. It will display the version of installed package.

$ composer --version


5. Remove PHP Composer

If you want to remove PHP composer from your system, just run the following command from terminal.

$ php -r “unlink(‘composer-setup.php’);”


6. Using Composer

Composer tracks dependencies on a per project basis. It used composer.json file to track required software and their allowed version, and composer.lock to maintain file consistency. They are automatically generated by composer and do not require user actions.

Run the following command to create a project.

$ mkdir sample
$ cd sample

Next, we will load a package monolog.

$ composer require monolog/monolog

PHP composer will automatically download and install monolog package. It will also create composer.json and composer.lock. You can always view the contents of these files by opening them in a file editor. Composer will also create a vendor directory where it stores all the project dependencies.


7. Updating packages

If you want to upload all dependencies in your project, run the following command. It will update all dependencies to the version specified in your file.

$ composer update

If you want to update specific packages, run the update command in the following format.

$ composer update vendor/package

For example, here is the command to update monolog package.

$ composer update vendor/monolog


8. Autoloading classes

Composer also allows you to autoload classes and libraries without using require or include statements for them. To enable autoloading, create a php file using a text editor.

$ sudo vi composer_sample.php

Add the following lines to it.

<?php

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

use Monolog\Logger;

use Monolog\Handler\StreamHandler;

 

// create a log channel

$log = new Logger('name');

$log->pushHandler(new StreamHandler('/~/sample/text.log', Logger::WARNING));

 

// add records to the log

$log->warning('Foo');

$log->error('Bar');

Save and close the file. Run it with the following command.

$ php composer_sample.php

In this article, we have covered how to install PHP composer, and how to use its update and autoloading capabilities.

Also read:

How to List Devices Connected to Network in Linux
How to Fix Bash Command Not Found Error
How to Disable Strict Mode in MySQL
How to Fix Stdin: Not in GZIP Format
How to Find & Kill Zombie Processes in Linux

Leave a Reply

Your email address will not be published.