DanielGG
DanielGG

Reputation: 61

composer gives error locally on php v7.4 and ubuntu 22.04

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?

Upvotes: 5

Views: 12707

Answers (3)

Anthony Urbina
Anthony Urbina

Reputation: 21

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

Upvotes: 0

slashleo
slashleo

Reputation: 113

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

Upvotes: 10

DanielGG
DanielGG

Reputation: 61

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

Upvotes: 0

Related Questions