Composer: Install specific PHP package version
- TL;DR
- How to Install a Specific PHP Package Version Using Composer
- Basic Syntax
- Installing a Version Constraint
- Installing Without Updating Other Packages
- Editing composer.json Manually
- Checking Available Versions
- Downgrading a Package
- Installing a Specific Version Globally
- Verifying Installed Version
- Summary
- Conclusion
TL;DR
To install a specific package version using Composer:
composer require vendor/package:version
How to Install a Specific PHP Package Version Using Composer
When working on PHP projects, you may need to install a particular version of a package, either to maintain compatibility with your existing codebase or to match a production environment. Composer makes this simple with version constraints.
This guide covers all the correct and reliable ways to install a specific version of a package using Composer.
Basic Syntax
The most direct way to install a specific package version is:
composer require vendor/package:version
Replace:
-
vendor/packagewith the actual package name (for example,laravel/framework) -
versionwith the version number you want to install.
Example:
composer require monolog/monolog:2.9.1
This installs exactly version 2.9.1 of Monolog.
Composer will update your composer.json file like this:
"require": { "monolog/monolog": "2.9.1"}
and automatically update your composer.lock file and install the package.
Installing a Version Constraint
Composer also allows flexible version constraints instead of a single fixed version. This is useful if you want to stay within a major or minor release line.
Common examples:
| Constraint | Meaning | Example |
|---|---|---|
1.5.* |
Any version starting with 1.5 |
composer require guzzlehttp/guzzle:1.5.* |
^1.5 |
Any version >=1.5.0 but <2.0.0 |
composer require guzzlehttp/guzzle:^1.5 |
~1.5 |
Any version >=1.5.0 but <2.0.0, allowing minor updates |
composer require guzzlehttp/guzzle:~1.5 |
>=1.5 |
Version 1.5 or higher |
composer require guzzlehttp/guzzle:>=1.5 |
1.5 - 2.0 |
Any version between 1.5 and 2.0 inclusive |
composer require guzzlehttp/guzzle:"1.5 - 2.0" |
Installing Without Updating Other Packages
By default, composer require may trigger dependency updates.
If you want to install a specific version without updating other packages, use the --no-update flag:
composer require monolog/monolog:2.9.1 --no-update
This adds the requirement to composer.json but doesn’t install or update anything yet.
You can then run:
composer update monolog/monolog
to install just that package according to your version constraint.
Editing composer.json Manually
Another approach is to manually specify the version constraint in composer.json, then run composer update:
"require": { "monolog/monolog": "^2.8"}
Then install:
composer update monolog/monolog
Checking Available Versions
If you’re unsure which versions are available for a package, run:
composer show vendor/package --all
Example:
composer show monolog/monolog --all
This displays all available versions, release dates, and dependencies, helping you choose the correct one before installation.
Downgrading a Package
To downgrade, simply require an older version:
composer require monolog/monolog:2.4.0
Composer will resolve dependencies and adjust your composer.lock file accordingly.
If a newer version is already installed, Composer will automatically downgrade it.
Installing a Specific Version Globally
To install a specific version of a global Composer package (e.g., a CLI tool):
composer global require laravel/installer:^5.3
You can verify it by running:
composer global show laravel/installer
Verifying Installed Version
To confirm the installed version of any package, run:
composer show vendor/package
Example:
composer show monolog/monolog
Output includes the exact installed version and metadata.
Summary
Here’s a quick reference for installing specific versions:
| Task | Command |
|---|---|
| Install an exact version | composer require vendor/package:1.2.3 |
| Install a version constraint | composer require vendor/package:^1.2 |
| Avoid updating other dependencies | composer require vendor/package:1.2.3 --no-update |
| Install globally | composer global require vendor/package:^1.2 |
| Check available versions | composer show vendor/package --all |
Conclusion
Installing a specific PHP package version with Composer is straightforward but essential for maintaining stability and compatibility across environments. Whether you’re locking to an exact version or defining flexible constraints, Composer gives you full control over dependency management efficiently, predictably, and with precision.
Stay Updated.
I'll you email you as soon as new, fresh content is published.