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

Ruby源碼安裝 cannot load such file -- zlib (LoadError) 和 cannot load such file -- openssl (LoadError) 及 gem No rule to make target `/include/ruby.h', needed by`zlib.o'. Stop. 解決辦法linux

發生問題時個人前置條件

  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. gem版本: 3.0.6
  5. Ruby安裝方式: 源碼安裝且源碼未刪除
  6. 安裝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) find /usr/local/lib -name libz.a
  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 byzlib.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 byzlib.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 byzlib.o'. Stop. 解決辦法

緣由

  1. 多是缺乏$(top_srcdir)/include/ruby.h變量對應值

解決

  1. 若是報錯 make: *** No rule to make target /include/ruby.h', needed byzlib.o'. Stop.
  2. 根據日誌得知 zlib.o: $(top_srcdir)/include/ruby.h 去查看源碼的確不存在變量值的話
    1. 在Makefile文檔第一行,設置變量top_srcdir的路徑
    2. (我採用)用絕對/相對路徑替換$(top_srcdir)
      • 建議先備份Makefile
        • cp Makefile Makefile.bak
      • vim Makefile
      • : %s/$(top_srcdir)/..\/../g
      • : wq
相關文章
相關標籤/搜索