Ruby cannot load such file -- zlib和openssl(LoadError)及gem No rule to make target `/include/ruby.h'解决

夙愿已清 提交于 2019-12-11 10:17:24

【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>

Ruby源码安装cannot load such file -- zlib和cannot load such file -- openssl及gem No rule to make target `/include/ruby.h'解决办法

发生问题时我的前置条件

  1. linux版本: Linux ossjh2 3.10.0-862.el7.x86_64 #1 SMP Fri Apr 20 16:44:24 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
  2. gcc版本: gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
  3. Ruby版本:ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-linux]
  4. Ruby安装方式: 源码安装且源码未删除
  5. 安装Ruby及gems目的: 使用ruby搭建redis集群

cannot load such file -- zlib 问题解决

原因

  1. 缺少zlib函式库
  2. 缺少ruby-zlib

解决

  1. (未安装zlib时)下载安装zlib
    • 我是使用源码安装,由是默认安装到/usr/local/lib,我选择使用root用户操作
    • 安装版本zlib-1.2.11
      1. 下载软件包 wget http://www.zlib.net/zlib-1.2.11.tar.gz
      2. 解压缩软件包 tar -zxvf zlib-1.2.11.tar.gz
      3. 进入zlib源码目录 cd zlib-1.2.11/
      4. 配置 ./configure
      5. make make
      6. 检查 make check
      7. 安装 make install
      8. 查看是否成功(目录中存在libz.a) cd /usr/local/lib
  2. 安装 ruby-zlib
    • Ruby源码提供了该源码,直接找到对应目录安装
      1. cd /root/ruby-2.6.5/ext/zlib
      2. ruby ./extconf.rb
        • 如果报错 checking for zlib.h... no 或 checking for deflateReset() in -lzlib... no
        • 则 ruby ./extconf.rb --with-zlib-dir =/usr/local/zlib
      3. make
        • 如果报错 make: *** No rule to make target `/include/ruby.h', needed by `zlib.o'. Stop.
        • 根据日志得知 zlib.o: $(top_srcdir)/include/ruby.h 去查看源码的确不存在的话
          1. 在Makefile文档第一行,设置变量top_srcdir的路径
          2. (我采用)用绝对/相对路径替换$(top_srcdir)
            • 建议先备份Makefile
            • vim Makefile
            • : %s/$(top_srcdir)/../../g
            • :wq
      4. 如果上一步make报错,在修改后再次make
      5. make install

cannot load such file -- openssl 问题解决

##原因

  1. 未安装openssl-devel
  2. 缺少ruby-openssl

##解决

  1. (未安装openssl时)下载安装openssl
    • 我是使用源码安装,默认安装到/usr/local/ssl,且没有链接命令
    • 安装版本 openssl-1.0.2t
      1. 下载软件包 wget https://www.openssl.org/source/openssl-1.0.2t.tar.gz
      2. 解压缩软件包 tar -zxvf openssl-1.0.2t.tar.gz
      3. 进入目录 cd openssl-1.0.2t
      4. ./config shared zlib
      5. make depend
      6. make
      7. make install
      8. 验证是否安装成功(显示版本信息) /usr/local/ssl/bin/openssl version -a
      9. 如有需要则可以手工链接openssl命令
        • 先确认/usr/bin/openssl和/usr/include/openssl不存在,若存在请备份或删除原来的openssl
          • (可选)备份方式
            • mv /usr/bin/openssl /usr/bin/openssl.bak
            • mv /usr/include/openssl /usr/include/openssl.bak
        • 链接刚刚安装的openssl
          • ln -s /usr/local/ssl/bin/openssl /usr/bin/openssl
          • ln -s /usr/local/ssl/include/openssl /usr/include/openssl
        • (可选)写入ld.so.conf记录动态库的路径
          • echo “/usr/local/ssl/lib” >> /etc/ld.so.conf
          • ldconfig -v
        • 验证链接是否成功 openssl version -a
  2. 安装ruby-openssl
    • Ruby源码提供了该源码,直接找到对应目录安装
      1. cd /root/ruby-2.6.5/ext/openssl
      2. ruby ./extconf.rb
        • (未链接刚安装的openssl可能)报错 checking for openssl.h... no 或 checking for deflateReset() in -openssl... no
        • 则 ruby ./extconf.rb --with-openssl-dir=/usr/local/ssl
      3. make
        • 如果报错 make: *** No rule to make target `/include/ruby.h', needed by `zlib.o'. Stop.
        • 根据日志得知 zlib.o: $(top_srcdir)/include/ruby.h 去查看源码的确不存在的话,处理方式同安装ruby-zlib
          1. 在Makefile文档第一行,设置变量top_srcdir的路径
          2. (我采用)用绝对/相对路径替换$(top_srcdir)
            • 建议先备份Makefile
            • vim Makefile
            • : %s/$(top_srcdir)/../../g
            • :wq
      4. 如果上一步make报错,在修改后再次make
      5. make install

No rule to make target `/include/ruby.h', needed by `zlib.o'. Stop. 解决办法

原因

1. 可能是缺少/include/ruby.h

解决

1. 如果报错 make: \*\*\* No rule to make target \`/include/ruby.h', needed by \`zlib.o'.  Stop.
2. 根据日志得知 zlib.o: $(top_srcdir)/include/ruby.h 去查看源码的确不存在的话
	1. 在Makefile文档第一行,设置变量top_srcdir的路径
	2. (我采用)用绝对/相对路径替换$(top_srcdir)
		- 建议先备份Makefile
			- mv Makefile Makefile.bak
		- vim Makefile
		- : %s/$(top_srcdir)/..\/../g
		- :wq
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!