How to create composer.json file for PHP project.
PHP Composer is a package dependency management tool. 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
composer init --help
It will show you following 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.
composer init --require=twig/twig:1.13.* -n
Composer.json file is created, now open your composer.json file.
In composer file, we have mentioned package dependency and it’s version. After that, run composer install and press enter.
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.
This command will guide you through creating your composer.json config.
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.