[Ruby] I want to add devise with Rails but I can’t do a bundle install

3 minute read

Since an error occurred when building the environment with docker and adding the gem, I will share information as a memorandum!

Development environment

docker rails (5.2.0) ruby (2.7.1) mysql (5.7) nginx puma

  • Gem is updated with docker-compose build, but it takes time, so please refer to the following article I am setting to update the gem quickly. Https://qiita.com/neko-neko/items/abe912eba9c113fd527e

error contents

Add devise gem in Gemfile and do docker-compose run –rm rails bundle install

~ RailsApp% docker-compose run --rm rails bundle install
Starting railsapp_db_1 ... done
Fetching gem metadata from https://rubygems.org/.............
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies....
Bundler could not find compatible versions for gem "railties":
  In snapshot (Gemfile.lock):
    railties (= 5.2.4.3)

  In Gemfile:
    devise (= 4.1.0) was resolved to 4.1.0, which depends on
      railties (< 5.1, >= 4.1.0)

    rails (~> 5.2.2) was resolved to 5.2.4.3, which depends on
      railties (= 5.2.4.3)

Running `bundle update` will rebuild your snapshot from scratch, using only
the gems in your Gemfile, which may resolve the conflict.

I didn’t know what was causing it, but for the time being it said “may be solved by bundle update” Execute docker-compose run –rm rails bundle update

↓↓↓↓↓↓↓↓

~ RailsApp% docker-compose run --rm rails bundle update
Starting railsapp_db_1 ... done
Fetching gem metadata from https://rubygems.org/.............
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies..........................
Bundler could not find compatible versions for gem "railties":
  In snapshot (Gemfile.lock):
    railties (= 5.2.4.3)

  In Gemfile:
    devise (= 4.1.0) was resolved to 4.1.0, which depends on
      railties (< 5.1, >= 4.1.0)

    rails (~> 5.2.2) was resolved to 5.2.4.3, which depends on
      railties (= 5.2.4.3)

It wasn’t…

Cause

From the conclusion, it seems that the error was that the *devise gem was not in the list. **
I have referred to the following article. https://qiita.com/hatorijobs/items/2928e152f22d009b07d0

If you run this article as it is, it will be solved, but if you are working with docker, the command is a little Since it is different, I will explain it here.

Workaround

Follow the steps below.

1. Check the list of gems with docker-compose exec [optional service name] gem list and confirm that there is no devise gem.

2. Install devise with docker-compose exec [optional service name] gem install devise.

3. Check the list of gems again with docker-compose exec [any service name] gem list, and confirm that devise (4.7.2) is present.

4. Describe **gem'devise','4.7.2' in Gemfile

5. Run docker-compose run --rm rails bundle update

Let’s look at them in order. ↓↓↓↓↓↓↓↓

**1. Check the list of gems with docker-compose exec [optional service name] gem list and confirm that there is no devise gem. **

~ RailsApp% docker-compose exec app gem list
  
*** LOCAL GEMS ***

actioncable (5.2.4.3, 5.2.2)
actionmailer (5.2.4.3, 5.2.2)
actionpack (5.2.4.3, 5.2.2)
actionview (5.2.4.3, 5.2.2)
actionjob (5.2.4.3, 5.2.2)

~Omitted~

web-console (3.7.0)

**2. Install devise with docker-compose exec [optional service name] gem install devise. **

~ RailsApp %docker-compose exec app gem install devise
Fetching warden-1.2.8.gem
Fetching bcrypt-3.1.13.gem

~Omitted~

Successfully installed devise-4.7.2
5 gems installed

**3. Check the list of gems again with docker-compose exec [arbitrary service name] gem list and confirm that devise (4.7.2) is present. **

~ RailsApp% docker-compose exec app gem list
  
*** LOCAL GEMS ***

actioncable (5.2.4.3, 5.2.2)
actionmailer (5.2.4.3, 5.2.2)
actionpack (5.2.4.3, 5.2.2)
actionview (5.2.4.3, 5.2.2)
actionjob (5.2.4.3, 5.2.2)

~Omitted~

devise (4.7.2)

~Omitted~

web-console (3.7.0)

4. Describe gem’devise’,’4.7.2’ in Gemfile

Gemfile.


source'https://rubygems.org'


# Bundle edge Rails instead: gem'rails', github:'rails/rails'
gem'rails','~> 5.0.0','>= 5.0.2.1'
# Use mysql as the database for Active Record
gem'mysql2','>= 0.3.18','< 0.5'
# Use Puma as the app server

~Omitted~

# Add login function
gem'devise','4.7.2'

~Omitted~

5.Run docker-compose run –rm rails bundle update

~ RailsApp% docker-compose run --rm rails bundle update

If there is no error in this, we win

Notice

There are some things I noticed while dealing with errors, so I will share it.

Running docker-compose exec app gem install devise will install the latest gem.
I decided that there is no particular problem with the latest version, so I omitted the explanation of how to specify the version.
* Conversely, please let me know if you know the method!

By the way, I found a site where all version history of devise is posted, so I will link the URL. https://rubygems.org/gems/devise/versions *The latest version as of June 10, 2020 is 4.7.2


[Any service name] is the service name specified in docker-compose.yml.
It can be decided arbitrarily, but it is an impression that many sites set db, app, or web.

*If you don’t understand, I think that the article will be a hit if you check “docker-compose.yml service name”.

At the end

Regarding Rails and docker, I will continue to post articles about errors etc. regularly for output. Also, I think that there was a point that I could not reach the first post, but thank you for reading until the end! !! !!

that’s all