Composer, install this PHP dependency manager on Ubuntu 18.04

about composer

In the next article we are going to take a look at Composer. This is a dependency manager for PHP. It will install all the necessary PHP packages our projects depend on and manage them for us.

This program has been a application-level package manager for the PHP programming language which provides a standard format for managing PHP software dependencies and required libraries. Though this tutorial is written for Ubuntu 18.04, the same steps can be used for Ubuntu 16.04.

Before following the steps that are going to be shown here, be sure to have PHP installed on your Ubuntu 18.04 system. If you don't have it installed, open a terminal (Ctrl + Alt + T) and type:

Install Php for Composer

sudo apt install php libapache2-mod-php php-mysql

Install Composer

Before downloading and installing this dependency manager, first we have to install the PHP-CLI package, which is available in the Ubuntu repositories. In the terminal (Ctrl + Alt + T) we write:

sudo apt install php-cli

Now that we have php-cli installed on our machine, we can download the necessary installer with:

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

The above command will download the composer-setup.php file to the current working directory.

Download installer for Composer

Next, we need to verify the integrity of the script data by comparing the script of the SHA-384 hash with the last hash of the installer. This can be found on the Composer Public Keys / Signatures page.

We will use the following wget command to download the expected signature of the latest installer from your Github page and store it in a variable called HASH:

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

Now run the following command to verify that the install script is not 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;"

If the hash values ​​match, you will see the following result:

verified installation of Composer

If the hashes don't match, you'll see something like “Installer corrupted”. Once the installer has been verified, we can proceed to the next step.

The following command will install Composer in the / usr / local / bin directory:

Composer user / local / bin installation

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

It will be installed as a system wide command and will be available to all users.

The last step is verify installation:

verify Composer installation

composer

The above command will print the available version, commands and arguments.

If you want to have one Composer installation per project, use the following command:

php composer-setup.php

This will create a file called composer.phar in your current working directory. You can use it by running:

./composer.phar comando

Getting started with Composer

Once the installation is finished on your Ubuntu system, we are going to a basic look at its use in a PHP project.

The first step is to create a directory that will be the root directory of the project and start a composer.json file. This file describes our PHP project, including PHP dependencies and other metadata.

mkdir ~/mi-primer-proyecto-con-composer

cd ~/mi-primer-proyecto-con-composer

The next step is to start a new composer.json using the "composer require package-name", Specifying the package we want to download.

In this example, we will create a sample application that will print the current time and date using a package called carbon.

Run the following command to start a new composer.json and install the carbon package:

Download carbon Composer

composer require nesbot/carbon

Directory of our project

If we take a look at the directory list for our project, we will see that it contains two files composer.json and composer.lock, and a directory called vendor.

carbon composer project directory listing

  1. El vendor directory is the directory where the project dependencies are stored.
  2. The file composer.lock contains a list of all installed packages. Including the exact versions of the packages.
  3. composer.json describes the PHP project and all PHP dependencies.

Searchable in Composer repository Packagist PHP packages to use.

Test file

Composer provides self-loading capabilities that allow us to use PHP classes without the need to use require o includes In the files.

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

Contents of the test file with composer

<?php

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

use Carbon\Carbon;

printf("Fecha y hora del sistema: %s", Carbon::now());

Saving the file, we will execute the script by typing:

php prueba.php

The result should look something like this:

result test file with composer

Update your PHP packages

If necessary, if you want to update your PHP packages, you can run:

composer update

The command will search for newer versions of installed packages and if a newer version is found it will update the package, if possible.

For get more information visit the official documentation page Composer.


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Javier Gonzalez said

    COMPOSE?
    hahahahahahahaha
    The fucking master!!!!!