Just in case you’re not aware of Composer is, it’s simply a third-party package managing tool. It brings streamlined efficiency to adding new libraries to your projects by writing simple one-line commands.

Contents

Installation

Prerequisites

Before you follow any of the guides below, you must ensure that PHP is already installed on your computer. Composer will not run properly without it and you will soon run into problems. In addition, you must set PHP as an environmental variable within your system for shortcut command access to the engine. See the following steps to do it on Windows.

Go to System Properties Go to the advanced tab Click Environment Variables. If not already present, create a new variable named PATH. If already present, go to the next step. Create a new variable value within the PATH variable, this value needs to be the path to your PHP folder, where the php.exe is located. In my case its C:\xampp\php After adding this value, press the OK button till all the windows are closed.

Installing Composer Software on Windows

Note: If you don’t have sufficient rights on your computer to install any software, please see the next section, installing on Mac & others. As this is a workaround for Windows users with this issue.

The easiest way to install Composer on windows is by downloading the native Composer-Setup.exe. This installs the required files to your local computer that are ready to be utilized within your projects. See the following steps and sub-steps –

Step 1

Download the Composer-Setup.exe file from the download page here. Install the software.

Step 2

Close all file-explorers and command windows. Open a new command window and type composer You should see Composer should load its initial home output, if not, see the paragraph below. If the prompt complained about PHP not being recognized, then see the prerequisites of this tutorial.

If you got the response of ‘composer is not recognized as an internal or external command’, then you will need to set the composer bin path in your environmental variables. On my device, this bin folder is located in ‘C:\Users\CodeWall\AppData\Roaming\Composer\vendor\bin’. This is the value you will need to add to your PATH variable.

Installing Composer Manually on Mac, Linux & Windows

Composer can be installed via a PHP Archive file, namely composer.phar. This file is available from download page, underneath the Manual Download sub-heading. Opt for the latest version, at the time of writing it is 1.8.0. After the composer.phar file is downloaded,

Step 1

Locate the composer.phar on your device and drag/move it to the root directory of your PHP project folder. Now open your command prompt and navigate to the projects root directory, for example, on mac it would be cd ~/phpProject or on Windows cd C:/phpProject Once here type the following command. php composer.phar Composer should load in the terminal. If it doesn’t and the terminal complains about PHP not being a recognized command, then see the prerequisite of this tutorial.

That’s it with the PHP archived package, it is now installed and ready to be utilized as and when without physically installing to your system.

Using Composer

Before you can utilize the power of Composer, see the following checklist is complete

Composer is installed, if not, see Installation section.

Both PATH variables for PHP and Composer so that they are accessible via shortcut commands in the terminal, if not, see the installation section.

Throughout these remaining examples, the demonstrations will be carried out with a Windows-based system using its native command prompt. Some commands may differ on your operating system but will be easy enough to find in the operating system’s documentation.

Initializing Composer with your project

Composer needs a kind-of-settings file to run properly, and ultimately for you to instruct it on what it should do. The composer.json file doesn’t come fresh-out-the-box so one must be created. Luckily, the composer engine helps with this task, literally walking you through each step of creation. See the following steps to build the .json file.

Step 1

Open the command prompt. Go to your PHP project folder, for this example, the following command was used – cd C:\project , make sure you replace the path in the example command with your project path. Then run the following command – composer init Type in a Package Name, something descriptive of your project and hit enter. Add a small description of your project and hit enter. For the Minimum Stability input, it depends on the project at hand, you can see further details in the docs but for this example, just press enter, and it defaults to ‘stable’. For the Package Type, it can be either of the following, Package, Library, meta package or composer-plugin. Pick what is relevant, but in this case, the project was entered. The license is another personal circumstance, if there is going to be a license, then you will need to specify it, if not, simply hit enter. The next line will ask you if you want to define your dependencies, at this moment, type no and press enter, and repeat the same for the dev-dependencies. Finally, type yes to confirm your settings and Composer to generate the file. See Figure 1 for an example of all these steps.

Step 2

Now let’s double check that the file was actually generated.

Go to your project folder and locate a file named composer.json. Open the file and check the configuration you entered is present. If it is, then we are good to go! If not, go back to Step 1 and repeat all the sub-steps. It should look similar to Figure 2 below.

Requiring a Package

Now for the fun part, the ability to download and install useful libraries with one line commands! There are actually two ways to do it, one via the command line directly. Or specifying the package within the composer.json file first then running a command. This probably sounds a bit confusing, but in the next couple of examples, you will come to understand the meaning of two separate ways.

Method 1. Requiring Packages using the Require command

This first way and my personal favorite is to simply use the Require command and let Composer do its magic. See the following steps to download a package named Guzzle, an excellent HTTP client library, but feel free to choose a library from packagist of your own, there is currently over 200 thousand to pick from!

Open the command prompt and navigate to your projects root directory, for example cd C:\project Enter the following command for the packages default version – composer require guzzlehttp/guzzle and press enter. If you need a specific version, skip this step and see step 3. Requiring a specific version use the following command composer require guzzlehttp/guzzle ^versionNo ) and press enter. Give it a few seconds and the process should begin. The terminal will output some messages, and you may see that multiple packages have been installed, this is not uncommon, so do not worry. The package probably has dependencies of its own. See Figure 3 for the terminal state after requiring Guzzle. Check your project directory for a new folder named vendor, this will now house some more folders which have just been downloaded and installed for you automatically! Lastly, open your composer.json file again and you should see that the Require section now has a new package present. See Figure 4 for an example.

Method 2. Requiring Packages using the Install command

So, there is another method of downloading and installing packages without actually specifying the package name in the command line. Rather, you define this in the composer JSON file and simply run. composer install See the following guide on how to utilize this method correctly.

Locate the composer.json file in your projects root directory, for example, C:\project Open the file with an editor. See Figure 2 above to see what it should be similar too, but maybe you already have packages defined from the last method. The required object is the point of importance, this is where we want to specify the next package to install. For this tutorial, the package of Guzzle will be installed again. Add the following line to your require section – "guzzlehttp/guzzle": "^6.3" Save the file, check Figure 4 for what your file should be similar too. Now boot up your command prompt and navigate to the root directory of your project, where the composer.json file is located. For example – cd C:\project Now run the following command – composer install and hit enter Sit back, relax, and watch the new package install automatically. See Figure 5 for an example of what it looks like after an update command is ran. And that is it, check your vendor folder to see if the packages are now present.

Method 3. Requiring Packages using the Update command

There is one last way to install packages but it’s not the recommended choice. Before using this method, it must be understood that all packages in the require section of your JSON file may be updated, and could, in fact, cause issues with dependencies you are already using.

For example,

Package 1 is of version 1.0 currently, and you’re using it throughout your project. When you run composer update, it could update to version 1.2 and some of your code could be using deprecated items or something of a worse case.

Nevertheless, this command can become very handy if you wanted to update all your dependencies in one go!

. See the following guide on how to utilize this method correctly.

Locate the composer.json file in your projects root directory, for example, C:\project Open the file with an editor. See Figure 2 above to see what it should be similar too, but maybe you already have packages defined from the last method. The required object is the point of importance, this is where we want to specify the next package to install. For this tutorial, the package of ChartJS will be installed, a visualization library. Add the following line to your require section – "nnnick/chartjs": "^2.7.3" Save the file, check Figure 6 for what it should be similar too. Now boot up your command prompt and navigate to the root directory of your project, where the composer.json file is located. For example – cd C:\project Now run the following command – composer update and hit enter Sit back, relax, and watch the new package install automatically. See Figure 7 for an example of what it looks like after an update command is ran. And that is it!

Deleting Packages from your project

This is one of the real gems of Composer, the ability to delete packages with a few simple actions. The fact that the project’s package directories can become vast, it can be trivial to start removing unwanted packages. Not with Composer though, it really makes this part a dream for you. See the following steps on how to remove a package.

Locate the composer.json file in your projects root directory, for example, C:\project Open the file and find the Require section Remove one of the packages defined here, don’t forget the trailing comma. Save the file. Open the command prompt and navigate to the root directory of your project, where the composer.json file is located. For example – cd C:\project Now run the following command, referencing the package for removal – composer remove nnnick/chartjs and hit enter Once again, allow Composer to do its thing. You will notice that there is a removing message, see Figure 8 for an example. Now the package has been removed, check your projects vendor folder, and you will notice that the package has now vanished! Lastly, check your composer.json file and you will again, notice that this package has been removed from there too.

Using a package in your project

In this very brief example, you will see how to utilize the projects that you have installed. See the following steps to use the GuzzleHttp package that was installed earlier in this tutorial.

Open your index.php file. Add the following line to autoload all packages in the vendor directory. require_once __DIR__ . '/vendor/autoload.php'; Then make sure you define a use statement to expose the library. use GuzzleHttp\Client; Finally, call the class like the following – $guzzle = new Client(); That is it, you can now use the packages full potential. See the full example code below.

require_once __DIR__ . '/vendor/autoload.php'; use GuzzleHttp\Client; $guzzle = new Client();

For a further understanding of auto-loading packages, see this very detailed article on phpenthusiast.

PDF Download

For those of you who would rather have this tutorial offline and printed, the tutorial has been compiled into a PDF.

Use the following link to download it.

Composer Tutorial PDF

Summary

After this tutorial has been carried out, you will be able to see the power, convenience and great management benefits that are present. Composer is an excellent tool to have and is becoming more and more popular every day. As always, any issues following this tutorial, please let me know in the comments section.

The Author: Dan Englishby

Post navigation