Installing the package

In order to install the packages (the code libraries that we want to download from Packagist to our project), the packages need to be registered to a composer.json file within the root directory of the website.

We'll get back to the composr.json later because we first need to find a package to install in the Packagist repository, which is found at packagist.org.

In the packagist.org website, type the name of the package that you want to download in the search box. For the sake of this tutorial, we’ll search for the “faker” package which generates fake data for our projects (something that can be most beneficial during the development stage of a website).

You will be shown a number of packages from which you need to choose the package that suits your needs the best.

Click on the package to open the package page. Here you can find relevant information about the package, including the latest version and dependencies. You need to copy the command to download the latest stable version (see the picture below).

Back to the terminal, you need to navigate to the root directory of your app or website, and run the command that you just copied from packagist.

$ composer require fzaninotto/faker

Once you press enter, Composer will start to auto-magically download the latest stable version of the package that you require. It also downloads all the packages on which the package depends and generates an autoload file.

At the end of the download process, when you navigate to the root directory, you will see a brand new vendor directory that holds within it all the packages that Composer installed in your website.

Also in the root directory, you need to create an index.php file.

In the composer.json file you will find the data that composer needs in order to work with, and manage the package that you have just installed. In our example:

{ "require": { "fzaninotto/faker": "^1.5" } }

When you open the vendor folder, you’ll find the autoloader file (autoload.php) that comes free of charge with the download. We require the autoloader in our scripts in order for the Composer libraries to work.

require_once __DIR__ . '/vendor/autoload.php' ;

To activate the autoloader, you may need to type the following command into the command line:

$ composer dumpautoload -o

Let's write in the index.php file a code that tests if the package that we have just downloaded really works: