How to Install and Use Composer – Complete Tutorial

In this article, we will show you how to install and use on various platforms. Composer is a dependency manager for PHP. It is a simple and reliable tool that developers use to manage and integrate external packages or libraries into their PHP-based projects. This way, they don’t have to create their web pages or web applications from scratch.

To help you master this tool, you’ll also learn how to create a basic PHP project.

Before learning how to install Composer, make sure you have access to the command line interface on your system or server.

Installing Composer

The first and easiest thing in this tutorial is to install Composer on your system. Here you can find the installation guide for both Windows and Linux. Mac users just need to open the terminal and follow the same steps as for Linux.

1. Install Composer on shared hosting, Linux or macOS

The commands to install Composer on shared hosting, Linux (PC or server), and macOS are the same.

Composer is now pre-installed on shared hosting plans. If you use shared hosting, you can skip this part. However, if you find that it is out of date, you can update Composer by running this command:

php composer.phar self-update

Follow these instructions to learn how to install Composer on your system:

  1. Connect to your hosting account using the SSH connection. You can learn how to do it in this .
  2. Download Composer from the official website with the following command: php -r “copy(‘https://getcomposer.org/installer’, ‘composer-setup.php’);”
  3. Verify the installer signature (SHA-384) to make sure the installer file is not corrupted. Introduce: php -r “if (hash_file(‘sha384’, ‘composer-setup.php’) === ‘e0012edf3e80b6978849f5eff0d4b4e4c79ff1609dd1e613307e16318854d24ae64f26d17af3ef0bf7cfb710ca74755a’) { echo ‘Installer verified’; } else { echo ‘Installer corrupt’; unlink(‘composer- setup.php’); } echo PHP_EOL;”

    The long string of characters in the above command (“e0012edf…«) is the signature of the installer. It changes every time a new version of Composer comes out. So make sure you get the latest SHA-384 of .

  4. Once done, you can install Composer locally or globally. Local installation means that the dependency manager will be stored in your current directory, and you must specify the path before running the corresponding commands. Meanwhile, the global installation allows you to run Composer from anywhere on your system by storing it in the directory /usr/local/bin. Here’s how to complete both methods:
    • local installation: php composer-setup.php

      Overall installation:

      php composer-setup.php –install-dir=/usr/local/bin –filename=composer

      You will get this result:

      All settings correct for using Composer Downloading… Composer (version 1.10.5) successfully installed to: /usr/local/bin/composer

  5. Once done, remove the installer: php -r “unlink(‘composer-setup.php’);”
  6. Try installing Composer: composer

    The command line should show you this output:

    ______ / ____/___ ____ ___ ____ ____ ________ _____ / / / __ / __ `__ / __ / __ / ___/ _ / ___/ / /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ / ____/____/_/ /_/ /_/ .___/____/____/___/_/ /_/ Composer version 1.10.5 2020-02-12 16:20:11

2. Installing Composer on Windows

Getting started with Composer on a Windows machine is a bit different. No command line instructions are required to download and install the software.

Simply follow these steps:

  1. Install PHP on your computer. We recommend using it for this purpose, as the process is easy and you can complete it in a few minutes.
  2. Once XAMPP is installed, download the .
  3. Run the installation wizard Composer. When it asks you to activate developer mode, ignore it and continue with the installation process.
  4. Another window will appear and ask you to locate the PHP command line. By default, it is in C:/xampp/php/php.exe. After specifying the location, click Following.
  5. You will be prompted for proxy settings. Leave the box unchecked and skip this part by pressing Following. Then, in the last window, click Install.
  6. After the installation is complete, open the command prompt. press CTRL + Rwrite “cmd» and click okay.
  7. Enter the following command: composer

Great job! You now have Composer installed on your Windows computer. The installer will automatically add Composer to the PATH variable. Now you can open command prompt and run the software from anywhere.

Generating and understanding composer.json

Now comes the interesting part: using Composer in your PHP project.

To achieve this, you must generate a file composer.json. You can think of it as a way to fetch list data for Composer. This file contains packages (dependencies) that need to be downloaded.

Also, composer.json also check version compatibility with your project. This means that if you are using an older package, composer.json will inform you to avoid future problems.

You have the option to create and update composer.json yourself. However, since this is a tutorial on automating redundant tasks, we do not recommend that you create the file manually.

We show the usefulness of composer.json creating an example project.

Our project is a simple PHP timer, which allows developers to find out how long it takes to execute code. This is very useful for debugging and optimization purposes.

You can follow these steps:

  1. Create a new directory for the project. Since our project is a timer, we’ll just name the folder phptimer. To do this, run the following command: mkdir phptimer
  2. Enter the newly created directory: cd phptimer
  3. Find a package or library for the project. The best place to do this is , where you’ll find tons of libraries to help your project development. For this tutorial, we need a timer pack. To get it, just type timer in the search bar:

    As you can see, there are several timer packages available and each one has a name and a short description of what it does. In this example, we choose phpunit/php-timer as it has the most downloads and most GitHub stars.

  4. Specify the desired package so Composer can add it to your project: composer require phpunit/php-timer

    The output will show the version of phpunit/php-timer:

    Using version ^1.0 phpunit/php-timer

The caret (^) is defined as the option for maximum interoperability. This means that Composer will always update the package until a certain version causes it to fail in some way.

In our case, the package update rate is >=1.0.9 <2.0.0, as version 2.0.0 will break backwards compatibility. For detailed information on Composer releases, visit the .

After running the above command, your project directory will have two new files: composer.json Y composer.lockand a folder called seller. This is the directory where Composer will store all your packages and dependencies.

Using the Autoload Script

Your project is almost good, and the only thing left to do is load the dependency in your PHP script. And fortunately the Composer autoload file helps you complete this process faster.

To use autoloading, write the following line before declaring or instantiating new variables in your script:

https://getcomposer.org/doc/articles/versions.md

We will give you an example to help you understand better.

Let’s say you want to test our project phptimer:

  1. Open the nano text editor to create a script called demo.php. nano demo.php

    Then paste the following lines into your file:

  2. Run the script: php demo.php

    The terminal should show output similar to the following:

    double(1.0893424438611E-5) 0ms

Updating your project dependencies

Lastly, you should know how to update your packages. You can do this in two ways:

Remember to replace the part of vendor/package with the name of the package you want to update.

When executing the command update, Composer also update the files composer.json Y composer.lock to match the current state of your project’s dependencies.

conclusion

Composer helps developers in managing dependencies of PHP projects. Thanks to this software, they can easily integrate and manage open source packages in one place.

Additionally, Composer can also resolve dependencies on a per-project basis. Therefore, developers can control the packages for each project and keep the size of the project under control.

In this article, you have learned how to install and use Composer effectively. To summarize, let’s go through all the steps one more time:

  1. install Composer on a shared hosting system, Linux, macOS or Windows.
  2. Generate and understand the file composer.json.
  3. Use the autoload script (Autoload Script) to load dependencies into your PHP file.
  4. Update your project dependencies.

We hope that by following our guide, you will have a solid foundation for building amazing projects with PHP. Good luck and feel free to ask any questions in the comments section below.

Gustavo is passionate about creating websites. He focuses on the application of SEO strategies at for Spain and Latin America, as well as the creation of high-level content. When he is not applying new WordPress tricks you can find him playing the guitar, traveling or taking an online course.

See also  How to set up an email account on an iPhone
Loading Facebook Comments ...
Loading Disqus Comments ...