LoginSignup
6
7

More than 3 years have passed since last update.

バージョン指定してrails new したけど、なぜか最新版が入ってしまう〜

Posted at

はじめに

この記事の趣旨は、ローカル環境Rails6.0.3.3の状態でRails6.0.3.1のアプリをrails newする手順とその際に発生したエラー対応をご紹介します。

環境

Rails 6.0.3.3

手順

1 インスール済みのrailsのバージョンを確認

% rails -v  
Rails 6.0.3.3

2 使用できるバージョン一覧を確認

% gem list rails
** LOCAL GEMS ***

rails (6.0.3.4, 6.0.3.3, 6.0.3.2, 5.2.4.3)

3 rails 6.0.3.1 をインストールする

gem i -v 6.0.3.1 rails

4 再びバージョンを確認

% gem list rails                                                                                                                                                                               

*** LOCAL GEMS ***

rails (6.0.3.4, 6.0.3.3, 6.0.3.2, 6.0.3.1, 5.2.4.3)

5 Gemfileを読み込まずに「rails new」する

rails _6.0.3.1_ new SampleApp -d mysql --skip-bundle

説明

  • -d mysql はDBをmysql に指定している.

  • _6.0.3.1_のところに指定したいバージョン名(数字)を記入。

  • new SampleAppはアプリ名。

  • --skip-bundle は Gemfileを読み込まないことを指定している。

6 Gemfileを 編集

現時点ではローカルのrailsバージョン(今回であれば6.0.3.3)が指定されているので、Gemfileを編集し、指定したいバージョン(今回であれば6.0.3.1)に変更する。

変更前) gem 'rails', '~> 6.0.3.3'
変更後) gem 'rails', '= 6.0.3.1'

説明

  • ~> 6.0.3.3 はrails 6.0.x.x (6.0系)であれば使用可能という意味なので、この状態だと最新である6.0.3.3. がインストールされてしまう。

7bundle install する

% bundle install

すると以下のようなエラーが発生する

The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
Fetching gem metadata from https://rubygems.org/............
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies...
Bundler could not find compatible versions for gem "activesupport":
  In snapshot (Gemfile.lock):
    activesupport (= 6.0.3.4)

  In Gemfile:
    web-console (>= 3.3.0) was resolved to 4.0.4, which depends on
      actionview (>= 6.0.0) was resolved to 6.0.3.4, which depends on
        activesupport (= 6.0.3.4)

    rails (= 6.0.3.1) was resolved to 6.0.3.1, which depends on
      activesupport (= 6.0.3.1)

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

簡単にいうと、「rails 6.0.3.1を指定したいのであれば、他のgemもバージョン合わせないと使えないよ!」とエラー出てます。

なので、エラー文指示通り、bundle updateで他のgemのバージョンを変更します。

8 bundle update する

% bundle update

指定したバージョンのアプリが作成できてるか確認

% rails -v
Rails 6.0.3.1

これにて終了です!

参考

【Railsエラー】rails newでバージョン指定しても、それ以上のバージョンが入ってしまう場合の対処法
https://techtechmedia.com/rails-new-version-error/

6
7
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7