How to install import script gem dependencies?

AI-generated summary

Installing Import Script Gem Dependencies

tobiasmuehl encountered an issue when trying to add their own Gemfiles to import scripts, resulting in a LoadError. They tried installing the gem as the root user inside the Docker image, but it didn’t work.

gerhard suggested adding dependencies to the Gemfile and using Bundler to install the gems. This involved running commands inside the Docker container to install the gems and then running the import script as the discourse user.

tobiasmuehl confirmed that installing the gem using Bundler worked, but running the import script with Bundler didn’t. Instead, they used su discourse -c 'ruby script/import_scripts/my_import_script.rb' to successfully run the script.

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

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