Install Elixir

Elixir is one package with the compiler, iex, mix and the standard library inside it. It depends on Erlang, and the package manager of every system installs both together, so one command is usually the whole job. This lesson installs the toolchain, reads the two lines of elixir --version, and runs one small script that asks the runtime what it is.

The package manager commands below are the ones to run on your machine. They are not run in the environment where this course checks its programs, because that environment has no network and the toolchain is already installed there. The scripts on the page are run, and their output is pasted from the run.

One command per system

On Ubuntu the APT package is called elixir. Refresh the package lists once, then install:

sudo apt update

sudo apt install elixir

On Rocky Linux the same package is in the DNF repositories of the system. The command is one line, and DNF pulls Erlang in as a dependency:

sudo dnf install elixir

macOS uses Homebrew, and FreeBSD uses the ports tree through pkg:

brew install elixir

sudo pkg update

sudo pkg install lang/elixir

On Windows the installer ships Elixir and Erlang together, and on any system a Docker image runs the toolchain without installing anything:

docker run --rm elixir:1 elixir --version

The package versions move: a distribution may ship Elixir 1.18 while the current release is 1.19. For this course any Elixir 1.14 or newer works, and a reader who wants a specific release installs it with asdf or mise instead of the system package. Neither tool is needed for the lessons.

The two version lines

The first check is the command itself. elixir --version prints two blocks: the Erlang runtime it runs on, then the Elixir version and the OTP release it was compiled with:

elixir --version

Erlang/OTP 27 [erts-15.2.7.14] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [jit:ns]

Elixir 1.19.7 (compiled with Erlang/OTP 27)

Read the first line for the runtime and the second for the language. The output above comes from the image this course is checked in, so the numbers on your machine will differ; the shape of the two lines is what matters. The flags of the first line describe the machine and the build of Erlang, not your program.

The second line says which OTP release the Elixir build was compiled with. A mismatch between the two lines is the usual reason a fresh install fails to start: the package manager installed an Elixir built for a newer Erlang than the one it also installed.

Ask the runtime from a script

The version is also available to a program. System.version/0 returns the Elixir version, and :erlang.system_info/1 answers questions about the runtime. This script prints all three values, with a label in front of each:

version.exs

IO.puts("Elixir " <> System.version()) IO.puts("OTP " <> to_string(:erlang.system_info(:otp_release))) IO.puts("ERTS " <> to_string(:erlang.system_info(:version)))

elixir version.exs

Elixir 1.19.7
OTP 27
ERTS 15.2.7.14

System.version/0 already returns a binary, so it joins the label with <> directly. The other two calls return a charlist, which is a list of character codes, and to_string/1 turns it into a binary first. The next section shows what happens without that call.

The mistake: a charlist version

Erlang functions return charlists where an Elixir function would return a binary, and the difference is invisible in prose. Joining the OTP release without to_string/1 stops the script with an ArgumentError:

otp.exs

IO.puts("OTP " <> :erlang.system_info(:otp_release))

elixir otp.exs

** (ArgumentError) construction of binary failed: segment 2 of type 'binary': expected a binary but got: ~c"27"
    otp.exs:1: (file)
    (elixir 1.19.7) lib/code.ex:1613: Code.require_file/2

The ~c"27" in the message is a charlist, and it is the clue: the value is a list, not a binary. Wrap it in to_string/1, or use List.to_integer/1 when the question is about the release as a number.

A page for each system

Each system has a page of its own with the commands of its package manager, the paths the installer writes, and the same two checks at the end. A reader who has never opened a terminal starts with the page of the system they use:

With a working elixir command, the next lesson explains what the runtime underneath it actually does. The shell comes after that.

The scripts on this page were run with Elixir 1.19.7 on Erlang/OTP 27 (erts 15.2.7.14), on Linux x86_64 in a Docker container.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does elixir --version print?

    Answer

    Two blocks: the Erlang/OTP runtime, then the Elixir version and the OTP it was compiled with

    The command reports the runtime first and the language second, each in its own block.

  2. Which part of elixir --version output tells you the Erlang runtime version?

    Answer

    The Erlang/OTP 27 [erts-...] line

    The first block names the runtime; the second names Elixir and the OTP it was built against.

  3. Why does System.version() join a label with <> without a conversion?

    Answer

    It returns a binary already

    System.version/0 returns a binary, while :erlang.system_info/1 returns a charlist.

  4. What does this program print? IO.puts(is_list(:erlang.system_info(:otp_release)))

    Answer

    true

    The OTP release comes back as a charlist, which is a list of character codes.

  5. What does this program print? IO.puts(System.version() |> String.split(".") |> hd())

    Answer

    1

    Splitting the version on dots gives ["1", "19", "7"], and hd/1 takes the first part.

  6. What does this program print? IO.puts(42 |> to_string() |> String.length())

    Answer

    2

    to_string/1 gives "42", which has two characters, so String.length/1 returns 2.

  7. What does this program do? IO.puts("OTP " <> :erlang.system_info(:otp_release))

    Answer

    An ArgumentError: the release is a charlist and <> needs a binary

    The concatenation expects a binary on each side and the release is a charlist.

  8. What does this program do? IO.puts("OTP " <> 27)

    Answer

    An ArgumentError: <> does not accept an integer

    The right side is an integer, and <> joins binaries only; to_string/1 is missing.

  9. Which command installs Elixir and Erlang together on Ubuntu?

    Answer

    sudo apt install elixir

    The elixir package of APT depends on Erlang, so APT installs both.

  10. What should you do when the second line of elixir --version names an OTP newer than the first line?

    Answer

    Install a matching Erlang, or an Elixir built for the OTP you have

    The Elixir build expects the OTP release it was compiled with; a mismatch is a broken install.

Автор статьи: Arthur Isaev