Search code examples
rubygraphicsrubygemstk-toolkit

Failing to load Ruby's tk on Ubuntu


I am really new to Ruby and I need to use this library for a specific GUI project. I am using:

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

My problem is similar to the one this person described a few months ago. I have tried the solution posted on that SO page which is based on this blog post.

After running:

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

And:

sudo gem install tk

I get the following error message:

    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

This error message is different from the one described in the blog post which is:

$ 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 

Then, after running:

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/

I get this (probably because I tried the same steps before):

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

Finally, running the last step does not work:

sudo gem install tk

And the terminal returns the following error message:

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.

What should I do?

Thanks in advance.


Solution

  • This blog post solved the problem.

    After running:

    sudo apt install tk-dev
    

    And installing 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
    

    Everything was solved.