Recently working with
PHP Composer on a project that is based on the Laravel framework, I came across the error:
Invalid version string. More precisely when performing the attempt to install a Laravel package that allows me to use models with the logical strategy of Eloquent to access MongoDB databases.
The following full error message was displayed on my console:
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.
I solved the problem by keeping PHP Composer updated, so run the Composer auto-update and try again the installation of the desired package:
The installation of the package should be performed after the composer update finishes, in my case I used the following command on the terminal command line:
composer require jenssegers/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)
jenssegers/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 voilá, solved the error problem invalid string version, is related to an outdated version of PHP composer installed on your computer or server.
Note that ideally you’re running the latest version of Composer, v2.x.
However, in some contexts the updates of versions of systems and tools do not allow the update so frequent without first going through analyzes and homologations. This is the case of companies with a large volume of systems running on internal servers in their infrastructures.
I hope the tip helps you resolve similar issues.