Composer is a package dependency management tool. In Composer you can declare the libraries you need them in your project and it will install them. In this post i’ll talk about how to create composer.json file for your project.
In my previous post, i have written what is composer, how to manage package dependency through composer.
How to Create composer.json File
I assume you have composer install on your system. Before creating 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 composer.json file the key element is require. In which we mention what package we need to install for current project.
You can create composer.json file manually or through command line. Better way is to create composer.json file through command line.
To create composer.json file through command line. Write
composer init --require=twig/twig:1.13.* -n
Composer.json file is created,now open composer.json file.
I define the package twig and it’s version which i require. After that i run
It will download and install the package mentioned in composer.json file.
To create composer using all the options simply type.
This command will guide you through creating your composer.json config.
Package name (/)
Where to Find The Packages
In this article we talk about how to define and install the packages which you want to pull for your project. But where to find these packages. By default composer will look for defined packages on Packagist, it’s online repository. Go on packagist.org , browser the package you need for your project and you will find the require, include this into your composer.json file.