Jupyter Notebook 安装 PHP 内核

编辑于 2023-05-21 10:13:45 阅读 2703

普通安装

安装zmq扩展

官方的zmq已多年不维护了,并且在php7.4中报错,所以只能选择第三方的了

wget https://github.com/stijnvdb88/php-zmq/archive/refs/tags/v4.3.4.tar.gz

tar -xvzf php-zmq-4.3.4.tar.gz 
mv php-zmq-4.3.4 /usr/src/php/ext/php-zmq

#安装依赖
apt-get install -y libzmq3-dev

#安装扩展
docker-php-ext-install php-zmq

安装Jupyter-PHP-Installer

这个也是多年未更新了,已经不能正常使用了。据说是作者电脑丢了,代码都在里面😂

下面介绍三种方案

方案一:降级composer

有瑕疵,比如不支持箭头函数,php标签等…,建议直接看方案三

现在最新的composer是2.5.5,直接执行这个包会报如下错误

The package name jupyter-php-instance is invalid, it should be lowercase and have a vendor name, a forward slash, and a package name, matching: [a-z0-9_.-]+/[a-z0-9_.-]+  

下面把composer降到1.8.5

root@php-jupyter:~# composer self-update 1.8.5 
Upgrading to version 1.8.5 (stable channel).
   
Use composer self-update --rollback to return to version 2.5.5

接着,下载并安装

curl -sS -o /tmp/jupyter-php-installer.phar https://litipk.github.io/Jupyter-PHP-Installer/dist/jupyter-php-installer.phar

php /tmp/jupyter-php-installer.phar install -v


#查看可用的内核列表
jupyter kernelspec list

#查看服务列表
jupyter server list

方案二:自己修改

有瑕疵,比如不支持箭头函数,php标签等…,建议直接看方案三

我fork了一份代码,然后做了些修改

  • 升级box至4.3.8
  • 兼容composer2.x
  • 兼容php8.1

使用方法:下载我修改的jupyter-php-installer.phar,然后执行

php /tmp/jupyter-php-installer.phar install -v

项目地址:https://github.com/chudaozhe/Jupyter-PHP-Installer

方案三:

https://github.com/Rabrennie/jupyter-php-kernel

方便简单,详见Dockerfile

docker 部署

Dockerfile

FROM php:8.1.9-fpm

WORKDIR /notebooks

COPY ./php-zmq /usr/src/php/ext/php-zmq

RUN apt-get update \
  && apt-get install -y python3-pip zlib1g-dev libzmq3-dev libzip-dev \
  && pip3 install jupyterlab \
  && docker-php-ext-install zip php-zmq \
  && curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

ENV PATH="/root/.composer/vendor/bin:$PATH"

RUN composer global require rabrennie/jupyter-php-kernel \
  && jupyter-php-kernel --install \
  && jupyter-lab --generate-config

CMD ["jupyter", "lab", "--allow-root", "--ip=0.0.0.0", "--LabApp.token=''", "--notebook-dir=/notebooks"]

构建和运行

docker build -t php-jupyter:v1 .

docker run -d --name php-jupyter -p 8888:8888 php-jupyter:v1

详见

https://github.com/chudaozhe/docker-php-jupyter

总结

虽然Jupyter官方仓库推荐的PHP内核是Jupyter-PHP,但Jupyter-PHP项目年久失修,已经无法适应高版本的PHP,所以这里推荐Rabrennie/jupyter-php-kernel

至此,PHP内核就安装完成了。那代码安装在哪里了呢?看这里

root@php-jupyter:~# jupyter kernelspec list
Available kernels:
  php        /root/.local/share/jupyter/kernels/PHP
  python3    /usr/local/share/jupyter/kernels/python3

root@php-jupyter:~# cat /root/.local/share/jupyter/kernels/PHP/kernel.json 
{
  "argv": ["jupyter-php-kernel", "-r", "-c", "{connection_file}"],
  "display_name": "PHP",
  "language": "php"
}

jupyter-php-kernel的完整路径:/root/.composer/vendor/bin/jupyter-php-kernel

token在哪里?

root@php-jupyter:/notebooks# cat /root/.local/share/jupyter/runtime/jupyter_cookie_secret 
ZpyOvPvqZPqxmo42E7gb18itWFfKu7fvh5dMm2dW8m8=

#远程连接
http://127.0.0.1:8888/lab?token=ZpyOvPvqZPqxmo42E7gb18itWFfKu7fvh5dMm2dW8m8=

参考

https://litipk.github.io/Jupyter-PHP-Installer/

https://github.com/Rabrennie/jupyter-php-kernel

https://github.com/hoto17296/docker-jupyter-php/blob/master/Dockerfile

广而告之,我的新作品《语音助手》上架Google Play了,欢迎下载体验