Elixir安装 #

一、安装前准备 #

Elixir运行在Erlang虚拟机(BEAM)上,因此需要先安装Erlang/OTP。

1.1 系统要求 #

操作系统 最低版本
macOS 10.14+
Windows Windows 10+
Linux 主流发行版

1.2 版本选择 #

推荐使用:

  • Elixir 1.16+
  • Erlang/OTP 26+

二、macOS安装 #

2.1 使用Homebrew(推荐) #

bash
brew install elixir

2.2 使用asdf版本管理器 #

asdf可以管理多个Elixir和Erlang版本:

bash
brew install asdf

asdf plugin add erlang
asdf plugin add elixir

asdf install erlang 26.2.5
asdf install elixir 1.16.2

asdf global erlang 26.2.5
asdf global elixir 1.16.2

2.3 验证安装 #

bash
elixir --version

输出示例:

text
Erlang/OTP 26 [erts-14.2.1] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit]

Elixir 1.16.2 (compiled with Erlang/OTP 26)

三、Windows安装 #

3.1 使用安装程序 #

  1. 访问 Elixir官网
  2. 下载Windows安装程序
  3. 运行安装程序,按提示完成安装

3.2 使用Scoop #

powershell
scoop install erlang
scoop install elixir

3.3 使用Chocolatey #

powershell
choco install elixir

3.4 验证安装 #

打开命令提示符或PowerShell:

powershell
elixir --version

四、Linux安装 #

4.1 Ubuntu/Debian #

bash
wget https://packages.erlang-solutions.com/erlang-solutions_2.0_all.deb
sudo dpkg -i erlang-solutions_2.0_all.deb
sudo apt-get update

sudo apt-get install esl-erlang
sudo apt-get install elixir

4.2 Fedora #

bash
sudo dnf install erlang
sudo dnf install elixir

4.3 Arch Linux #

bash
sudo pacman -S erlang
sudo pacman -S elixir

4.4 使用asdf(推荐) #

bash
git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.14.0

echo '. "$HOME/.asdf/asdf.sh"' >> ~/.bashrc
echo '. "$HOME/.asdf/completions/asdf.bash"' >> ~/.bashrc
source ~/.bashrc

asdf plugin add erlang
asdf plugin add elixir

asdf install erlang 26.2.5
asdf install elixir 1.16.2

asdf global erlang 26.2.5
asdf global elixir 1.16.2

五、IEx交互式Shell #

5.1 启动IEx #

bash
iex

输出示例:

text
Erlang/OTP 26 [erts-14.2.1] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit]

Interactive Elixir (1.16.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)>

5.2 基本使用 #

elixir
iex(1)> 1 + 1
2

iex(2)> "Hello, " <> "Elixir"
"Hello, Elixir"

iex(3)> IO.puts("Hello, World!")
Hello, World!
:ok

5.3 常用快捷键 #

快捷键 功能
Ctrl+C 退出IEx
Ctrl+C, Ctrl+C 强制退出
Tab 自动补全
↑/↓ 历史命令
Ctrl+L 清屏

5.4 帮助系统 #

elixir
iex(1)> h()
iex(2)> h(IO)
iex(3)> h(IO.puts/1)
iex(4)> h(Enum.map/2)

5.5 编译和加载模块 #

elixir
iex(1)> c("my_module.ex")
[MyModule]

iex(2)> r(MyModule)
{:reloaded, MyModule}

六、开发工具 #

6.1 VS Code #

推荐扩展:

  • ElixirLS: Elixir support and debugger
  • Elixir Test
  • Elixir Formatter

6.2 IntelliJ IDEA #

安装 Elixir 插件。

6.3 Emacs #

使用 AlchemistElixir-mode

6.4 Vim/Neovim #

使用 vim-elixirelixir-ls

七、项目创建 #

7.1 创建新项目 #

bash
mix new my_app

输出:

text
* creating README.md
* creating .formatter.exs
* creating .gitignore
* creating lib/my_app.ex
* creating lib/my_app/application.ex
* creating test/test_helper.exs
* creating test/my_app_test.exs
* creating mix.exs

Your Mix project was created successfully.

7.2 项目结构 #

text
my_app/
├── lib/
│   ├── my_app.ex
│   └── my_app/
│       └── application.ex
├── test/
│   ├── test_helper.exs
│   └── my_app_test.exs
├── .formatter.exs
├── .gitignore
├── mix.exs
└── README.md

7.3 编译项目 #

bash
cd my_app
mix compile

7.4 运行测试 #

bash
mix test

7.5 启动IEx with项目 #

bash
iex -S mix

八、包管理 #

8.1 Hex包管理器 #

Hex是Elixir的包管理器。

8.2 添加依赖 #

编辑 mix.exs

elixir
defp deps do
  [
    {:phoenix, "~> 1.7"},
    {:ecto, "~> 3.10"}
  ]
end

8.3 安装依赖 #

bash
mix deps.get

8.4 查看依赖 #

bash
mix deps

8.5 更新依赖 #

bash
mix deps.update --all

九、常见问题 #

9.1 Erlang版本不兼容 #

确保Erlang版本与Elixir版本兼容。

9.2 编译错误 #

清理并重新编译:

bash
mix clean
mix compile

9.3 依赖冲突 #

删除依赖缓存:

bash
rm -rf _build
rm -rf deps
mix deps.get

十、总结 #

本章介绍了:

  • 各平台安装Elixir的方法
  • IEx交互式Shell的使用
  • 开发工具配置
  • Mix项目管理
  • Hex包管理

准备好编写第一个Elixir程序了吗?让我们进入下一章。

最后更新:2026-03-27