无法在 Ubuntu 上加载 Ruby 的 tk

问题描述

我对 Ruby 真的很陌生,我需要将此库用于特定的 GUI 项目。我正在使用:

  • Ubuntu 18.04.5 LTS;
  • ruby 2.5.1p57(2018-03-29 修订版 63029)[x86_64-linux-gnu]

我的问题与几个月前描述的一位 this 人相似。我已经尝试了基于 this 博文的 SO 页面上发布的解决方案。

运行后:

sudo apt-get install tcl8.5-dev tk8.5-dev

还有:

sudo gem install tk

我收到以下错误消息:

    Building native extensions. This Could take a while...
ERROR:  Error installing tk:
    ERROR: Failed to build gem native extension.

    current directory: /var/lib/gems/2.5.0/gems/tk-0.4.0/ext/tk
/usr/bin/ruby2.5 -r ./siteconf20210424-11015-1mre8d0.rb extconf.rb
mkmf.rb can't find header files for ruby at /usr/lib/ruby/include/ruby.h

extconf Failed,exit code 1

Gem files will remain installed in /var/lib/gems/2.5.0/gems/tk-0.4.0 for inspection.
Results logged to /var/lib/gems/2.5.0/extensions/x86_64-linux/2.5.0/tk-0.4.0/gem_make.out

错误消息与 blog post 中描述的错误消息不同,后者是:

$ gem install tk
Building native extensions. This Could take a while...
ERROR:  Error installing tk:
  ERROR: Failed to build gem native extension.
# ...
Search tcl.h
checking for tcl.h... no
Search tk.h
checking for tk.h... no
Search Tcl library............*** extconf.rb Failed ***
# ...
Warning:: cannot find Tcl library. tcltklib will not be compiled (tcltklib is disabled on your Ruby. That is,Ruby/Tk will not work). Please check configure options.

Can't find proper Tcl/Tk libraries. So,can't make tcltklib.so which is required by Ruby/Tk.
If you have Tcl/Tk libraries on your environment,you may be able to use them with configure options (see ext/tk/README.tcltklib).

The gem provides many switches for specifying the configuration parameters,however,the related parameters (--with-tk-lib and --with-tcl-lib) don’t yield the desired effect (actually,any effect at all).

The workaround,originally found in a Ruby forum is to symlink the libraries to the paths where the extension expects 

然后,运行后:

sudo ln -s /usr/lib/x86_64-linux-gnu/tcl8.5/tclConfig.sh /usr/lib/
sudo ln -s /usr/lib/x86_64-linux-gnu/tk8.5/tkConfig.sh /usr/lib/
sudo ln -s /usr/lib/x86_64-linux-gnu/libtcl8.5.so.0 /usr/lib/
sudo ln -s /usr/lib/x86_64-linux-gnu/libtk8.5.so.0 /usr/lib/

我明白了(可能是因为我之前尝试过相同的步骤):

ln: Failed to create symbolic link '/usr/lib/libtk8.5.so.0': File exists

最后,运行最后一步不起作用

sudo gem install tk

并且终端返回如下错误信息:

Building native extensions. This Could take a while...
ERROR:  Error installing tk:
    ERROR: Failed to build gem native extension.

    current directory: /var/lib/gems/2.5.0/gems/tk-0.4.0/ext/tk
/usr/bin/ruby2.5 -r ./siteconf20210424-21301-8u83wl.rb extconf.rb
mkmf.rb can't find header files for ruby at /usr/lib/ruby/include/ruby.h

extconf Failed,exit code 1

Gem files will remain installed in /var/lib/gems/2.5.0/gems/tk-0.4.0 for inspection.

我该怎么办?

提前致谢。

解决方法

blog post解决了问题。

运行后:

sudo apt install tk-dev

并安装 Ruby/Tk gem:

gem install tk -- --with-tcltkversion=8.6 \
--with-tcl-lib=/usr/lib/x86_64-linux-gnu \
--with-tk-lib=/usr/lib/x86_64-linux-gnu \
--with-tcl-include=/usr/include/tcl8.6 \
--with-tk-include=/usr/include/tcl8.6 \
--enable-pthread

一切都解决了。