Mix Elixir build tool

Version 1.20.4 · source

Start here

mix new <name>
Create a project
mix deps.get
Fetch dependencies
mix compile
Build the project
mix test
Run the tests
iex -S mix
Shell with project loaded
mix run <file.exs>
Run a script in the app
mix format
Format the code
mix help
List all tasks

Concepts

  1. mix.exs deps, aliases
  2. mix.lock pinned versions
  3. _build/<env> beams per MIX_ENV
  4. Release self-contained

Projects

mix new <name>
Create a new project
mix new <name> --sup
Project with a supervision tree
mix new <name> --umbrella
Umbrella with several apps
mix new <path> --app <app> --module <Module>
Choose the app and module names
mix phx.new <name>
Phoenix project (needs the phx_new archive)
Install first: mix archive.install hex phx_new. Phoenix has its own mix phx.* tasks; see mix help | grep phx.

Compile & run

mix compile
Compile the project
mix compile --force
Recompile everything
mix compile --warnings-as-errors
Fail when there are warnings
mix run <file.exs> [args]
Run a script inside the app
Extra arguments are available through System.argv/0.
mix run -e "<expr>"
Evaluate an expression
mix run --no-halt
Keep the app running
mix eval "<expr>"
Evaluate without starting the app
iex -S mix
Interactive shell with the project
mix clean
Remove compiled files
mix do <task> + <task>
Run several tasks in one call
The + separator needs spaces on both sides (Elixir 1.14+); older versions used a comma.

Test

mix test
Run the whole suite
mix test <file>
Run one test file
mix test <file>:<line>
Run the test at a line
A line that starts a describe block runs everything in it.
mix test --failed
Re-run last failures
mix test --stale
Only tests for changed modules
mix test --only <tag>
Run tests with a tag
mix test --exclude <tag>
Skip tests with a tag
mix test --trace
One test at a time, verbose
mix test --max-failures <n>
Stop after n failures
mix test --seed 0
Run in definition order
mix test --slowest <n>
Show the n slowest tests
mix test --cover
Report code coverage
mix test --repeat-until-failure <n>
Repeat to find flaky tests

Dependencies

mix deps.get
Fetch and lock dependencies
mix deps.update <dep>
Update one dependency
Also updates the deps it depends on.
mix deps.update --all
Update every dependency
mix deps
List deps and their status
mix deps.tree
Show the dependency tree
mix deps.compile
Compile dependencies only
mix deps.clean <dep>
Remove a dependency's build
mix deps.clean --unused --unlock
Drop deps no longer in mix.exs
mix deps.unlock <dep>
Remove a lock entry
Dependency forms in mix.exs
{:plug, "~> 1.16"}{:ex_doc, "~> 0.34", only: :dev, runtime: false}{:lib, github: "<user>/<repo>", tag: "<tag>"}{:local, path: "../local"}

Format & quality

mix format
Format files per .formatter.exs
mix format --check-formatted
Fail if anything is unformatted
mix format <file>
Format specific files
mix format --dry-run --check-formatted
Check without writing
mix hex.outdated
Show deps with newer versions
mix hex.audit
Check deps for retired packages

Releases

MIX_ENV=prod mix release
Build a production release
MIX_ENV=prod mix release <name>
Build a named release
mix release --overwrite
Replace existing release files
mix release.init
Copy release config templates
_build/prod/rel/<name>/bin/<name> start
Run the release in the foreground
bin/<name> daemon
Run in the background (Unix)
bin/<name> remote
Open a shell on the running node
bin/<name> stop
Stop the running release

Environments & tooling

MIX_ENV=<env> mix <task>
Run a task in another env
MIX_DEBUG=1 mix <task>
Print task debug output
mix local.hex
Install or update the Hex client
mix local.rebar
Install rebar for Erlang deps
mix archive.install hex <package>
Install a global archive
mix hex.info <package>
Show package details
mix hex.search <term>
Search packages on Hex
mix hex.publish
Publish a package
Aliases in mix.exs
defp aliases do  [setup: ["deps.get", "compile"],   check: ["format --check-formatted", "test"]]end

Recipes

Fresh clone to green tests
mix deps.getmix compile --warnings-as-errorsmix test
CI gate
mix format --check-formattedmix compile --warnings-as-errorsmix test --cover
Fix a failing test fast
mix test --failed --trace# once it passes, run all againmix test
Ship a release
mix deps.get --only prodMIX_ENV=prod mix release_build/prod/rel/<name>/bin/<name> start

Help

mix help
List available tasks
mix help <task>
Docs for one task
mix --version
Show Elixir and Mix versions