phpubuntucomposer-phpphar

How to install Phar on Ubuntu?


I got into a situation where if I run composer install on my PHP project, I get this error:

Fatal error: Class 'Phar' not found in /usr/local/bin/composer on line 23

As I read composer update class phar not found, I tried to check if Phar was installed by doing php -i | grep 'phar', and Phar is not installed.

I am using PHP 5.6.

How can I install Phar on Ubuntu please?

[EDIT] When I try apt-get install php-phar, I get:

Package php-phar is a virtual package provided by:
  php7.2-common 7.2.7-1+ubuntu14.04.1+deb.sury.org+1
  php7.1-common 7.1.19-1+ubuntu14.04.1+deb.sury.org+1
  php5.6-common 5.6.36-1+ubuntu14.04.1+deb.sury.org+1
  php7.0-common 7.0.30-1+ubuntu14.04.1+deb.sury.org+1
You should explicitly select one to install.

Then, if I try apt-get install php5.6-common:

php5.6-common is already the newest version.
php5.6-common set to manually installed.
0 upgraded, 0 newly installed, 0 to remove and 295 not upgraded.

Solution

  • Try to run php -i|grep phar

    If it shows result something like :

    /etc/php/{version}/cli/conf.d/20-phar.ini,
    Registered PHP Streams => https, ftps, compress.zlib, php, file, glob, data, http, ftp, compress.bzip2, phar, zip
    Phar-based phar archives => enabled
    Tar-based phar archives => enabled
    ZIP-based phar archives => enabled
    phar.cache_list => no value => no value
    phar.readonly => On => On
    phar.require_hash => On => On
    

    Everything should be correctly installed.

    If it shows no result or no phar ini files mentioned / disabled then you just need to include the correct ini on your main php.ini and set the path to phar.sofiles.

    or if you locate the phar.so file just add it on your main php.ini with

    extension=phar.so

    or

    extension=path/to/your/phar.so

    Update + extra to ensure ext dir location

    It will be clearer if you grasp ini file location and ext dir used by your installation.

    Try to do this: php -i | grep ini

    Then on the first/second line there will be your main ini file.

    In my machine it shows this: Loaded Configuration File => /etc/php/{version}/cli/php.ini

    then do this command to make sure ext folder used or any extension declared on main ini: cat /etc/php/{version}/cli/php.ini|grep ext

    You could also do:

    cat /etc/php/{version}/cli/php.ini|grep phar

    To check phar related parameter on main ini file.

    If those step show nothing, I think it will be simpler if you just reinstall your php package on your machine.