Home » PHP » How to Install Specific Version of Package Using Composer

How to Install Specific Version of Package Using Composer

Composer is a package manager for PHP programming language. You can use it to install or update packages, manage their dependencies, and a lot of other things. By default, Composer installs the latest version of a required package.

But you can install any specific version which you want. In this short article, I will show you how to install a specific version of the package using Composer.

How to Specify the Package Version in Composer

1. Available Versions

Very often Composer packages use Git to store their files. So, Composer uses Git tags as package versions. You can find all available versions for a specific package using the following command:

composer show --all laravel/framework

Look at versions line. Also, you can find information about available versions on the official site. Open packagist.org and find the required the package using the search field. You will see information about available versions in the right sidebar:

If you have access to the repository, you can just show available tags:

git tag

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 used.

2. Version Constraints

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 write ^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.
  • >=x.x.x – allowed versions that are higher or equal than the specified one.
  • <=x.x.x – allowed versions that are lower or equal than 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.

In addition, you can use a specific branch name instead of a tag. For example, if you want Composer to install always the latest version of the package, you can specify the master branch as the version. But if you use the branch name as a version, you should add the dev- prefix to it. For example, dev-master.

By default, when you require any package without specifying the version, Composer uses the latest 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, just add the version number after the package name. For example:

composer require laravel/framework 9.36.1


composer require laravel/framework:9.36.1

You also can use version constraints here. But you should add double quotes to avoid errors from the shell:

composer require laravel/framework "^9.36.1"

Or 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:

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 the version or name in it. Local packages have higher priority than the packages from the repositories. So if you have added the repository correctly, Composer will look for the local package first.

4. Specifying Version for Update

In a simple case, you can just change the package version in the composer.json file to needed one and run the following command:

composer update

Or you can use the same require command. It will update or downgrade the package to the selected version if it is possible:

composer require laravel/framework:9.36.1

But in a lot of cases updating or downgrading packages is pretty difficult. Because any installed package depends on some packages and may be in dependencies of other packages. Changing its version may produce a dependency hell. The simplest way to solve this in my opinion is this sequence: remove all packages with conflicts, install the package that you want with the required version, and then, reinstall all removed packages that depend on it or its dependencies.

Wrapping Up

In this short article, I have explained how to install a specific version of the package using Composer. As you can see it is very easy. The versioning system can be pretty difficult to understand, but you don’t need to know all constraints.

Your Reaction

Leave a Comment

Exit mobile version