Configuring Packagist Mirrors for Faster Composer Package Management
Mirror Configuration Methods
There are two approaches to enable Packagist mirror services:
- **Global System Configuration:** Add configuration information to Composer's global configuration file
config.. See "Method One"
- **Project-Specific Configuration:** Add configuration information to a project's
composer. file. See "Method Two"
Method One: Modifying Composer's Global Configuration File (Recommended)
Open your command prompt (Windows users) or terminal (Linux, macOS users) and execute the following command:
composer config -g repo.packagist composer https://packagist.phpcomposer.com
Method Two: Modifying the Current Project's composer. Configuration File
Open your command prompt (Windows users) or terminal (Linux, macOS users), navigate to your project's root directory (where the
composer. file is located), and execute the following command:
composer config repo.packagist composer https://packagist.phpcomposer.com
The above command will automatically add mirror configuration information to the end of your project's
composer. file (you can also manually add it):
"repositories": {
"packagist": {
"type": "composer",
"url": "https://packagist.phpcomposer.com"
}
}
As an example, consider a Laravel project's
composer. configuration file after executing the above command (note the last few lines):
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"require": {
"php": ">=5.5.9",
"laravel/framework": "5.2.*"
},
"config": {
"preferred-install": "dist"
},
"repositories": {
"packagist": {
"type": "composer",
"url": "https://packagist.phpcomposer.com"
}
}
}
That's all! Try running
composer install to experience significantly faster package installation!
Mirror Principle
Typically, package data (mainly zip files) is downloaded from
github.com, while package metadata is downloaded from
packagist.org. However, due to well-known reasons, international websites often have slow connection speeds and may be blocked or inaccessible.
The "Packagist China Full Mirror" service caches all packages and metadata in domestic data centers and accelerates delivery through domestic CDN. This eliminates the need to request international websites, thereby accelerating the
composer install and
composer update processes, making them faster and more stable. Therefore, even if
packagist.org or
github.com experience issues (mainly slow connections or being blocked), you can still download and update packages.
Disabling Mirrors
If you need to remove mirror configuration and restore to the official Packagist source, execute the following command:
composer config -g --unset repos.packagist
After execution, Composer will reset the source address to its default value (the official source). If you need to use the mirror again in the future, simply set the mirror address using the methods described in "Mirror Configuration Methods" above.