3

im having a little problem here.

Im using Ubuntu 22.04 and im working locally with php 8.1 and composer 2.2.6 and everything works good, but now i have project that requires php 7.4 so i installed the php v7.4 and change it that its my globally version for my system.

The problem now is that it doesn't find the composer.

When i run composer, composer install, composer -v or similar it gives me this error.

PHP Parse error: syntax error, unexpected '|', expecting variable (T_VARIABLE) in /usr/share/php/Composer/IO/BaseIO.php on line 163 Parse error: syntax error, unexpected '|', expecting variable (T_VARIABLE) in /usr/share/php/Composer/IO/BaseIO.php on line 163

Do some of you know what the problem may be? Should i install composer also for the php v7.4?

DanielGG
  • 41
  • 1
  • 2
  • "it doesn't find the composer" - what does that mean? The error message is pretty clear about the code that is executed, and Composer 2.2.6 should be compatible with PHP down to v5. What have you tried to resolve the problem? How did you install Composer and PHP in the first place? – Nico Haase Aug 09 '22 at 13:34

3 Answers3

5

I have almost the same setup as OP: Ubuntu 22.04.1 LTS and a project which requires PHP 7.4.x, I got exactly the same error message when trying to run composer with any kind of argument, inside the project folder and also outside. I installed composer with sudo apt-get install composer and apt show composer -a gave me 2.2.6 as repository version.

The main difference is, that I don't have PHP 8.x installed as I don't need it (I installed php7.4 and extensions with the ppa:ondrej/php repository), so I couldn't switch my PHP version.

What was working for me was removing composer with sudo apt-get remove composer and then following the download guide on the official composer website: https://getcomposer.org/download/

WARNING: They explicitly say to not redistribute the install code, because it will change with every version but instead link to the download page (done). I will do it anyway because the current latest version (2.4.2 at the time of posting) works for the same circumstances specified in OPs question and for documentation purposes.

How to install Composer locally (might need sudo, and second line hash will be different if version changes):

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '55ce33d7678c5a611085589f1f3ddf8b3c52d662cd01d4ba75c0ee0459970c2200a51f492d557530c71c15d8dba01eae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

After that, if you want to use it globally put it somewhere in PATH e.g.:

sudo mv composer.phar /usr/local/bin/composer

composer --version and also running install/update should work now

slashleo
  • 63
  • 7
0

I solved the problem with just installing the composer again but with php 7.4 as a php verison globally and now it works with both (php 7.4 and php 8.1).

DanielGG
  • 41
  • 1
  • 2
  • What do you mean by "with php 7.4 as a php verison globally"? How did you do that? – Nico Haase Aug 09 '22 at 13:34
  • 1
    You can have multiple php verisons on your system and than with "sudo update-alternatives" --config php" you can choose which one you want to be used globally from your system. https://help.clouding.io/hc/en-us/articles/360021630059-How-to-Install-Multiple-PHP-Versions-7-2-7-4-8-0-and-8-1-on-Ubuntu-20-04 – DanielGG Aug 09 '22 at 17:16
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 12 '22 at 13:09
  • Thanks @DanielGG, removing Composer, then running `sudo update-alternatives --config php`, choosing version 8.1 and reinstalling Composer fixed the original issue for me. For what it's worth, I ran into a different error, "Class 'Normalizer' not found", which I resolved by running `sudo apt install php8.1-intl` (see https://stackoverflow.com/a/71420719/570570). – appel Dec 17 '22 at 21:04
  • I tried to do the same as @appel but inverse, going from php8.1 to php7.4 and it did not work. slashleo solution did work for me. – Guilherme Sampaio Jan 09 '23 at 17:44
-2

Try remove and reinstall composer globally:

#Remove Compser:

sudo apt-get remove composer

#Install composer:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '55ce33d7678c5a611085589f1f3ddf8b3c52d662cd01d4ba75c0ee0459970c2200a51f492d557530c71c15d8dba01eae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

#This install composer globally:

sudo mv composer.phar /usr/bin/composer

#Use composer in your project:

composer install

This run to me, hope it will help you!! :3

  • 1
    Please do not duplicate existing answers, unless you want to share new insights. If this is the case here, please edit your answer to contain all neccessary details – Nico Haase Apr 20 '23 at 04:07
  • Hello, the difference is at the end of the answer, sudo mv composer.phar /usr/bin/local/composer didn´t worked for me. I had to run: sudo mv composer.phar /usr/bin/composer – Anthony Urbina May 02 '23 at 14:19
  • Please add all neccassary clarification to your answer by editing it – Nico Haase May 02 '23 at 14:32