Composer is an open-source, MIT-licensed package manager for PHP that is application-oriented. It sets up a project created in the PHP programming language by loading the various dependencies needed. 

The main benefit of using Composer is that you can automatically update these dependencies and are not required to include these files with your PHP code because they are downloaded once more on the system used with the command. 

This tutorial demonstrates how to install Composer on Ubuntu 22.04 and get going with it.

Install PHP and other dependencies.

Composer needs php-cli to run PHP scripts on the command line and unzip to extract zipped archives, in addition to dependencies like git and curl that should already be present on your Ubuntu 22.04 system. 

You have to install these prerequisites. First, execute the following code to update the package manager cache:

sudo apt update

Run the subsequent command to install the necessary packages after that:

sudo apt install php-cli unzip

When asked to confirm installation, type Y and then press ENTER.

You can start to install Composer on Ubuntu after installing the requirements.

Download and install Composer.

Composer offers a PHP installer script. It will be downloaded, checked to ensure it isn't damaged, and then used to install Composer.

Ensure that you are in your home directory then use curl to retrieve the installer:

cd ~

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

After that, we have to ensure that the downloaded installer matches the SHA-384 hash for the most recent installer on the Composer Public Keys/ Signatures page. 

You can use the following command to programmatically get the most recent hash from the Composer page and save it in a shell variable to speed up the verification step:

HASH=`curl -sS https://composer.github.io/installer.sig`

You can perform the following tests to confirm the result:

echo $HASH

Now run the PHP code listed below, which is available on the Composer download page, to make sure the installation script can be run without risk:

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

The following output will appear:

Installer verified

You must download the installation script again and double-check that you are using the right hash if the output states that the installer is faulty. 

Repeat the verification procedure after that. You can install Composer on Ubuntu once you have a validated installer.

Simple Composer use

Composer tracks dependencies on a per-project basis. Other users will find it simpler to establish the same environment. 

The Composer uses the composer.json file to keep track of the program requirements and permitted versions.

It also employs a composer- lock file to preserve consistency even if the directory is copied. When the need command is used, these files are generated automatically.

Launch a terminal and type the following commands:

mkdir c_sample && cd c_sample

After that, pick a package to load. Downloadable PHP packages can be found on the website packagist.org. 

We employ the monolog/monolog package in this illustration. Either proceed with the following steps or look up a different package on the website.

Type this into the terminal window:

composer require monolog/monolog

The composer.json and composer.lock files are created by the system along with the program download.

List the directory's contents after the procedure is finished.

ls –l

A vendor directory and the files composer.json and composer.lock are displayed in the list.

To see the composer.json file's contents, enter the following:

cat composer.json

The monolog software is added to the system. The software's minimum version is indicated by the carat symbol beside the version number.

? Boost your online presence with our high-performance Linux VPS hosting! Experience seamless website loading and rock-solid reliability for your business. ???

Configuring Autoloading

Since PHP doesn't load classes automatically by default, Composer offers an autoload script that you may add to your project to enable autoloading. 

When you add the first dependency, Composer automatically creates this file. The vendor/autoload.php file must only be in your PHP scripts before any class creation.

Open your text editor and create a new file:

sudo nano composer_sample.php

Then, insert the following:

<?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('~/c_sample/text.log', Logger::WARNING));

// add records to the log

$log->warning('Foo');

$log->error('Bar');

Ctrl+O will write the file, while Ctrl+X will close it.

Execute the autoload monolog command:

php composer_sample.php

Update project Dependencies

Run the update command each time you would like to update your project dependencies to more recent versions:

composer update

This method will check the libraries you need for your project for updated versions. The composer will replace the previously installed version if a newer version is discovered and it complies with the version constraint specified in the composer.json file. 

To reflect these modifications, the composer.lock file will be changed. Additionally, you can update one or more individual libraries by naming them as follows:

composer update vendor/package vendor2/package2

Conclusion

Composer is a potent tool that may make handling dependencies in PHP projects much easier. It offers a dependable method of locating, downloading, and maintaining PHP packages that a project needs. 

We learned how to install Composer on Ubuntu, add new dependencies to a project, and update these dependencies as newer versions become available in this article. 

People also read: