For a good few years, I have been using
awesome_print for improved readability in rails console. But recently, I came across an issue where I had to read Audits from the console and my usual savior
awesome_print wasn't bundled.
So, I stopped doing what I was meant to and spent next few hours figuring out, how can I install a gem in any environment without making code changes?
Apparently this is possible by updating the
$LOAD_PATH variable and then requiring the gem manually.
In Ruby, an identifier starting with a
$ symbol is a global variable.
$LOAD_PATH is an array of absolute paths i.e it stores the exact location of all the dependencies in the project.
So, let's assume we want to install
awesome_print in a production Rails Console. We can do so by following these steps:
- SSH into the production server.
- Run this to install the gem,
bundle install awesome_print
- Run this to figure out the path where gem is installed,
gem which awesome_print
which should give something like,
- Now, lets start our Rails console
- We can add the path of this gem to the
$LOAD_PATHglobal variable using:
$LOAD_PATH << /home/user/.asdf/installs/ruby/2.7.0/lib/ruby/gems/2.7.0/gems/awesome_print-1.9.2/lib
- Let's try and require our freshly installed gem using:
Follow the above steps and you should be able to install and use any gem you want on your production server without making any code changes.