Reputation: 1
Ok, so this is my first ever question. I'm bracing for feeling dumb, but here goes.
I'm currently learning Ruby before I become familiar with Ruby on Rails. I've been following an online course, and at this juncture we've just learned how to use rack to handle some of the logic for a server, and running a config.ru file to start the server.
But, every time I try to start the server using rackup, I get a LoadError
like this:
1: from /usr/local/bin/rackup:23:in `<main>'
/usr/local/bin/rackup:23:in `load': cannot load such file -- /usr/lib/ruby/gems/2.5.0/gems/rack-2.0.7/bin/rackup (LoadError)```
Now, I've been slamming my head against every possible angle I can think of. I understand this error to be saying that path - from which the rackup command is trying to load the rack code - doesn't exist. I've tried making multiple different .ru files to test if its a problem with the shebang, a problem with the content of the .ru file, and so on. But that doesn't seem to be the case. I can't even run the example lobster.ru file that comes with the rack gem. I get the same error.
I also tried reinstalling the rack gem, changing the default version of it. Neither helped. I considered using different versions of Ruby, but that didn't seem like it was relevant. The problem is clearly that rackup is looking in the wrong place.
But, if I use require 'rack'
on a simple ruby server, there aren't any problems. Rack works. So all of this told me that it must be something to do with the rackup command itself.
Indeed, when I go to /usr/lib/ruby/gems/2.5.0/gems
there is no rack-2.0.7
directory. Instead, that directory is in var/lib/gems/2.5.0/gems
I've noticed that var/lib/gems/2.5.0
is in the GEM PATHS of the RubyGems Environment. But /usr/lib/ruby/gems/2.5.0'
is not.
So my ideas on solving the issue were:
Change the way that rackup loads the rack gem. I looked into this and it would mean changing what looks like seriously proper code in the rubygems or even rackup files. I will break it if I tamper with it because I have no clue what I'm doing. This is not a sensible option.
Add the usr/lib...
to the GEM PATH, so that when rackup and rubygems looks for the rack gem it can find it. Having searched a bit about this possibility, it seems like manually adding things to the GEM PATH is not recommended. I tried it anyway...but it didn't work (FYI, I added to the GEM PATH by adding export GEM_PATH=...
to the .bashrc
EDIT: Having slept on it, I can see why the second option above doesn't work either. Simply adding the missing directory to the GEM PATH or to the $LOAD_PATH won't help because rackup is looking in a specific place for rack but can't find it. If I look at the rackup code, it looks like this:
if Gem.respond_to?(:activate_bin_path)
load Gem.activate_bin_path('rack', 'rackup', version)
else
gem "rack", version
load Gem.bin_path("rack", "rackup", version)
end
The segments of code doing the looking are directed by :activate_bin_path
and bin_path
, which are both defined in the rubygems
code.
So if I understand this correctly, its like rackup is getting the wrong directions to a place that doesn't exist. Like it's turning right when it should go left. My options are either to somehow change its directions to tell it 'go left' or to build the thing its looking for 'on the right'.
Making sure it 'goes left' is the better solution but I can't figure out how to do that. So I built what its looking for 'on the right': I copied the rack gem to the /usr/lib/ruby/gems/2.5.0/gems/
directory that rackup is looking in. It has solved the problem of rackup not finding what it is looking for - rackup now works and the server works. BUT this solution is going to haunt me in the future, e.g. if rack is updated... It's a barbarian solution, and I'd prefer an elegant one.
So if anyone has insight into how to make sure rackup gets the proper directions, please let me know! I think it must have something to do with making sure the rubygems bin_path
and :activate_bin_path
correspond with where the gems are actually installed. At the moment the fundamental discrepancy seems to be that the gems live in var
but its looking in usr
.
Thanks in advance!
Upvotes: 0
Views: 913
Reputation: 2829
Try this file:
config.ru
app = ->(env){
status = 200
headers = {"content-type" => "text/html"}
body = ["<Html><Body><H1>Hello, World!</H1></Body></Html>"]
[status, headers, body]
}
run app
rackup config.ru
Upvotes: 0