Ubuntu 20.04服务器安装PHP rar扩展报错求助
Ubuntu 20.04下PHP 8.0安装RAR扩展编译失败的解决办法
问题详情
在Ubuntu 20.04服务器上执行sudo pecl -v install rar安装PHP RAR插件时,出现编译错误,相关信息如下:
系统内存信息
root@root:/# free -m total used free shared buff/cache available Mem: 2000 870 113 36 1016 821 Swap: 0 0 0
当前PHP版本
root@root:/# php -v PHP 8.0.22 (cli) (built: Aug 15 2022 09:40:14) ( NTS ) Copyright (c) The PHP Group Zend Engine v4.0.22, Copyright (c) Zend Technologies with Zend OPcache v8.0.22, Copyright (c), by Zend Technologies
错误日志
/tmp/pear/temp/rar/rar_stream.c: In function ‘_rar_get_archive_and_fragment’: /tmp/pear/temp/rar/rar_stream.c:787:66: warning: passing argument 1 of ‘zend_resolve_path’ from incompatible pointer type [-Wincompatible-pointer-types] 787 | zend_string *arc_str = zend_resolve_path(tmp_archive, tmp_arch_len); | ^~~~~~~~~~~ | | | char * /tmp/pear/temp/rar/rar_stream.c:787:66: note: expected ‘zend_string *’ {aka ‘struct _zend_string *’} but argument is of type ‘char *’ /tmp/pear/temp/rar/rar_stream.c:787:48: error: too many arguments to function ‘zend_resolve_path’ 787 | zend_string *arc_str = zend_resolve_path(tmp_archive, tmp_arch_len); | ^~~~~~~~~~~~~~~~~ make: *** [Makefile:215: rar_stream.lo] Error 1 rolling back 454 file operations ERROR: `make' failed
解决步骤
该错误是因为PECL仓库中的RAR扩展版本未适配PHP 8.x的API变更(zend_resolve_path函数参数和调用方式改变),直接安装官方源码仓库的修复版本即可:
- 安装编译依赖
sudo apt update && sudo apt install -y libzip-dev unrar-free php8.0-dev build-essential git
- 克隆适配PHP 8的RAR扩展源码
git clone https://github.com/php/pecl-php-rar.git cd pecl-php-rar
- 编译并安装扩展
phpize ./configure make sudo make install
- 启用扩展
编辑对应PHP版本的php.ini文件(CLI版路径通常为/etc/php/8.0/cli/php.ini,Web服务版如Apache为/etc/php/8.0/apache2/php.ini),添加以下内容:
extension=rar.so
- 验证安装结果
执行以下命令,若输出rar则说明安装成功:
php -m | grep rar
内容的提问来源于stack exchange,提问作者Metehan Kıran
相关产品推荐
相关产品推荐

