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

PHP composer, How to use package management

The php composer is a command tool for managing packages.

Packages are extensions to various php functions, which can be installed as needed to efficiently create programs that implement the desired functions.

I will show you how to use the composer command.

This is written by a Japanese who can't speak English with the help of translation application. Sorry if it's not good.

What is a php package?

You can add various functions to php. These functions are available as packages on GitHub and other sites. The tool to manage these packages is composer.

You can think of it as being similar to pip in Python or gem in Ruby.


The composer commands are used to add, update, and remove packages.

Recently, frameworks such as Laravel can also be easily installed using composer.

If you are using php, you must use composer.

Composer is OSS (Open Source Software), and the packages you install with composer are also OSS. You can use OSS for free, but be aware that not everything is freeware.

OSS also has a license. It just allows the following.

  • "Don't need the money."
  • "Program modification approved."
  • "Redistribution of the program is permitted."

A detailed license is written in the configuration file.

Note that it's never all 'free'.

Don't think that you can do anything about it.

How to install composer

composer is not included in php by default. You need to install it separately.

How to install a package using composer

There are two installation methods.

  • Installed in the OS execution user environment
  • Install only on specific php projects (limited to specified directories)

Installed in the OS execution user environment

composer global require package_name

The same package can be used by multiple php projects when installed on the OS execution user. Install packages that can be used commonly by multiple projects, such as static code checking tools.

Use the "require" command to install the package. If you add the "global" option to it, it will install the package in the OS execution user environment.

In this case, the home directory of the compoer will be in the following location

Mac, Linux~/.composer

In this home directory, a composer.json file and a vendor directory will be created.

composer.json is a configuration file that contains information about the packages managed by composer.

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

Install in a php project

Install the packages that will be used only for the php project.

Web frameworks, such as the Laravel framework, have different versions for different projects, so they must be installed in a specific project.

Let's take a step-by-step look at installing it into a php project.

Step 1. Go to the home directory of the project

Go to the home directory of your php project.

Step 2. Run the composer initialization

If you already have composer in your php project, you don't need it. Please proceed to the next step.

composer init

Run this to enable package management in the composer of your php project.

The command prompt will ask you to type various things, but you can edit them later in the configuration file, so just enter everything.

Then a composer.json file will be generated in the project's home directory. The home directories of the project and the composer are the same.

    "name": "***/***",
    "authors": [
            "name": "***",
            "email": "***@***"
    "require": {}

(**** will be created as appropriate depending on the environment)

If you run the composer command in the home directory of the php project, you can add packages and so on only in the php project.

Step 3, Add the package

To install using composer, simply run the following command

composer require pagekage_name

This is the same as removing the global option from the command to install in the OS user environment.

The difference between composer.json and composer.lock files

The composer.json and composer.lock files are the same in that they contain package dependencies. However, there is a crucial difference.

composer.json is a configuration file. On the other hand, composer.lock is a result file that is created when you run a command in composer that changes the package dependencies.

You can also edit the composer.json file directly.

However, do not edit the composer.lock file directly. This is because it is a result file that is automatically written out.

composer.json exports the packages installed by composer. However, it does not control which packages the package further depends on.

This dependent package is not written in composer.json, only in composer.lock.

Only composer.lock will contain information about all the packages you have installed.

composer.jsonA file with incomplete dependency checks for all packages.
Write only the packages to be installed by the user.
composer.lockA file in which package dependencies have already been inspected.
Write all packages installed in the composer, including dependent packages.

What happens when you run 'composer install'?

This means that when you run 'composer install', the speed of the package installation will be different depending on whether or not composer.lock is present.

If composer.lock is missing
Inspect all package dependencies from composer.json.

Output the result to composer.lock.

Install the package.
If composer.lock is present
Skip dependency checking of installed packages.

Use the composer.lock that already exists.

Run a dependency check on composer.json for the additions and changes.

Install the package.

If you have the composer.lock file, the installation process will be faster since it will start with the dependencies of the installed packages already checked.

composer.lock is used to create a copy of a PHP project, where all packages are exactly the same, and to set up a production environment.

Composer commands

There are many different composer subcommands that can be used. Here are some of the most commonly used ones.

init - Initial processing

composer init

This is the initialization process for package management. Basically, it is executed only once at the beginning.

Create a composer.json file in your composer home directory, which will contain the packages added to your environment and their versions, as well as the packages for your development environment and other package related settings.

If you run it while composer.json is present, it will overwrite the contents of composer.json.

require - Package addition and update

composer [global] require package_name
globalOS execution user installation options
package_namePackage Name

Used to add or update packages, add packages to the composer.json file, or update the version of an existing package.

Also, update composer.json and composer.lock. composer.lock will output the package installation information. This file can be shared to easily create the same php environment.

Updates can also be done with the update command.

install - Install all at once

composer install

If you have composer.lock, use it, if not, create a new composer.lock from composer.json and build the php environment.

There are situations where you need to create the same environment in the development site, so this is the time to use it.

  • Build a production environment from a development environment.
  • Build a local virtual environment from the development server.
  • Build multiple servers in the same environment for load balancing. etc…

The install command will scan all packages in composer.json and composer.lock.

The require command specifies the package individually.

update - Rebuild the package

composer update [package_name]
package_namePackage name.
If omitted, all packages are rebuilt.

Rebuild the package resources from the composer.json file, ignoring the contents of composer.lock for the current installation status. Then, regenerate the composer.lock.

This is used when you edit the composer.json file directly, such as changing the version of a package.

It will be used frequently during development, but when it is used in production, it will be used only for upgrade work.

show - show package information

composer show [package_name]
package_namePackage name.
If omitted, information on all packages is displayed.

Displays information about the installed packages.

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

self-update - Update the composer command

composer self-update

Run an update of the composer command.

create-project - Create a development project

composer create-project package_name path ver
package_namePackage Name.
pathInstallation location.
Project home path.
(* select latest)

You can create projects using Composer, including the web framework Laravel. This allows you to build an environment easily and quickly.

dump-autoload - Rebuild class map

composer dump-autoload

Rebuild the class map used by the class autoloading feature. The package versioning will not be changed.

Used in environments where class autoloading is used. Use this command to update the class map when you add or remove classes.

validate - composer.json syntax check

composer validate

This is a syntax check of composer.json, run after directly editing composer.json to check for errors in your edits.

Make sure you run composer.json after editing it.


This is not the only way to use composer.

There are also many other important things to know, such as how to set up a test package, and how to use different commands for development and production environments.

For now, I've only introduced the minimum usage.

I'll be adding other details on how to use it in the future.

Leave a Reply