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