I found the problem, that is the environment has been reset after each system
call in Ruby formula. So we need to set the correct LD_LIBRARY_PATH
as
if OS.linux?
ENV["LD_LIBRARY_PATH"] = "#{Formula["..."].opt_prefix}/lib:...:$LD_LIBRARY_PATH"
end
so that LD_LIBRARY_PATH
persists during the build processes.