31 C
Texas
angeloma
Senior Writer and partner

Using Composer on Linux

Thanks to the facilities it gives us for managing PHP dependencies, Composer has become a fundamental application for developers. It is hard to believe that such a small application can contribute as much to a complex process as application development. However, you have to learn the basics of it. So, in this post, I will help you to use Composer on Linux.

First of all, we will quickly tell you what Composer is.

Composer is a tool for dependency management in PHP. So, it allows you to declare the libraries your project depends on and it will manage (install/update) them for you. This way we will not have to worry about installing those libraries.

So as you can see, it becomes quite necessary in any kind of project with PHP. Especially if you need any additional library.

- Advertisement -

This guide has been made using Ubuntu 20.04. However, it should work for any Linux distribution. So, let us start to work.

Install Composer on Ubuntu 20.04

To install Composer on Ubuntu 20.04 you need to follow our post:

How to install PHP Composer on Ubuntu 20.04

With this Composer will be installed globally and can be used from any directory on the system. This way we can work in a much more comfortable and efficient way.

Using Composer on Linux

Installing Composer is quite simple. Everything is summarized in a few commands and this makes it compatible with almost all Linux distributions.

On the other hand, despite this, another question arises: how do you update the program? You can do this by running the following command:

:~$ composer self-update

Just like that, Composer will stay up to date and we will be able to take advantage of an agile development cycle.

All Composer needs is a file that we have to create called composer.json in the project folder. As you can see it’s a JSON file where we will specify all the dependencies that our project requires.

So, create it with any text editor and add something like this

{
     "require": {
         "monolog/monolog": "2.0.*"
     }
}

This is the most basic information that the file composer.json So can have, so you can add as many libraries you want or require your project. I’ll add another one.

{
     "require": {
         "monolog/monolog": "2.0." ,
         "phpmailer/phpmailer": "5.2."
     }
}

As you can see, the key require is where the required libraries are set up. In this example, it is specified that composer will download the latest version of the 2.0.x branch of monolog. And the latest version of the 5.2.x branch of phpmailer. So you can specify which specific versions of the library you need.

If you do not feel comfortable editing the file manually, Composer can do it for you using a more interactive method.

To do this, execute the following command and fill in the necessary fields.

:~$ composer init
composer init
Welcome to the Composer config generator
This command will guide you through creating your composer.json config.
Package name (/) [angelo/composer]: osradar/angelo
Description []: test
Author [, n to skip]: Angelo Osradar [email protected]
Minimum Stability []: alpha
Package Type (e.g. library, project, metapackage, composer-plugin) []: project
License []: GPL
Define your dependencies.
Would you like to define your dependencies (require) interactively [yes]? yes
Search for a package: monolog
1.- Using Composer on Ubuntu 20.04
1.- Using Composer on Ubuntu 20.04

Honestly, I find it faster and more productive to do it manually. But I’ll give you that alternative.

Once you have the composer.json file done, you have to install the dependencies.

Using Composer on Linux to install the dependencies

To install the dependencies we have specified, just run the following command:

:~$ composer install

And that’s it, then the program will start all the downloads it has to do. And in turn, since there is no file called composer.lock, it will create it

The composer.lock file is a file that allows us to reinstall over and over again all the necessary libraries. This is to avoid that, if some developer joins the project, he cannot know which are the exact versions of the libraries that the project needs.

I explain it in another way, in the file composer.json we established that a 2.0.x monolog is required and at the moment of making the project the 2.0.1 is downloaded. In some time another developer can join the project and in his file composer.json specifies 2.0.x monolog but by that time there could be a 2.0.7 version that could bring some incompatibility. If both files are held by the new developer composer will know exactly which version to download.

If you want to update the versions of the dependencies just execute this command:

:~$ composer update

It’s as simple as that.

Using the downloaded dependencies

Composer creates a folder called “vendor” where all these dependencies are created. Also, composer provides you with an autoload system of the classes that make up those libraries you are using.

Therefore, the “autoload.php” file is the only script you will have to include from PHP.

<?php
require "vendor/autoload.php";
?>

And that’s how easy you can use Composer on Linux.

Conclusion

Thanks to this post, you will be able to have the basic notions of the use of Composer in Linux. It’s a pretty simple tool to use but it solves a big problem in PHP web development as it is the dependency management.

Please share this post and join our Telegram channel.

- Advertisement -
Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

LEAVE A REPLY

Please enter your comment!
Please enter your name here



Latest article