Installing and using Composer on Windows

Let’s see step by step how to install composerhe PHP dependency manager par excellence, and some examples of how to use it to create projects or install libraries in an agile way.

In this tutorial you will find:

  1. Basic system requirements to get composer working.
  2. Quick installation of Composer on Windows.
  3. Installation or use through composer .phar file in Windows.
  4. How to use composer on Windows.
  5. What is the composer.lock file and what is it for?

Content

  • System Requirements
  • How to Install Composer on Windows
    • Using the Windows installer
    • Manual installation on Windows
  • Create new projects with Composer
  • Download libraries and dependencies with Composer
  • Composer to update libraries
  • BONUS: What is the composer.lock file for?

System Requirements

Before installing anything we must verify that our equipment will be compatible with Composer. The minimum system requirements are:

  • Have PHP 5.3.2 or higher installed. If your PHP installation is more than two years old, it will almost certainly meet this condition, but if you still have doubts, you can check the version in several ways:
    1. Go to the console of our system, write php -v (WINDOWS with the PATH variable set) php –version (LINUX)
    2. Another way is to run the php function phpversion() in any script php.
    3. If you have WAMP installed on Windows in the php installation path, normally C:\wamp\bin\php You will have a folder named phpX.XX with the php version.
  • Some specific php configuration directives are also needed, but installing Composer will alert you to any incompatibility.
  • Regarding the operating system, Composer works with both Windows, Linux or MacOS.

How to Install Composer on Windows

Using the Windows installer

This is the easiest way to install composer on Windows.

First you must download the official Composer installer from its website: composer.exe installer

Then you must run the downloaded file. What this installer will do is install the latest version of Composer available and set the environment variable (PATH) so that you can call the specific command (composer) from anywhere on your PC at the Windows command line.

Note: If you had the Windows terminal (cmd) open, close it and reopen it. Only the information from the PATH environment variable is loaded

Manual installation on Windows

For the manual installation of Composer you will need to download the file with the extension .phar. You can do it from the Composer download page (in English) or from the official link that I leave you: version 1.2.4 of 12/6/2016

Now we must create a folder to identify the installation location. Put the downloaded file in a folder named composer for example in c:/ and open the Windows CMD for that folder. Type the following command in CMD to create a .bat file:

C:\bin>echo @php "%~dp0composer.phar" %*>composer.bat

Now we only needset the PATH environment variable (a special WINDOWS global variable) with the path to our folder with the composer files. in my example c:\Composer\. We are going to do this with CMD command line. We wrote:

SET PATH=%PATH%;c:\Composer\

To check that our installation is correct we must close CMD and reopen it. We write the following command and If it runs successfully, we are done.:

composer -V

Create new projects with Composer

One of composer’s features is the installation of frameworks PHP like symfony, laravel, cakephp etc. With Composer we can download the entire structure of folders and dependencies of the framework desired leaving the project ready to start programming.

For one symfony clean install For example, we could use the following command:

composer create-project symfony/framework-standard-edition my_project_name

or for a specific version of the framework:

composer create-project symfony/framework-standard-edition my_project_name "2.8.*"

Download libraries and dependencies with Composer

Composer allows you to quickly and agilely install libraries and dependencies of our projects. If what we want is to install new libraries for our project, the first thing we should do is open the composer JSON file (composer.json), and add the desired parameters to require. For the dompdf library, for example, we would write:

"dompdf/dompdf": "dev-master"

If we were working on new composer.json it would be like this:

{
    "require": {
        "dompdf/dompdf": "dev-master"
    }
}

Next, to carry out the download and installation of the library we must open the command line, go to the root folder of the project and write the following instruction:

php composer install

Composer to update libraries

If we want to update the libraries to the most recent versions available with respect to our composer.json we can run the command

php composer update

This command, executed in the root folder of our project, will download and update the libraries indicated in the composer.json to the latest version available for the set configuration.

if in the file composer.json we have indicated the exact version of the library, the composer update will have no effect on it, since the version is fixed. If, on the contrary, we have indicated a version of the type “3.2.*” it would update to a version within the 3.3.0 and 3.2.0 range.

BONUS: What is the composer.lock file for?

After installing the dependencies established in the file composer.json composer will write the exact versions installed in a file called composer.lock. This file sets the specific versions used by the project.

It is recommended to include the file composer.locknext to composer.jsonin your preferred version control system (such as GIT).

When we make a install Composer checks if the file exists .lockIf so, the exact versions listed within are downloaded. This means that anyone who builds the project with Composer will have the same versions of the downloaded libraries.

Leave a Reply