Skip to content
Start » PHP » PHP Composer, error: Invalid version string

PHP Composer, error: Invalid version string

Recently working with PHP Composer on a project that is based on the framework Laravel, I came across the error: Invalid version string.

More precisely when trying to install a package Laravel which allows me to use models with Eloquent's logical strategy to access databases MongoDB.

The package name is Laravel MongoDB, created by Jens Segers and can be found in the Github repository jensegers/laravel-mongodb.

The following full error message was displayed on my console:

[UnexpectedValueException] Could not parse version constraint ^1.0.1: Invalid version string "^1.0.1"

The real reason for the error could not be identified, however, the solution is simple, especially if your PHP Composer installation is still using version 1.x.

Solution for PHP Composer 'Invalid version string' error

I solved the problem by keeping PHP Composer up to date, to do so, run Composer auto-update and try again to install the desired package:

composer selfupdate

The package installation must be performed after the composer update is finished, in my case I used the following command in the terminal command line:

composer require jensegers/mongodb

The above command resulted in the following message output in my terminal console:

Using version ~2.1 for jenssegers/mongodb ./composer.json has been updated Loading composer repositories with package information Updating dependencies (including require-dev) - Installing jenssegers/mongodb (v2.1.3) Downloading: 100% jensegers/mongodb suggests installing jenssegers/ mongodb-session (Add MongoDB session support to Laravel-MongoDB) jenssegers/mongodb suggests installing jenssegers/mongodb-sentry (Add Sentry support to Laravel-MongoDB)

And voila, the error problem is solved. invalid string version, is related to an outdated version of composer PHP installed on your computer or server.

Note that ideally you are running the latest version of Composer, v2.x.

However, in some contexts, system and tool version updates do not allow for such frequent updating without first undergoing analysis and approval. This is the case of companies with a large volume of systems running on internal servers in their infrastructures.

Hope the tip helps you to resolve similar issues.

keep up to date

Don't forget to follow me on twitter and sign the channel on Youtube to receive updates on new articles, video lessons and free courses in software development and engineering.

Leave a Reply

Your email address will not be published.

%d bloggers like this: