PHP: How do I install phpsh, interactive shell prompt for php under CentOS or Fedora?

phpsh requires readline support built into python. It also requires python version 2.4+. You can check which version of python you have installed by typing:

python -V

Let us download and install readline:

tar zxf readline-5.2.tar.gz
cd readline-5.2
make install

Now let us install python with readline support:

tar zxf Python-2.5.1.tgz
cd Python-2.5.1

I had some problems on one of the servers where it would not compile readline support in to python. I was able to compile reading support in to python by:

echo "readline readline.c -lreadline -ltermcap" >> Modules/Setup.local

Now let us continue with python installation.

./configure --prefix=/usr/local/python-2.5.1 --enable-readline
make -i install

If you do not do “make -i install”, install may fail with following error (-i means ignore any errors):
Compiling /usr/lib/python2.5/ ...
make: *** [libinstall] Error 1

If you scroll up, you will find following error which seems to be the root cause:

Compiling /usr/lib/python2.5/test/ ...
Sorry: UnicodeError: ("\\N escapes not supported (can't load unicodedata module)",)

Once you do python install with make -i install, that library ( which is not built until later stage of build process) gets installed. If you want, you can type make install once again (without ignoring errors) and it will complete without errors.

Once you have python installed, you would want to use the new version. I like to keep a backup of old files in case I have to use older version for any reason. Run following which creates symbolic links and makes backups of current files:

for binaries in `find /usr/local/python-2.5.1/bin/*` ; do
mv /usr/bin/`basename ${binaries}` /usr/bin/`basename ${binaries}`.bak
ln -s ${binaries} /usr/bin/`basename ${binaries}`

Now let us get phpsh and try it out:

tar zxf phpsh-latest.tgz
cd ../phpsh
chmod +x phpsh

At this point you should be at the shell: php>

Following is a snippet from README file which comes with phpsh. You should take a look since it has more details on how to use phpsh:

Type php commands and they will be evaluated each time you hit enter. Ex:
php> $msg = "hello world"
Put = at the beginning of a line as syntactic sugar for return. Ex:
php> = 2 + 2
If you end a line with a backlash (\), you can enter multi-line input.
For example,
php> print "like \
... this"
like this

There we go. Now you have a great interactive php shell prompt. Note that there is interactive mode built into php as well. I personally do not like it as much but you can try it out for yourself by typing:

php -a
Interactive mode enabled
echo "Hello world";
Hello world

DISCLAIMER: Please be smart and use code found on internet carefully. Make backups often. And yeah.. last but not least.. I am not responsible for any damage caused by this posting. Use at your own risk.

4 thoughts on “PHP: How do I install phpsh, interactive shell prompt for php under CentOS or Fedora?

  1. bcl

    Thanks for the post! It helped me solve a problem I was having with readline missing from python2.5


  2. MP

    Unfortunately it’s still not working for me
    Traceback (most recent call last):
    File “./phpsh”, line 20, in ?
    import readline
    ImportError: No module named readline

    however, when I do a python -V, it doesn’t respond with 2.5.1. It responds as it did before I began with “Python 2.4.3”. should I do a full uninstall of python first? Or is there an environment variable I need to toggle?

  3. Sunny Walia Post author

    MP: that is probably because you have two versions installed now. Try doing “which python” (without the quotes of course) and see if it is pointing to the one you installed or not. If it is not, you can either uninstall older version or rename old binary to something like python.version in case you needed it. Let me know if you need more help.

Leave a Reply

Your email address will not be published. Required fields are marked *