Search code examples
ruby-on-railsrubyrubygemsbundler

Bundler: how to remove uninstalled gems


I'm trying to install the pg_search gem. In the first attempt I did not pay attention to the necessary version of ruby (we are using 2.3.1 and 2.4 was required), in the error message that appeared I was asked to run bundle update, but it updated pg_search to 2.3.5 which require ruby >= 2.5. Even though I specified an older version of the gem, it still shows the same message:

Gem::InstallError: pg_search requires Ruby version >= 2.5.
An error occurred while installing pg_search (2.3.5), and Bundler cannot continue.
Make sure that `gem install pg_search -v '2.3.5'` succeeds before bundling.

I already installed the gem by running docker-compose run web gem install pg_search -v 2.1.4, and recreated the container. My Gemfile:

source 'https://rubygems.org'

gem 'rails', '~> 5.2.0'
# Use sqlite3 as the database for Active Record
# Use Puma as the app server

#gem 'mina-puma', :require => false
gem 'puma', '~> 3.7.1'
gem 'pg', '~> 0.18'
gem 'pg_search', '~> 2.1', '>= 2.1.4'
...

Bundler version: bundler (>= 1.3.0)

I would like to know how to remove pg_search 2.3.5 and install 2.1.4.


Solution

  • Even though I specified an older version of the gem

    No, you didn't.

    You specified '~> 2.1', '>= 2.1.4', which means anything 2.1.4 <= version < 3.0.0.

    By running bundle update, this installed the latest available version that met your requirements, which was apparently 2.3.5, not 2.1.4.

    If you need to also specify a constraint to ruby version 2.3.1, you can also put this in the Gemfile:

    ruby '2.3.1'
    

    ...And then running bundle update will also take that into consideration when finding the latest compatible dependencies.


    I would like to know how to remove pg_search 2.3.5 and install 2.1.4

    You don't have version 2.3.5 installed against this ruby version, because it's incompatible.

    Apparently you've already installed version 2.1.4.

    The problem is that your Gemfile.lock is still expecting version 2.3.5. There are a few ways you could resolve this, but one way or another you need to update the Gemfile.lock to have a compatible set of dependencies with your ruby version.

    The simplest approach is probably to just re-run bundle update pg_search, but make sure you're actually using the correct ruby version this time. That should downgrade the dependency, as the newer library version isn't compatible with the older ruby version.

    If you still encounter issues, you could take my advice of adding the ruby constraint to the Gemfile, and revert whatever other changes you've recently made that created this incompatible mix of dependencies.