LoginSignup
9

More than 1 year has passed since last update.

posted at

updated at

Organization

【Rails】ArgumentError: Malformed version number string 0.32+gitでwebpacker:installが実行できない場合の対処方法

はじめに

webpacker:installを実行すると表題のエラーメッセージが表示されてしまい、Railsが実行できない状況が起きました。
今回はこの現象の回避方法についてまとめます。

この記事の対象者

  • 表題のエラーメッセージが表示され、webpacker:installを実行できない人

実際にどのようなエラーになるのか

エラーに遭遇した環境

Docker公式のRuby:2.7.1を利用したコンテナで遭遇しました。
他の環境では試していないため、同じ方法で対処できると断定はできませんが、参考になればと思います。
使用したDockerfileとRailsのバージョンは以下の通りです。

Dockerfile
FROM ruby:2.7.1
LABEL maintainer="TomoProg"
RUN apt-get update -qq && apt-get install -y \
    build-essential \
    libpq-dev \
    nodejs \
    default-mysql-client \
    vim-tiny

RUN apt-get install -y yarn

RUN mkdir /myapp
WORKDIR /myapp
$ rails --version
Rails 6.0.3.2

実際のエラーメッセージ

webpacker:installを実行すると以下のようなエラーメッセージが表示されました。

root@3cd2b69c861a:/myapp# bin/rails webpacker:install
rails aborted!
ArgumentError: Malformed version number string 0.32+git
/usr/local/bundle/gems/webpacker-4.2.2/lib/tasks/webpacker/check_yarn.rake:12:in `block (2 levels) in <main>'
/usr/local/bundle/gems/railties-6.0.3.2/lib/rails/commands/rake/rake_command.rb:23:in `block in perform'
/usr/local/bundle/gems/railties-6.0.3.2/lib/rails/commands/rake/rake_command.rb:20:in `perform'
/usr/local/bundle/gems/railties-6.0.3.2/lib/rails/command.rb:48:in `invoke'
/usr/local/bundle/gems/railties-6.0.3.2/lib/rails/commands.rb:18:in `<main>'
/usr/local/bundle/gems/bootsnap-1.4.7/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
/usr/local/bundle/gems/bootsnap-1.4.7/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
/usr/local/bundle/gems/bootsnap-1.4.7/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
/usr/local/bundle/gems/bootsnap-1.4.7/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
/usr/local/bundle/gems/bootsnap-1.4.7/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
/usr/local/bundle/gems/activesupport-6.0.3.2/lib/active_support/dependencies.rb:324:in `block in require'
/usr/local/bundle/gems/activesupport-6.0.3.2/lib/active_support/dependencies.rb:291:in `load_dependency'
/usr/local/bundle/gems/activesupport-6.0.3.2/lib/active_support/dependencies.rb:324:in `require'
/myapp/bin/rails:9:in `<top (required)>'
/usr/local/bundle/gems/spring-2.1.0/lib/spring/client/rails.rb:28:in `load'
/usr/local/bundle/gems/spring-2.1.0/lib/spring/client/rails.rb:28:in `call'
/usr/local/bundle/gems/spring-2.1.0/lib/spring/client/command.rb:7:in `call'
/usr/local/bundle/gems/spring-2.1.0/lib/spring/client.rb:30:in `run'
/usr/local/bundle/gems/spring-2.1.0/bin/spring:49:in `<top (required)>'
/usr/local/bundle/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `load'
/usr/local/bundle/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `<top (required)>'
/myapp/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Tasks: TOP => webpacker:install => webpacker:check_yarn
(See full trace by running task with --trace)

原因

webpacker:installする際にyarnが必要なのですが、
そのままapt-getでインストールすると0.32+gitというバージョンでインストールされてしまい、
webpack:installした際に行われるyarnのバージョンチェック時にエラーが出てしまうようです。
ちなみにyarnのバージョンは以下のコマンドで確認できます。

$ yarn --version
0.32+git

対処方法

yarnの公式サイトに掲載されているこちらのインストール方法を参考に、安定版で最新のyarnをインストールするようにDockerfileを下記のように変更しました。
(このインストール方法はUbuntu/Debianでのインストール方法です。別のOSの場合はリンク先のOperating Systemのプルダウンメニューから環境に合わせて確認してください。)

Dockerfile
FROM ruby:2.7.1

# -- 省略 --

# そのままapt-getでインストールする方法ではなく
#RUN apt-get install -y yarn

# 公式のインストール方法を参考に安定版で最新のyarnをインストールする
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update && apt-get install yarn

# -- 省略 --

その結果、yarnのバージョンが変わり、webpacker:installが正常に完了するようになりました。
実際のyarnのバージョンとwebpacker:installの実行結果は下記の通りです。

yarnのバージョン確認

$ yarn --version
1.22.4

webpacker:installの実行結果

root@cb295863c75e:/myapp# bin/rails webpacker:install
      create  config/webpacker.yml
Copying webpack core config
      create  config/webpack
      create  config/webpack/development.js
      create  config/webpack/environment.js
      create  config/webpack/production.js
      create  config/webpack/test.js
Copying postcss.config.js to app root directory
      create  postcss.config.js

-- 省略 --

├─ thunky@1.1.0
├─ type-is@1.6.18
├─ unpipe@1.0.0
├─ utils-merge@1.0.1
├─ wbuf@1.7.3
├─ webpack-dev-middleware@3.7.2
├─ webpack-dev-server@3.11.0
└─ ws@6.2.1
Done in 280.04s.
Webpacker successfully installed 🎉 🍰

まとめ

ArgumentError: Malformed version number string 0.32+gitが起きた際の対処方法をまとめてみました。
同じようなエラーで困っている方に参考になればと思います。

それでは良いRailsライフを!

TomoProg

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
What you can do with signing up
9