Today, we’re going to discuss one of the most important tools in PHP: the dependency manager Composer. In this article, we’ll look at the ins and outs of Composer and how to install it and use it in your day-to-day PHP development.
If you’ve been coding in PHP for some time, you’ll be aware of how PHP libraries can help save work and make code reusable. In the past, it was harder to add libraries to PHP, which led to a lot of reinventing the wheel for common features. For example, a database abstraction layer (DAL) is a must-have for any PHP application, but there was no standard library or package, so many people ended up developing their own.
Certainly, there were a lot of options available at that time, but it was difficult to add dependencies, to keep track of them, and to manage your application in the long run. That’s where a dependency manager like Composer comes in. In fact, before Composer, there was a popular tool called PEAR which was used to manage PHP extensions and libraries. But it had its own limitations, which Composer was created to address.
In a nutshell, we need a tool which can be used to install libraries and manage application dependencies. Composer does a great job of this. It is an application-level package manager for PHP that has gained immense popularity and become the de facto standard for managing dependencies in PHP applications.
What Is Composer?
Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you. — Composer Introduction
It’s important to note that Composer allows you to install the necessary libraries on a per-project basis. It allows you to use different versions of the same library across different PHP projects. Of course, there’s an option to install a library globally, but it’s not recommended. If you’ve heard of npm for Node.js, or Bundler for Ruby, that’s what Composer is for PHP.
To install and use libraries that are managed by Composer, you just need to declare them in your project in a standard format, and Composer will manage the rest. For example, if you want to install the phpmailer library by using Composer, you just need to run the following command in the root of your project.
$composer require phpmailer/phpmailer
This installs the phpmailer library and its dependencies in the vendor directory of your project. More importantly, it also creates composer.json and composer.lock files that will be used to track the dependencies of your project. We’ll get back to this in detail later in this article.
In fact, Composer has become a global standard when it comes to managing third-party PHP libraries. Popular PHP frameworks like Symfony, Laravel, Drupal and many more have already adopted Composer-based workflows in their development lifecycle.
In the next section, we’ll see how to install Composer.
How to Install Composer
When it comes to installing Composer, there are two ways. You could install it either locally on a per-project basis or globally. We’ll install it globally so that it can be used across different projects.
How to Install Composer on *nix and macOS
For *nix-based systems—Linux and Unix—it just takes a single command to install and run up Composer right away. Go ahead and run the following command in your terminal.
$curl -s https://getcomposer.org/installer | php All settings correct for using Composer Downloading... Composer (version 1.10.6) successfully installed to: /home/sajal/composer.phar Use it: php composer.phar
And with that, Composer is installed locally, and you are ready to use it with the php composer.phar
command! However, we want to install it globally, so let’s do that by executing the following command.
$sudo mv ./composer.phar /usr/bin/composer
Now, you should be able to use Composer globally. Go ahead and test it to see if it has installed successfully.
$composer --version Composer version 1.10.6 2020-05-06 10:28:10
On the other hand, if you’d installed Composer locally, you would run the php composer.phar --version
command.
So as you can see, it’s pretty straightforward to install Composer on Linux- and Unix-based systems. In the next section, we’ll see how to install it on Windows systems.
How to Install Composer on Windows
For Windows users, it’s even easier to install Composer. You just need to download the Composer setup executable and run it. Follow along with the installation steps, and it’ll install the latest Composer version. You should be able to run Composer commands right away.
It’s important to note that you need to restart your terminal after you’ve installed Composer since the PATH
variable only gets loaded when the terminal is started.
So that was a brief guide to installing Composer on different operating systems.
What Is Packagist?
Now, you’re aware of the basics of Composer and how to install it. The next question is: how do you know which libraries are available that you could install with Composer? Is there any central repository where Composer keeps a list of available libraries?
Yes: Packagist.
Packagist is the default Composer package repository from where Composer pulls libraries and its dependencies when you ask it to install a specific library. There are hundreds of libraries available on Packagist, which shows the popularity of Composer. In your PHP projects, if you need a feature which you think should be already available as a third-party library, Packagist is the first place you should check!
When it comes to searching libraries, Composer is also capable of looking beyond the Packagist repository. You could ask Composer to look at repositories other than Packagist for installing libraries by altering the repositories
key in the composer.json
file. In fact, this is what you’ll do if you want to manage your own private Composer packages.
In the next section, we’ll see how to use Composer to install libraries in your PHP projects.
How to Use Composer
Mostly, there are two ways when it comes to installing libraries with Composer. Let’s quickly go through it to understand how it works.
The install
Command
To use install
, you need to create a composer.json file in your project first. In the composer.json file, you just need to declare your project dependencies, as shown in the following snippet.
{ "require": { "phpmailer/phpmailer": "~6.1" } }
Next, when you run the composer install
command from that folder, Composer installs the phpmailer package and its dependencies in the vendor directory. More importantly, it also creates the composer.lock file, which maintains a list of all of the packages and the exact versions of them that are installed.
The require
Command
We can say that the composer require
command is a sort of shortcut for the previous process of creating a composer.json file. require
will add a package to your composer.json file automatically. The following command shows how to install the phpmailer package with the help of require
.
$composer require phpmailer/phpmailer
After installing the phpmailer package and its dependencies, require
also adds an entry of the package which is installed in the composer.json file. If the composer.json file doesn’t exist, it’ll be created on the fly. Of course, it also updates the composer.lock file to write package information, along with the exact versions.
So that’s how you can install the necessary dependencies in your PHP projects by using Composer. And with that, we’ve reached the end of this article as well.
Conclusion
Composer has become the de facto standard for managing project dependencies in PHP. In this article, we’ve gone through the basics of Composer and how to install it.
The Best PHP Scripts on CodeCanyon
The free libraries on Packagist are wonderful for basic functionality—the foundation for a good app. However, for more specialized features or for complete applications that you can use and customize, take a look at the professional PHP scripts on CodeCanyon.
Here are a few of the best-selling and up-and-coming PHP scripts available on CodeCanyon for 2020.