Ruby require 'tk' yields LoadError: no such file to load -- tk

Brooks Childers picture Brooks Childers · Jan 2, 2014 · Viewed 8.3k times · Source

I'm not able to get ruby to require 'tk' successfully. I'm using rvm, ruby 2.0.0, ActiveTcl-8.6, and Ubuntu 12.04 LTS. I have run wish provided with ActiveTcl and it seems to be working.

I've looked on the RVM site http://rvm.io/integration/tk and several StackOverflow questions like this one RVM Ruby with TK installation (OSX).

I have tried rvm reinstall 2.0.0 --enable-shared --enable-pthread --with-tk --with-tcl several times on different versions of ruby with no success.

Any thoughts?

When I run irb and do require'tk' I get the following:

LoadError: cannot load such file -- tk
from /home/brooks/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /home/brooks/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from (irb):2
from /home/brooks/.rvm/rubies/ruby-2.0.0-p353/bin/irb:12:in `<main>'

When this works I think you should get true returned.

I haven't tried anything else mainly because I can't figure out what else to do. I've been looking into how require works and checking the load path with ruby -e '$:' I get

[brooks@ubuntu:~/sites/depot]$ruby -e 'puts $:'
/home/brooks/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/site_ruby/2.0.0
/home/brooks/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/site_ruby/2.0.0/x86_64-linux
/home/brooks/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/site_ruby
/home/brooks/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/vendor_ruby/2.0.0
/home/brooks/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/vendor_ruby/2.0.0/x86_64-linux
/home/brooks/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/vendor_ruby
/home/brooks/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0
/home/brooks/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/x86_64-linux

This looks like what you would expect, I think. Sorry for the formatting, I'm new to the editor.

Following up on the idea that it's a path problem, I found tk.rb in my files and tried the following in irb:

2.0.0-p353 :003 > require '/home/brooks/.rvm/src/ruby-2.0.0-p353/ext/tk/lib/tk'   
LoadError: cannot load such file -- tcltklib
from /home/brooks/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /home/brooks/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /home/brooks/.rvm/src/ruby-2.0.0-p353/ext/tk/lib/tk.rb:6:in `<top (required)>'
from /home/brooks/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /home/brooks/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from (irb):3
from /home/brooks/.rvm/rubies/ruby-2.0.0-p353/bin/irb:12:in `<main>'

So now it's require 'tcltklib' in the tk.rb file that's causing the problem. This seems to confirm that there is some issue with $PATH or $load_path. But a search for the file tcltklib.rb turns up nothing. There is a tcltklib.c file.


I tried a few more things. After a more careful reading of the output of the ruby installs via rvm, there was a warning about the X11 lib not being installed and that Tk wouldn't be active after the ruby build. I did sudo apt-get install libx11-dev and then rvm reinstall 2.0.0 --enable-shared --enable-pthread --with-tk --with-tcl again. This time in irb require 'tk' caused a core dump. I tried it another time and it worked. I then ran a short ruby script that also uses require 'tk'. It also core dumped, but worked after a few tries. Now it's hit or miss for both, sometimes works, sometimes it core dumps.

Answer

Kalyan Kumar picture Kalyan Kumar · Mar 11, 2015

Not onty sudo apt-get install tk is enough but you also need the below library files to be installed so that your problem will be solved and you wont see this error never again.

$ sudo apt-get install libtcltk-ruby

thanks KK