Composer is a package manager for the PHP programming language. It allows you to install or update packages, manage their dependencies, and many other things. By default, Composer installs the latest stable version of a selected package.
However, you can install any exact version of a package you need. In this short article, I will show you how to install a specific version of the package using Composer.
Table of Contents
How to Specify the Package Version in Composer
1. Available Versions
If you would like to install an older version of package, you need to find a version which you can install first. You can find all available package versions using the following command:
composer show --all laravel/framework
Look at the version line on the above example. Also, you can find information about available versions on the official site. Open packagist.org and find the required package using the search field. You will see information about available versions in the right sidebar or at the bottom of the page:
Usually, package files are stored using version control systems. For example, Git. There is an interesting circumstance, tag names in the Git repository should match the package version from composer.json. So if you have access to the repository, you can show available tags in the browser or command line:
git tag
2. Version Constraints
The version that you can specify in the command line when you install the package or in the composer.json file is not just a version. It is a version constraint that is used to determine the maximum version of the package that can be installed.
Let’s have a look at these constraints. Here is the most popular of them:
- x.x.x – you can set just a specific version. This means that Composer will not be able to update this package.
- x.x.* – the asterisk means any number, so if you set the version to 1.1.*, the package manager will be able to update it to the 1.1.2 – 1.1.9 versions but not to 1.2.0.
- ^.x.x.x – allows Composer to update the package until the new version does not break backward compatibility. For example, if you set version ^1.1.1, Composer can update the package to 1.1.4 or 1.2.0 but not to the next major version – 2.0.0. It is the most popular and recommended operator since it allows to automatically receive minor changes and security updates.
- >=x.x.x – allowed versions that are higher or equal to the specified one.
- <=x.x.x – allowed versions that are lower or equal to the specified one.
- ~x.x.x – this constraint allows changing only the last number in the version. For example, if you write ~1.2, the package manager can install the package with version 1.0 or 1.9.
Actual version of packages that are currently installed in your project can be found in the composer.lock file
You can also use a specific branch name instead of a tag. For example, if you want Composer to install the latest version of the package, you can specify the main or master branch name instead of the version. However, if you use the branch name as a version, you should add the dev- prefix. For example, dev-master.
By default, when you install any package without specifying the version, Composer uses the most recent version that satisfies the dependencies of other installed packages. Composer adds this version into the composer.json file with the caret ^ constraint.
3. Specifying Version for Installing
If you want to specify the version for any Composer package during installation, add the required version number after the package name. For example:
composer require laravel/framework 9.36.1
Or:
composer require laravel/framework:9.36.1
You also can use version constraints here. You should add double quotes to avoid errors from the shell, though:
composer require laravel/framework "^9.36.1"
Alternatively, you may want to install the latest development version of the package:
composer require laravel/framework dev-master
Note, that you should have set minimum-stability to dev in your composer.json file:
composer.json"minimum-stability": "dev",
Or you can set the stability of the package with the version using @ character:
composer require laravel/framework dev-master@dev
If you want to override any package by the local one, you don’t have to change its version or name. Local packages have higher priority than the packages from the repositories. So if your local repository is added correctly, Composer will first look for the package locally.
4. Specifying Version for Update
Now, you know how to install specific version of package. Let’s look how to update it. In a simple case, you can change the package version in the composer.json file to the needed one and run the following command:
composer update
Or you can use the require command. It will update or downgrade the package to the selected version if it is possible:
composer require laravel/framework:10.1
However, updating or downgrading packages is difficult in many cases. Any installed package depends on some packages, and others may depend on it. Changing its version may provoke a dependency hell. The simplest way to solve it, on my opinion, is to do the following steps:
- Remove all packages that have conflicts;
- Install the wanted package with the needed version;
- Reinstall all removed packages.
In this case, Composer will allow you to solve conflicts for each package individually so you can understand where exactly the issue is.
Wrapping Up
In this short article, I have explained how to install a specific version of a package using Composer. As you can see, it is straightforward. The versioning system can be difficult to understand, but you don’t need to know all constraints.