Elixir Examples
Beginner 15 min read Phase 1

Getting Started

Install Elixir, explore the IEx interactive shell, and create your first Mix project. A beginner-friendly guide with code examples comparing Elixir to Python and JavaScript.

What Is Elixir?

Elixir is a dynamic, functional programming language designed for building scalable and maintainable applications. Created by Jose Valim in 2011, it runs on the Erlang Virtual Machine (BEAM), the same platform that powers telephone switches, messaging systems, and other infrastructure that demands high availability.

Why the BEAM Matters

The BEAM VM was built by Ericsson in the 1980s to run telecom systems that needed 99.9999999% uptime (roughly 31 milliseconds of downtime per year). When you write Elixir, your code inherits this runtime’s strengths: lightweight processes, preemptive scheduling, hot code upgrades, and built-in distribution across nodes.

Elixir adds modern language features on top of the BEAM: a Ruby-like syntax, powerful metaprogramming through macros, first-class documentation, and a thriving ecosystem of libraries published on hex.pm.

Why Learn Elixir?

  • Concurrency without complexity. The BEAM spawns lightweight processes (not OS threads) that communicate through message passing. You can run millions of them simultaneously.
  • Fault tolerance by design. Supervisors monitor processes and restart them when they crash, following the “let it crash” philosophy.
  • Productivity. Pattern matching, the pipe operator, and a consistent standard library make code concise and readable.
  • Growing ecosystem. Phoenix (web framework), LiveView (real-time UIs), Nx (numerical computing), and Nerves (embedded systems) are just a few of the major projects in the ecosystem.

Installing Elixir

The recommended way to install Elixir depends on your operating system.

macOS (using Homebrew):

brew install elixir

Ubuntu/Debian:

sudo apt-get update
sudo apt-get install elixir

Windows (using the official installer):

Download the installer from elixir-lang.org/install and follow the prompts.

Using asdf version manager (recommended for managing multiple versions):

asdf plugin add erlang
asdf plugin add elixir
asdf install erlang 27.0
asdf install elixir 1.17.2-otp-27
asdf global erlang 27.0
asdf global elixir 1.17.2-otp-27

After installation, verify everything is working:

elixir --version

You should see output similar to:

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

Elixir 1.17.2 (compiled with Erlang/OTP 27)
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9.663 17h4.673M12 3v1m6.364 1.636l-.707.707M21 12h-1M4 12H3m3.343-5.657l-.707-.707m2.828 9.9a5 5 0 117.072 0l-.548.547A3.374 3.374 0 0014 18.469V19a2 2 0 11-4 0v-.531c0-.895-.356-1.754-.988-2.386l-.548-.547z"/>Tip
The output shows both the Erlang/OTP version and the Elixir version. Elixir relies on Erlang being installed, and most installation methods handle both automatically.

Exploring IEx

IEx (Interactive Elixir) is Elixir’s REPL (Read-Eval-Print Loop). Start it by running iex in your terminal:

IEx
iex> 1 + 1
2
iex> "hello" <> " " <> "world"
"hello world"
iex> String.upcase("elixir")
"ELIXIR"

IEx is an essential tool you will use constantly. A few helpful commands inside IEx:

IEx
iex> h String.upcase
Returns a string where all characters are converted to uppercase...
iex> i "hello"
Term
  "hello"
Data type
  BitString
Byte size
  5
Description
  This is a string: a UTF-8 encoded binary...

The h helper displays documentation for any module or function. The i helper inspects a value, showing its type and metadata. These two commands alone make IEx a powerful learning environment.

<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/>Note
To exit IEx, press Ctrl+C twice, or type System.halt.

Comparing the REPL Experience

If you are coming from Python or JavaScript, the IEx experience will feel familiar but has some Elixir-specific features built in.

# Python REPL
>>> help(str.upper)
>>> type("hello")
<class 'str'>
>>> "hello".upper()
'HELLO'
// Node.js REPL
> typeof "hello"
'string'
> "hello".toUpperCase()
'HELLO'
# Elixir IEx
iex> h String.upcase
iex> i "hello"
iex> String.upcase("hello")
"ELIXIR"

Notice that in Elixir, functions are called on modules (String.upcase("hello")) rather than on the data itself ("hello".upper()). This is a characteristic of functional programming: data and functions are separate.

Creating Your First Mix Project

Mix is Elixir’s build tool. It creates projects, compiles code, runs tests, manages dependencies, and much more. Think of it as a combination of npm, pip, cargo, and make rolled into one.

Create a new project:

mix new hello

This generates a project directory:

hello/
  lib/
    hello.ex        # Your main module
  test/
    hello_test.exs  # Tests
    test_helper.exs # Test configuration
  mix.exs           # Project configuration (like package.json or Cargo.toml)
  README.md
  .formatter.exs    # Code formatter configuration
  .gitignore

Let’s look at the generated lib/hello.ex:

defmodule Hello do
  @moduledoc """
  Documentation for `Hello`.
  """

  @doc """
  Hello world.

  ## Examples

      iex> Hello.hello()
      :world

  """
  def hello do
    :world
  end
end

This defines a module named Hello with a single function hello/0 that returns the atom :world. The @moduledoc and @doc attributes are documentation that IEx and documentation generators can read.

Running Your Code

There are several ways to execute Elixir code.

Run a script directly:

Create a file called hello.exs (note the .exs extension for scripts):

IO.puts("Hello, world!")

Run it:

elixir hello.exs

Run inside your Mix project:

cd hello
iex -S mix

The -S mix flag tells IEx to start with your project loaded:

IEx
iex> Hello.hello()
:world
iex> IO.puts("Hello from my first project!")
Hello from my first project!
:ok

Run tests:

mix test
Compiling 1 file (.ex)
Generated hello app
..
Finished in 0.03 seconds (0.03s async, 0.00s sync)
1 test, 0 failures

.ex vs .exs Files

Elixir uses two file extensions: .ex files are compiled into BEAM bytecode and are used for production code. .exs files are scripts that are interpreted at runtime, typically used for configuration, tests, and one-off scripts. Both contain valid Elixir code; the difference is in how they are executed.

Modifying the Project

Open lib/hello.ex and add a new function:

defmodule Hello do
  @moduledoc """
  Documentation for `Hello`.
  """

  @doc """
  Greets the given name.

  ## Examples

      iex> Hello.greet("Elixir")
      "Hello, Elixir!"

  """
  def greet(name) do
    "Hello, #{name}!"
  end
end

The #{} syntax is string interpolation – it evaluates the expression inside the braces and inserts the result into the string.

Now test it:

IEx
iex> Hello.greet("Elixir")
"Hello, Elixir!"
iex> Hello.greet("World")
"Hello, World!"

Create Your Own Mix Project

  1. Run mix new greeter to create a new project.
  2. In lib/greeter.ex, write a function greet/1 that takes a name and returns "Welcome, <name>! Let's learn Elixir together.".
  3. Add a function greet/0 (no arguments) that returns "Welcome, stranger! Let's learn Elixir together.".
  4. Start iex -S mix and call both functions.
  5. Run mix test to make sure the generated tests still pass.

Bonus: Add @doc attributes to your functions and use h Greeter.greet in IEx to view them.

What’s Next

You now have Elixir installed, know how to use IEx for exploration, and can create and run Mix projects. In the next lesson, you will learn about Elixir’s basic data types – the building blocks every program is made of.

Related Lessons

Key Takeaways

  • Elixir runs on the Erlang VM (BEAM), giving it access to decades of battle-tested concurrency and fault-tolerance infrastructure
  • IEx is your interactive playground for experimenting with Elixir code
  • Mix is the build tool that manages projects, dependencies, and tasks
  • Every Elixir project follows a conventional directory structure created by `mix new`