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:

wget ftp://ftp.cwru.edu/pub/bash/readline-5.2.tar.gz
tar zxf readline-5.2.tar.gz
cd readline-5.2
./configure
make install

Now let us install python with readline support:

wget http://www.python.org/ftp/python/2.5.1/Python-2.5.1.tgz
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/zipfile.py ...
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/test_multibytecodec.py ...
Sorry: UnicodeError: ("\\N escapes not supported (can't load unicodedata module)",)

Once you do python install with make -i install, that library (unicodedata.so 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}`
done

Now let us get phpsh and try it out:

wget http://www.phpsh.org/phpsh-latest.tgz
tar zxf phpsh-latest.tgz
cd ../phpsh
chmod +x phpsh
./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
php>

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.


3 Responses to “ PHP: How do I install phpsh, interactive shell prompt for php under CentOS or Fedora? ”

  1. bcl
    January 8th, 2008 | 9:05 am

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

    bcl

  2. MP
    June 27th, 2008 | 11:14 am

    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. June 27th, 2008 | 11:37 am

    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

*
To prove that you're not a bot, enter this code
Anti-Spam Image