如何使用Erlang/OTP 25编译器重新编译rebar3(Elixir 1.13.4环境)
执行mix setup失败(OTP25环境下rebar3编译报错)
问题现象
- 学习Phoenix LiveView Starter免费录屏教程时,未进入正式开发阶段,执行
mix setup命令失败,核心报错如下:
=ERROR REPORT==== 6-Jul-2022::12:47:58.702361 === beam/beam_load.c(551): Error loading function rebar3:parse_args/1: op put_tuple u x: please re-compile this module with an Erlang/OTP 25 compiler escript: exception error: undefined function rebar3:main/1 in function escript:run/2 (escript.erl, line 750) in call from escript:start/1 (escript.erl, line 277) in call from init:start_em/1 in call from init:do_boot/3 ** (Mix) Could not compile dependency :telemetry, "/Users/martinstabenfeldt/.mix/rebar3 bare compile --paths /Users/martinstabenfeldt/Work/pragmaticstudio.com-liveview-starter/code-bundle/live_view_studio/_build/dev/lib/*/ebin" command failed. Errors may have been logged above. You can recompile this dependency with "mix deps.compile telemetry", update it with "mix deps.update telemetry" or clean it with "mix deps.clean telemetry"
- 本地环境版本查询结果:
$ elixir --version Erlang/OTP 25 [erts-13.0.2] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [jit:ns] [dtrace] Elixir 1.13.4 (compiled with Erlang/OTP 24)
- 项目mix.exs依赖配置:
defp deps do [ {:phoenix, "~> 1.5.13"}, {:phoenix_ecto, "~> 4.4"}, {:ecto_sql, "~> 3.4"}, {:postgrex, ">= 0.0.0"}, {:phoenix_live_view, "~> 0.16.4"}, {:floki, ">= 0.30.0", only: :test}, {:phoenix_html, "~> 3.0.4"}, {:phoenix_live_reload, "~> 1.2", only: :dev}, {:phoenix_live_dashboard, "~> 0.4"}, {:telemetry_metrics, "~> 0.6"}, {:telemetry_poller, "~> 1.0"}, {:gettext, "~> 0.11"}, {:jason, "~> 1.0"}, {:plug_cowboy, "~> 2.0"}, {:faker, "~> 0.17.0"}, {:number, "~> 1.0"} ] end
- 已尝试操作:执行
mix local-rebar --force、mix deps.update telemetry,均未解决问题。
根因分析
- 核心版本不匹配:当前运行的Erlang/OTP版本为25,但安装的Elixir 1.13.4是基于OTP24编译的,Elixir编译版本和运行时OTP版本不一致会引发各类字节码加载异常。
- 本地缓存的rebar3是基于低于OTP25的版本编译的,OTP25无法加载旧版本编译的rebar3字节码,导致依赖编译流程直接中断,报错中提到的telemetry编译失败只是表层现象,并非问题根源。
- 之前执行的rebar更新命令有误:正确的mix任务名是
local.rebar(点分隔),不是local-rebar(横杠分隔),命令写错导致旧的rebar3缓存根本没有被替换。
解决步骤
按顺序执行以下操作即可修复:
- 对齐Elixir与OTP编译版本,二选一即可:
- 方案A(推荐):安装基于OTP25编译的Elixir版本,可直接升级到Elixir 1.14+,原生完美兼容OTP25
- 方案B:将本地Erlang/OTP降级到24版本,和当前已安装的Elixir 1.13.4编译版本对齐
- 彻底删除本地旧的rebar缓存:
rm -rf ~/.mix/rebar ~/.mix/rebar3 - 重新安装适配当前OTP版本的rebar:
mix local.rebar --force - 清理项目旧的编译和依赖缓存:
mix deps.clean --all rm -rf _build - 重新执行项目初始化命令:
mix setup
内容的提问来源于stack exchange,提问作者martins
相关产品推荐
相关产品推荐

