Send by LINE
B! Bookmarks in Hate-bu
Bookmarks in Pocket
RSS feeds

php composer: How to use package management

Other language site
No available translations found
Google Translate
php logo

php composer is a tool to manage a wide variety of additional functions. It is how to use it.

What is the package of php?

php can add a variety of functions. These functions are published in GitHub etc. in the unit of package. A tool to manage that package is composer.

You should think of it like Python's pip, Ruby's gem.

Program Package management
PHP composer
Python pip
Ruby gem

Use the composer command to add, update, and delete packages. Also, in recent years, laravel and other frameworks can be easily installed using this composer.

When dealing with php it is good to think that composer is sure to use.

How to install composer

The composer installation is done by downloading and setting composer.phar file.

The composer.phar file is obtained from here (

Setting command execution path

Move the composer.phar file to the directory where the command execution path is passed.

mv composer.phar /usr/local/bin/composer

It does not necessarily have to be put in /usr/local/bin. It is optional as long as $ PATH passes.

For Windows, an installer (Setup.exe) is prepared. You can get it from the above download site. You can install it easily according to the instructions of the installer.

How to install packages using composer

There are two methods for installing it: installing in the execution user environment and installing only for specific php projects.

Install in execution user environment

When installing to an executing user, you can use the same package with multiple php projects.

It is used for installing commonly available packages that do not depend on the environment of the project, such as the static code checking tool.

composer global require package_name

Use the require command to install the package. It installs to the execution user environment by adding global.

The compoer home directory when installed in the execution user environment is the following location.

windows C:\Users\{user_name}\AppData\Roaming\Composer
mac ~/.composer

A composer.json file is created in this home directory. This json file is a configuration file that contains information on packages managed by composer.

Also, the package installed by composer will be added to the vendor directory.

Install on php project

To install to the php project, do the following steps.

Step1. Move to project home directory

Step2. Perform initialization of composer

It is unnecessary if composer already exists in php project. Proceed to the next step.

composer init

Execute the above command so that package management can be done with composer in the php project.

At the command prompt you will be prompted for a variety of inputs, but you can enter them all at once as you can edit them in the configuration file.

A composer.json file is created in the project's home directory. The home directory of the project is also the composer's home directory.

If you execute the composer command in the php project home directory, you can add packages only within the php project.

Step3. Add package

Install using composer. Just execute the command below.

composer require pagekage_name

composer command


composer init

Initialization processing of package management by composer.

When package management is done on a project basis, it is executed once at the beginning.

Create a composer.json file in composer's home directory.

The composer.json file describes the packages and their versions added to the environment, as well as the package settings, such as packages for development environments.


composer [global] require package_name

It is used for adding and updating packages.
Add packages to composer.json file and update version of existing package.

Also update the composer.json, composer.lock file.

The composer.lock file contains package installation information. By sharing this file you can easily build the same php environment.


composer install

composer.json, composer.lock file to build the php environment.

At the development site there are scenes where you create several same environments. We will use it at that time.

  • Build a production environment from development environment
  • Build from development server to local virtual environment
  • Construct multiple servers with the same environment for load balancing. etc...


composer update ["package_name"]

Rebuild the package's resources from the composer.json file and make it into the composer.lock file.

It is used when editing composer.json file directly. Normally you will not have a scene to use.


composer show [package_name]

Displays the installed package information.

If the package name is omitted, the package list is displayed. If package name is specified, detailed information on that package is displayed.


composer self-update

Perform update of composer.


composer create-project package_name path ver

You may be able to create projects using composer, such as web framework such as laravel. By using this you can create an easy and fast environment.


composer dump-autoload

It is used in an environment using class autoloader. When adding or deleting a class, update the class map.

Scenes to use when using laravel come up.

composer dump-autoload -o 

The class information changed by executing the above command is reflected in the class map.


This is not the only way to use composer.

Besides, there are important elements such as how to divide the development environment from the production environment, but in the meantime I introduced only the minimum usage.

Other detailed usage methods will be added later.

Leave a Reply