GitLab依赖库rugged链接自定义库路径问题解决

今天,使用源码安装GitLab时出了些问题,困扰了不少时间,特此记录下。

首先,安装过程是根据官网安装操作进行的,稍微有些不一样的是,操作系统是CentOS7,以及多数第三方依赖库和程序非yum安装,为源码安装且为自定义路径(如:ruby、OpenSSL、CURL等)。

前面安装都不会有什么大问题,在安装Gems这一步时,由于我使用的OpenSSL为自定义路径,安装过程会在rugged这里停下且报错:

[root@gitlab gitlab]# sudo -u git -H bundle install --deployment --without development test postgres aws kerberos
...
Fetching rugged 0.27.4
Installing rugged 0.27.4 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /home/git/gitlab/vendor/bundle/ruby/2.5.0/gems/rugged-0.27.4/ext/rugged
/usr/local/ruby/bin/ruby -r ./siteconf20180902-27288-1tc8p83.rb extconf.rb
checking for gmake... yes
checking for cmake... yes
checking for pkg-config... yes
 -- cmake .. -DBUILD_CLAR=OFF -DTHREADSAFE=ON -DBUILD_SHARED_LIBS=OFF -DCMAKE_C_FLAGS=-fPIC -DCMAKE_BUILD_TYPE=RelWithDebInfo  
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
        --with-opt-dir
        --without-opt-dir
        --with-opt-include
        --without-opt-include=${opt-dir}/include
        --with-opt-lib
        --without-opt-lib=${opt-dir}/lib
        --with-make-prog
        --without-make-prog
        --srcdir=.
        --curdir
        --ruby=/usr/local/ruby/bin/$(RUBY_BASE_NAME)
        --with-sha1dc
        --without-sha1dc
        --use-system-libraries
extconf.rb:21:in `sys': ERROR: 'cmake .. -DBUILD_CLAR=OFF -DTHREADSAFE=ON -DBUILD_SHARED_LIBS=OFF -DCMAKE_C_FLAGS=-fPIC -DCMAKE_BUILD_TYPE=RelWithDebInfo  ' failed (RuntimeError)
        from extconf.rb:83:in `block (2 levels) in <main>'
        from extconf.rb:80:in `chdir'
        from extconf.rb:80:in `block in <main>'
        from extconf.rb:77:in `chdir'
        from extconf.rb:77:in `<main>'

To see why this extension failed to compile, please check the mkmf.log which can be found here:

  /home/git/gitlab/vendor/bundle/ruby/2.5.0/extensions/x86_64-linux/2.5.0/rugged-0.27.4/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /home/git/gitlab/vendor/bundle/ruby/2.5.0/gems/rugged-0.27.4 for inspection.
Results logged to /home/git/gitlab/vendor/bundle/ruby/2.5.0/extensions/x86_64-linux/2.5.0/rugged-0.27.4/gem_make.out

An error occurred while installing rugged (0.27.4), and Bundler cannot continue.
Make sure that `gem install rugged -v '0.27.4' --source 'https://rubygems.org/'` succeeds before bundling.

In Gemfile:
  gemnasium-gitlab-service was resolved to 0.2.6, which depends on
    rugged
[root@gitlab gitlab]#

根据提示查看日志,可以得到如下信息:

[root@gitlab gitlab]# tail /home/git/gitlab/vendor/bundle/ruby/2.5.0/extensions/x86_64-linux/2.5.0/rugged-0.27.4/mkmf.log
-- Checking for module 'libcurl'
--   No package 'libcurl' found
-- Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the system variable OPENSSL_ROOT_DIR (missing: OPENSSL_CRYPTO_LIBRARY OPENSSL_INCLUDE_DIR) 
CMake Error at src/CMakeLists.txt:176 (MESSAGE):
  Asked for OpenSSL TLS backend, but it wasn't found


-- Configuring incomplete, errors occurred!
See also "/home/git/gitlab/vendor/bundle/ruby/2.5.0/gems/rugged-0.27.4/vendor/libgit2/build/CMakeFiles/CMakeOutput.log".
See also "/home/git/gitlab/vendor/bundle/ruby/2.5.0/gems/rugged-0.27.4/vendor/libgit2/build/CMakeFiles/CMakeError.log".
[root@gitlab gitlab]#

主要原因是由于找不到我自定义位置安装的OpenSSL库,且找不到变量OPENSSL_ROOT_DIR值导致!

起初,按其提示的那样,加上OPENSSL_ROOT_DIR,再进行安装:

[root@gitlab gitlab]# sudo -u git -H OPENSSL_ROOT_DIR=/usr/local/openssl bundle install --deployment --without development test postgres aws kerberos
...
Bundle complete! 228 Gemfile dependencies, 322 gems now installed.
Gems in the groups development, test, postgres, aws and kerberos were not installed.
Bundled gems are installed into `./vendor/bundle`
[root@gitlab gitlab]#

看样子的确安装上了哦!

后面启动服务时,也能启动成功:

[root@gitlab gitlab]# systemctl start gitlab-unicorn
[root@gitlab gitlab]# systemctl status gitlab-unicorn
● gitlab-unicorn.service - GitLab Unicorn Server
   Loaded: loaded (/usr/lib/systemd/system/gitlab-unicorn.service; enabled; vendor preset: disabled)
   Active: active (running) since Sun 2018-09-02 01:58:46 CST; 56s ago
 Main PID: 29802 (ruby)
   CGroup: /system.slice/gitlab-unicorn.service
           ├─29802 unicorn_rails master -D -c /home/git/gitlab/config/unicorn.rb -E production
           ├─29842 unicorn_rails worker[0] -D -c /home/git/gitlab/config/unicorn.rb -E production
           ├─29846 unicorn_rails worker[1] -D -c /home/git/gitlab/config/unicorn.rb -E production
           ├─29856 unicorn_rails worker[2] -D -c /home/git/gitlab/config/unicorn.rb -E production
           └─29859 unicorn_rails worker[3] -D -c /home/git/gitlab/config/unicorn.rb -E production

Sep 02 01:58:46 gitlab systemd[1]: Started GitLab Unicorn Server.
Sep 02 01:58:46 gitlab systemd[1]: Starting GitLab Unicorn Server...
[root@gitlab gitlab]#

真的是这样吗,A MA YI !!!

咱们来看看日志怎么说:

[root@gitlab gitlab]# tail -150 log/unicorn.stderr.log
...
/home/git/gitlab/vendor/bundle/ruby/2.5.0/gems/rugged-0.27.4/lib/rugged.rb:10:in `require': /home/git/gitlab/vendor/bundle/ruby/2.5.0/gems/rugged-0.27.4/lib/rugged/rugged.so: undefined symbol: curl_global_cleanup - /home/git/gitlab/vendor/bundle/ruby/2.5.0/gems/rugged-0.27.4/lib/rugged/rugged.so (LoadError)
        from /home/git/gitlab/vendor/bundle/ruby/2.5.0/gems/rugged-0.27.4/lib/rugged.rb:10:in `rescue in <top (required)>'
        from /home/git/gitlab/vendor/bundle/ruby/2.5.0/gems/rugged-0.27.4/lib/rugged.rb:6:in `<top (required)>'
        from /usr/local/ruby/lib/ruby/gems/2.5.0/gems/bundler-1.16.4/lib/bundler/runtime.rb:81:in `require'
        from /usr/local/ruby/lib/ruby/gems/2.5.0/gems/bundler-1.16.4/lib/bundler/runtime.rb:81:in `block (2 levels) in require'
        from /usr/local/ruby/lib/ruby/gems/2.5.0/gems/bundler-1.16.4/lib/bundler/runtime.rb:76:in `each'
        from /usr/local/ruby/lib/ruby/gems/2.5.0/gems/bundler-1.16.4/lib/bundler/runtime.rb:76:in `block in require'
        from /usr/local/ruby/lib/ruby/gems/2.5.0/gems/bundler-1.16.4/lib/bundler/runtime.rb:65:in `each'
        from /usr/local/ruby/lib/ruby/gems/2.5.0/gems/bundler-1.16.4/lib/bundler/runtime.rb:65:in `require'
        from /usr/local/ruby/lib/ruby/gems/2.5.0/gems/bundler-1.16.4/lib/bundler.rb:114:in `require'
        from /home/git/gitlab/config/application.rb:5:in `<top (required)>'
        from /home/git/gitlab/config/environment.rb:7:in `require'
        from /home/git/gitlab/config/environment.rb:7:in `<top (required)>'
        from config.ru:19:in `require'
        from config.ru:19:in `block in <main>'
        from /home/git/gitlab/vendor/bundle/ruby/2.5.0/gems/rack-1.6.10/lib/rack/builder.rb:55:in `instance_eval'
        from /home/git/gitlab/vendor/bundle/ruby/2.5.0/gems/rack-1.6.10/lib/rack/builder.rb:55:in `initialize'
        from config.ru:1:in `new'
        from config.ru:1:in `<main>'
        from /home/git/gitlab/vendor/bundle/ruby/2.5.0/gems/unicorn-5.1.0/lib/unicorn.rb:56:in `eval'
        from /home/git/gitlab/vendor/bundle/ruby/2.5.0/gems/unicorn-5.1.0/lib/unicorn.rb:56:in `block in builder'
        from /home/git/gitlab/vendor/bundle/ruby/2.5.0/gems/unicorn-5.1.0/bin/unicorn_rails:139:in `block in rails_builder'
        from /home/git/gitlab/vendor/bundle/ruby/2.5.0/gems/unicorn-5.1.0/lib/unicorn/http_server.rb:752:in `build_app!'
        from /home/git/gitlab/vendor/bundle/ruby/2.5.0/gems/unicorn-5.1.0/lib/unicorn/http_server.rb:129:in `start'
        from /home/git/gitlab/vendor/bundle/ruby/2.5.0/gems/unicorn-5.1.0/bin/unicorn_rails:209:in `<top (required)>'
        from /home/git/gitlab/vendor/bundle/ruby/2.5.0/bin/unicorn_rails:23:in `load'
        from /home/git/gitlab/vendor/bundle/ruby/2.5.0/bin/unicorn_rails:23:in `<main>'
...

看样子是CURL库没有连接上,真的只有CURL库吗?查看文件的资源连接:

[root@gitlab gitlab]# ldd /home/git/gitlab/vendor/bundle/ruby/2.5.0/gems/rugged-0.27.4/lib/rugged/rugged.so
        linux-vdso.so.1 =>  (0x00007ffe015c4000)
        libruby.so.2.5 => /usr/local/ruby/lib/libruby.so.2.5 (0x00007ff8f3959000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x00007ff8f3732000)
        libdl.so.2 => /lib64/libdl.so.2 (0x00007ff8f352e000)
        libcrypt.so.1 => /lib64/libcrypt.so.1 (0x00007ff8f32f7000)
        libm.so.6 => /lib64/libm.so.6 (0x00007ff8f2ff4000)
        libc.so.6 => /lib64/libc.so.6 (0x00007ff8f2c33000)
        /lib64/ld-linux-x86-64.so.2 (0x00007ff8f41b7000)
        libfreebl3.so => /lib64/libfreebl3.so (0x00007ff8f2a30000)
[root@gitlab gitlab]#

不止CURL库没有连接上,自定义的OpenSSL甚至zlib库都没有连接上!

这里简单介绍下,这些资源库的安装位置:

[root@gitlab gitlab]# ls /usr/local/openssl/lib/
engines  libcrypto.a  libcrypto.so  libcrypto.so.1.0.0  libssl.a  libssl.so  libssl.so.1.0.0  pkgconfig
[root@gitlab gitlab]# ls /usr/local/curl/lib/
libcurl.a  libcurl.la  libcurl.so  libcurl.so.4  libcurl.so.4.5.0  pkgconfig
[root@gitlab gitlab]# ls /usr/local/lib/libz.so
/usr/local/lib/libz.so
[root@gitlab gitlab]# cat /etc/ld.so.conf.d/complie.conf 
# User Defined.
/usr/local/lib
/usr/local/openssl/lib
/usr/local/curl/lib/
/usr/local/python3/lib/
[root@gitlab gitlab]#

程序及库都为自定义的路径,并且为系统加上了库路径搜寻位置,但这里设置的搜索位置,在程序编译时,并不会生效!仍需特别指定,一般是程序编译时的选项,或者编译时添加CPPFLAGS=-I<include_dir> , LDFALGS=-L<lib_dir> 以及PKG_CONFIG_PATH=<pkgconfig_dir>这样的变量来指定系统搜索路径以外的路径。

但这里,我们是安装ruby的Gem程序rugged,它本身并未提供这样的编译选项支持。

如果单纯解决连接问题的话,将这些库以及它们的include文件都放入编译时的搜寻路径中(如:/usr/include , /usr/lib)即可解决,但我并想这么做!

于是需要添加变量,来使其能够找到这些路径,经过一番尝试,终于解决:

[root@gitlab gitlab]# rm -f vendor/bundle/ruby/2.5.0/specifications/rugged-0.27.4.gemspec
[root@gitlab gitlab]# sudo -u git -H PKG_CONFIG_PATH=/usr/local/lib/pkgconfig/:/usr/local/curl/lib/pkgconfig/:/usr/local/openssl/lib/pkgconfig/ bundle install --deployment --without development test postgres aws kerberos
...
Bundle complete! 228 Gemfile dependencies, 322 gems now installed.
Gems in the groups development, test, postgres, aws and kerberos were not installed.
Bundled gems are installed into `./vendor/bundle`
[root@gitlab gitlab]# ldd /home/git/gitlab/vendor/bundle/ruby/2.5.0/gems/rugged-0.27.4/lib/rugged/rugged.so                                                                                          linux-vdso.so.1 =>  (0x00007fffa9707000)
        librt.so.1 => /lib64/librt.so.1 (0x00007f202df46000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f202dd29000)
        libcurl.so.4 => /usr/local/curl/lib/libcurl.so.4 (0x00007f202daba000)
        libssl.so.1.0.0 => /usr/local/openssl/lib/libssl.so.1.0.0 (0x00007f202d848000)
        libcrypto.so.1.0.0 => /usr/local/openssl/lib/libcrypto.so.1.0.0 (0x00007f202d3f4000)
        libdl.so.2 => /lib64/libdl.so.2 (0x00007f202d1f0000)
        libz.so.1 => /usr/local/lib/libz.so.1 (0x00007f202cfd5000)
        libruby.so.2.5 => /usr/local/ruby/lib/libruby.so.2.5 (0x00007f202ca9c000)
        libcrypt.so.1 => /lib64/libcrypt.so.1 (0x00007f202c865000)
        libm.so.6 => /lib64/libm.so.6 (0x00007f202c563000)
        libc.so.6 => /lib64/libc.so.6 (0x00007f202c1a1000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f202e47f000)
        libfreebl3.so => /lib64/libfreebl3.so (0x00007f202bf9e000)
[root@gitlab gitlab]#

第一步是删除ruby gems程序的安装状态文件,如不删除,已安装的程序不会重新编译安装。

通过指定PKG_CONFIG_PATH,来添加额外的pkgconfig文件的搜索路径,让编译器能够找到所需库的连接方式和它们具体的安装路径。

最后安装成功,rugged.so动态库也正确地连接上了所需的资源库,之后日志里也没有类似的报错,大功告成。

发表评论

error: Content is protected !!