How to create composer.json file for PHP project.
PHP Composer is a cross-platform package dependency manager that manages package dependency in a PHP project. In Composer file, we can mention the dependencies which we need for our project and it will download them. In this tutorial, You are going to learn how to create a composer.json file for your PHP project.
If you are not familiar with composer then read my previous tutorial. What is PHP Composer and how to manage package dependency through composer?
How to Create composer.json File
I assume you have already installed composer on your machine. Before creating a composer.json file, let’s check it’s schema. Open your terminal and type
How to install PHP composer on Mac
1 |
composer init --help |
It will show you following options.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
Options: --name Name of the package --description Description of package --author Author name of package --homepage Homepage of package --require Package to require with a version constraint, e.g. foo/bar:1.0.0 or foo/bar=1.0.0 or "foo/bar 1.0.0" (multiple values allowed) --require-dev Package to require for development with a version constraint, e.g. foo/bar:1.0.0 or foo/bar=1.0.0 or "foo/bar 1.0.0" (multiple values allowed) --stability (-s) Minimum stability (empty or one of: stable, RC, beta, alpha, dev) --license (-l) License of package --help (-h) Display this help message. --quiet (-q) Do not output any message. --verbose (-v|vv|vvv) Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug --version (-V) Display this application version. --ansi Force ANSI output. --no-ansi Disable ANSI output. --no-interaction (-n) Do not ask any interactive question. --profile Display timing and memory usage information --working-dir (-d) If specified, use the given directory as working directory. |
But don’t worry in a composer.json file the key element is require. In which we mention the package dependency which we need for our project.
You can create a composer.json file manually or through a command line. Let’s create a composer.json file through the command line.
To create a composer.json file through the terminal we use composer init command.
1 |
composer init --require=twig/twig:1.13.* -n |
Composer.json file is created, now open your composer.json file.
1 |
vim composer.json |
1 2 3 4 5 |
{ "require": { "twig/twig": "1.13.*" } } |
In composer file, we have mentioned package dependency and it’s version. After that, run composer install and press enter.
1 |
composer install |
It will download and install the necessary package dependency mentioned in a composer.json file.
To create a composer file using all the options simply type.
1 |
composer init |
This command will guide you through creating your composer.json config.
1 2 |
Package name (/) ..... |
From Where to Find the Packages Name and Version for Composer
In this tutorial, we have talked about how to define and install the packages through composer. But where to find these packages. By default composer will look for defined packages on Packagist, it’s online repository. Go on packagist.org , browse the package you need for your project and you will find the require, include this into your composer.json file.