How to install import script gem dependencies?

Some import scripts are using external dependencies, and those seem to work just fine.

When trying to add my own Gemfiles, I’m always presented with

/usr/local/lib/ruby/gems/2.3.0/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:274:in `require': cannot load such file -- redcarpet (LoadError)
        from /usr/local/lib/ruby/gems/2.3.0/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:274:in `block in require'
        from /usr/local/lib/ruby/gems/2.3.0/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:240:in `load_dependency'
        from /usr/local/lib/ruby/gems/2.3.0/gems/activesupport-4.2.7.1/lib/active_support/dependencies.rb:274:in `require'
        from emed.rb:4:in `<main>'

I’m installing the gem using the root user inside the docker image since the discourse user isn’t privileged enough, and the gem’s can be run from the command line from the discourse user just fine. Just require redcarpet doesn’t work.

I recommend adding your dependency to the Gemfile and using bundler to install the gems. Run this inside the Docker container:

cd /var/www/discourse
echo "gem 'my_gem'" >> Gemfile
su discourse -c 'bundle install --no-deployment --without test --without development --path vendor/bundle'

And you need to run your import script as user discourse as well:

su discourse -c 'bundle exec ruby script/import_scripts/my_import_script.rb
5 Likes

Thanks for the reply. Installing the gem using bundler worked the way you suggested, however running the import script with bundler didn’t work. Used su discourse -c 'ruby script/import_scripts/my_import_script.rb' instead, that worked