In this tutorial, we will see how to install Composer on your VPS under Linux (Debian or Ubuntu). Composer is THE dependency manager of the PHP ecosystem, allowing to install and use external libraries very easily.

To browse the list of all libraries created by the community, go to the Packagist website.

Dependencies

  • PHP-CLI is a mandatory dependency, Composer being a PHP script, you must have it on your system: apt install php7.3-cli.
  • Git is mandatory for Composer to work properly, apt install git on Linux.
  • curl is required for Composer to work properly, apt install curl on Linux.
  • unzip** is required for Composer to work properly, apt install unzip on Linux.

Installing Composer

To install Composer on your server, follow the procedure below:

  1. Login to your server, as administrator.
  2. Enter the command: wget -O - https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer

Composer is now installed on your system!

Checking the installation of Composer

You can verify that Composer has been installed by typing the command :

composer -n -V

You should see something like this:

Composer version 2.0.7 2020-11-13 17:31:06

Using Composer

Let's move on to using Composer, we will create our first project and add an external library.

Start by creating a new directory for the project :

mkdir ~/composer-test
cd ~/composer-test

Now let's use composer init to initialize Composer in our new project. You can do the same to add Composer to an existing project.

The composer init command needs to receive at least the name of the project. This one must be normalized, on the form: author/project (no spaces and no capital letters!). In our case, we will create a new project FirstComposer :

composer init -n --name seb/first-composer

The command will create a composer.json file in the project. This file will contain the definition of the dependencies: which library you need and in which version, for the proper functioning of our code. If you look at the content of the file with cat composer.json, you should see something like :

{
    "name": "seb/first-composer",
    "require": {}
}

The require section will contain the list of libraries we need. These libraries will be installed automatically by Composer, in the vendor/ directory. You should never edit the contents of this directory, it is generated by Composer, any changes will be overwritten!

We will now create our PHP script, which will use Composer. To add it in our code, the documentation says that we have to add this piece of code at the top of our script:

<?php

require_once __DIR__ . '/vendor/autoloader.php'; // We ask PHP to load all the libraries managed by Composer, to be able to use them in our project. Nothing more to do!

Create the index.php file and this piece of code at the top.

Now that Composer is added to our code, let's use it! A wide variety of libraries exist. For our example, we will add the library that allows us to easily communicate with the API of mTxServ :).

This library is named mtxserv/mtxserv-php. To install a library, you have to use the composer require command. In our case, this gives :

composer require mtxserv/mtxserv-php

You should see, if there are no errors:

Using version ^1.0 for mtxserv/mtxserv-php
./composer.json has been updated
Running composer update mtxserv/mtxserv-php
Loading composer repositories with package information
Updating dependencies
Lock file operations: 3 installs, 0 updates, 0 removals
  - Locking guzzle/guzzle (v3.9.3)
  - Locking mtxserv/mtxserv-php (v1.0.1)
  - Locking symfony/event-dispatcher (v2.8.52)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 3 installs, 0 updates, 0 removals
  - Downloading symfony/event-dispatcher (v2.8.52)
  - Downloading guzzle/guzzle (v3.9.3)
  - Downloading mtxserv/mtxserv-php (v1.0.1)
  - Installing symfony/event-dispatcher (v2.8.52): Extracting archive
  - Installing guzzle/guzzle (v3.9.3): Extracting archive
  - Installing mtxserv/mtxserv-php (v1.0.1): Extracting archive
3 package suggestions were added by new dependencies, use `composer suggest` to see details.
Package guzzle/guzzle is abandoned, you should avoid using it. Use guzzlehttp/guzzle instead.
Generating autoload files

And that's it, the library is in our project! It has been uploaded by Composer to the vendor/ directory, at the root of the project. A composer.lock file has also been created. This file is used by Composer to know the exact version you want for the libraries, you should never modify it by hand.

To finish our tutorial, we just have to test that the library you have just added is working well in the project. To start, we consult the documentation to know how to use it: readme :

<?php

$client = \Mtxserv\Client::factory(array(
    'client_id'     => 'YOUR_CLIENT_ID',
    'client_secret' => 'YOUR_CLIENT_SECRET',
    'api_key'       => 'YOUR_API_KEY'
));

If we add this code, it should work: the Client class should be found by PHP. Edit the index.php file:

<?php

require_once __DIR__ . '/vendor/autoloader.php'; 

$client = \Mtxserv\Client::factory(array(
    'client_id'     => 'YOUR_CLIENT_ID',
    'client_secret' => 'YOUR_CLIENT_SECRET',
    'api_key'       => 'YOUR_API_KEY'
));

Now we just have to test, you should have no errors like Uncaught Error: Class Mtxserv\Client not found :

php index.php

Are you beginning to understand the interest of Composer? It will manage your libraries, their versions, their incorporation in our project, automatically!