Skip to main content

Hello World (first programs) · Beginner · 16 min

Hello World in Elixir: iex, scripts, Mix and your first process

By Padding · Updated 18 Nov 2026
Elixir 1.20.5 · Erlang/OTP 29 · elixir:1.20-otp-29

You will install Elixir 1.20 on Erlang/OTP 29, print Hello World in iex and from a script, build and test a small Mix project, and start processes that send messages back to you.

On this page

Elixir is a functional language that runs on the BEAM, the virtual machine built for Erlang at Ericsson to run telephone switches that were not allowed to go down. Elixir code compiles to the same bytecode as Erlang, so it inherits the BEAM's main idea: a program is made of many small, isolated processes that talk only by sending each other messages. These are not operating system processes or threads. Each one has its own memory, and the BEAM can run hundreds of thousands of them at once on an ordinary laptop.

In this tutorial you install Elixir 1.20 and Erlang/OTP 29, try a few lines in iex (Interactive Elixir), run a script, chain function calls with the pipe operator, and create a project with Mix, Elixir's build tool. Then you go one step further and start processes that send messages back to you, which is what Elixir is built for.

José Valim started Elixir in 2011, and version 1.0 was released in September 2014. It is used most for real-time web applications with the Phoenix framework, chat and messaging back ends (Discord built its chat infrastructure on Elixir), and connected devices through the Nerves project. Data in Elixir is immutable: once a value exists it never changes, and functions return new values instead. You will see why that matters as you go.

Step 1 · Install Elixir and Erlang

Elixir needs Erlang/OTP underneath it, so you install both. Ubuntu's own elixir package is usually several versions behind, so use the official install script, which downloads ready-built Erlang and Elixir into your home folder. You need curl first:

  • Ubuntu or macOS:
Bash
sudo apt install -y curl
curl -fsSO https://elixir-lang.org/install.sh
sh install.sh elixir@1.20.5 otp@29.1

On macOS, skip the apt line; curl is already installed. The script ends by printing two export PATH=... lines. They add the folders holding erl, elixir, iex and mix to your PATH, the list of folders your terminal searches for programs. Add them to your shell's start-up file and load it (on macOS, use ~/.zshrc instead of ~/.bashrc):

Bash
echo 'export PATH=$HOME/.elixir-install/installs/otp/29.1/bin:$PATH' >> ~/.bashrc
echo 'export PATH=$HOME/.elixir-install/installs/elixir/1.20.5-otp-29/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
  • Windows: in PowerShell, run curl.exe -fsSO https://elixir-lang.org/install.bat and then .\install.bat elixir@1.20.5 otp@29.1, and add the folders it prints to your PATH. Or install Erlang from its installer and then the Elixir installer for OTP 29.

If you already use a version manager such as mise or asdf, it can install Erlang 29 and Elixir 1.20 instead; follow its Elixir instructions. Files you unpack this way get no system security updates: to upgrade, run the script again with the newer version numbers from the Elixir install page and update the two PATH lines.

Now check both versions:

Bash
elixir --version

Check it worked

The terminal prints a line about Erlang/OTP 29 and then a line starting Elixir 1.20.5 (compiled with Erlang/OTP 29), or a later 1.20 patch number. command not found means the PATH lines have not run in this terminal: open a new terminal, or run source ~/.bashrc again.

Step 2 · Try Elixir in iex

Start Interactive Elixir:

Bash
iex

It prints a banner like Interactive Elixir (1.20.5) - press Ctrl+C to exit and a prompt iex(1)>. iex runs each line when you press Enter and shows its value. Type these lines one at a time:

Text
iex(1)> IO.puts("Hello, world")
Hello, world
:ok
iex(2)> String.upcase("hello")
"HELLO"
iex(3)> name = "Ada"
"Ada"
iex(4)> String.upcase(name)
"ADA"
iex(5)> name
"Ada"

What each line shows:

  • IO.puts("Hello, world") calls the puts function in the IO module. Elixir code is organised in modules, named with a capital letter, and you call a function as Module.function(arguments). puts prints the text and a new line. The :ok underneath is the value the function returned. :ok is an atom, a constant whose value is its own name; Elixir functions return atoms such as :ok and :error to say how things went.
  • String.upcase("hello") returns a new string in capitals.
  • name = "Ada" binds the name name to the value "Ada".
  • String.upcase(name) returns "ADA", but name is still "Ada" afterwards. The function did not change the string; it built a new one. This is immutability: no function can change a value you already have.

Leave iex by pressing Ctrl+C twice, or by typing System.halt().

Check it worked

IO.puts prints Hello, world followed by :ok, and the last line shows name is still "Ada" after String.upcase(name) returned "ADA". After Ctrl+C twice your normal terminal prompt is back.

Step 3 · Run hello.exs as a script

Make a folder for this tutorial and create hello.exs in it. On macOS or Linux you can paste the whole block into the terminal. On Windows, cat > ... << blocks do not work in PowerShell, so create the file in your editor with the two lines between the EXS markers, then run the commands after them. Do the same for every later cat > file << block in this tutorial: save the lines between the markers under the filename given, then run the remaining commands.

Bash
mkdir -p ~/hello-elixir-demo
cd ~/hello-elixir-demo
cat > hello.exs <<'EXS'
# My first Elixir script
IO.puts("Hello, world")
EXS
elixir hello.exs

Line by line:

  • # My first Elixir script is a comment. Elixir ignores everything from # to the end of the line.
  • IO.puts("Hello, world") prints the text, as in iex. A script does not show return values; only what you print appears.
  • elixir hello.exs compiles the file in memory and runs it straight away.

The extension matters. .exs means an Elixir script: it is compiled each time you run it and nothing is saved to disk. .ex files hold the modules of a project, and Mix compiles those once into .beam bytecode files that the BEAM loads. You will see both in the next steps.

Check it worked

The terminal prints Hello, world. ls shows only hello.exs: no compiled file was written next to it.

Step 4 · Greet by name with the pipe operator

Create greet.exs, which takes a name from the command line and passes it through three functions:

Bash
cat > greet.exs <<'EXS'
name = List.first(System.argv(), "world")

clean =
  name
  |> String.trim()
  |> String.capitalize()

IO.puts("Hello, #{clean}")
IO.puts("Your name has #{String.length(clean)} letters.")
EXS
elixir greet.exs
elixir greet.exs "  ada  "

Line by line:

  • System.argv() returns the words you typed after the script name, as a list such as [" ada "]. List.first(list, "world") returns the first item, or "world" if the list is empty.
  • |> is the pipe operator. It takes the value on its left and passes it as the first argument of the function on its right. name |> String.trim() |> String.capitalize() means the same as String.capitalize(String.trim(name)), but it reads top to bottom in the order things happen: take the name, trim the spaces, capitalise it.
  • clean = ... binds the final result to a new name. name itself is unchanged.
  • "Hello, #{clean}" is string interpolation: Elixir puts the value of clean where #{...} is. This works in double-quoted strings.
  • String.length(clean) counts letters as a reader sees them (graphemes), not bytes. A name such as Adébáyọ̀ may store a tone mark as an extra combining character (the grave accent on ọ̀ here), but Elixir still counts 7 letters. byte_size(clean) would give the number of bytes, which is larger.

Try a name with tone marks too:

Bash
elixir greet.exs Adébáyọ̀

Check it worked

With no name the script prints Hello, World (capitalised by the pipe). With " ada " it prints Hello, Ada and Your name has 3 letters., with the spaces gone. With Adébáyọ̀ it reports 7 letters.

Step 5 · Create a project with Mix

Scripts suit small jobs. Anything larger is a Mix project: a folder with a standard layout that Mix knows how to compile, test and run. Create one:

Bash
cd ~/hello-elixir-demo
mix new hello_app
cd hello_app
ls

mix new lists each file it creates. The ones that matter now:

  • mix.exs describes the project: its name, version and the libraries it depends on.
  • lib/hello_app.ex is where your code goes. Mix compiles every .ex file under lib/.
  • test/hello_app_test.exs holds the tests. Test files are scripts, so they end in .exs.

Replace lib/hello_app.ex with this module. Write the whole file, so you know exactly what is in it:

Bash
cat > lib/hello_app.ex <<'EX'
defmodule HelloApp do
  @moduledoc "My first Elixir project."

  @doc """
  Builds a greeting for a name.

      iex> HelloApp.greet("Ada")
      "Hello, Ada"
  """
  def greet(name) do
    "Hello, #{name}"
  end

  def hello do
    :world
  end
end
EX
mix run -e 'IO.puts(HelloApp.greet("Ada"))'

Line by line:

  • defmodule HelloApp do ... end defines a module called HelloApp. Every do is closed by an end.
  • @moduledoc and @doc are documentation. They are stored with the compiled code: start iex -S mix inside hello_app and h HelloApp.greet shows them. Plain iex does not load the project, so there it reports that HelloApp could not be loaded.
  • The indented iex> lines inside @doc are an example. Elixir can run them as a test, called a doctest, to check the documentation is still true.
  • def greet(name) do ... end defines a public function. A function returns the value of its last line, here the greeting string; there is no return keyword.
  • def hello returns the atom :world. Mix's generated test checks for it, so it stays.
  • mix run -e '...' compiles the project, loads it, then runs the code in quotes.

Now run the tests:

Bash
mix test

Check it worked

The first mix run prints a compiling message and then Hello, Ada. A _build folder has appeared: ls _build/dev/lib/hello_app/ebin shows Elixir.HelloApp.beam, the compiled bytecode. mix test ends with Result: 2 passed (1 doctest, 1 test).

Step 6 · Start a process that sends a message back

Now the part Elixir is built for. Go back to the demo folder and create messages.exs:

Bash
cd ~/hello-elixir-demo
cat > messages.exs <<'EXS'
parent = self()

spawn(fn ->
  send(parent, {:greeting, "Hello from #{inspect(self())}"})
end)

receive do
  {:greeting, text} -> IO.puts("Got: #{text}")
after
  1_000 -> IO.puts("No reply within one second")
end

IO.puts("I am #{inspect(parent)}")
EXS
elixir messages.exs

Line by line:

  • self() returns the process ID (PID) of the process running this code. Even a script runs inside a BEAM process.
  • spawn(fn -> ... end) starts a new process that runs the anonymous function between fn -> and end. spawn returns at once; the new process runs alongside the first.
  • send(parent, {:greeting, "..."}) puts a message in the parent's mailbox. The message is a tuple, a fixed-size group of values in curly braces, here an atom and a string. Processes share no memory, so sending a message is the only way to pass data between them.
  • receive do ... end waits for a message that matches a pattern. {:greeting, text} matches any two-item tuple whose first item is :greeting, and binds the second item to text.
  • after 1_000 -> gives up after 1,000 milliseconds if no message arrives. The underscore in 1_000 is only there to make the number easier to read.
  • inspect(self()) turns a PID into text such as #PID<0.110.0>.

Processes are cheap enough to start thousands for a small job. Create squares.exs, which starts one process per number from 1 to 10,000, has each one square its number and send the result back, then adds up the replies:

Bash
cat > squares.exs <<'EXS'
parent = self()

for n <- 1..10_000 do
  spawn(fn -> send(parent, {:square, n * n}) end)
end

total =
  Enum.reduce(1..10_000, 0, fn _, sum ->
    receive do
      {:square, value} -> sum + value
    end
  end)

IO.puts("Sum of squares: #{total}")
EXS
elixir squares.exs
  • for n <- 1..10_000 do ... end runs its body once for each number in the range 1..10_000.
  • Enum.reduce(1..10_000, 0, fn _, sum -> ... end) runs the function 10,000 times, starting from 0. Each time it waits for one message and adds its value to the running sum. The _ means "a value I do not use". The replies can arrive in any order, and the total is the same either way.

Each process uses only a few kilobytes of memory (the Erlang efficiency guide gives 327 words, about 2.6 KB on a 64-bit machine, for a new process) and lives only as long as its one multiplication, so even 10,000 of them at once would need only about 26 MB.

Check it worked

messages.exs prints Got: Hello from #PID<...> and then I am #PID<...>, with two different PIDs: the message really came from a second process. squares.exs prints Sum of squares: 333383335000, the same answer every time you run it.

Step 7 · Fix the errors beginners hit

mix outside a project. Mix commands such as mix run and mix test look for mix.exs in the current folder. Run one from the demo folder, which has no mix.exs:

Bash
mix test

Mix stops with an error saying it could not find a Mix.Project. cd hello_app first, then run the command again.

Expecting a function to change a value. Because data is immutable, this prints ada, not ADA:

Bash
cat > immutable.exs <<'EXS'
name = "ada"
String.upcase(name)
IO.puts(name)

name = String.upcase(name)
IO.puts(name)
EXS
elixir immutable.exs

The first IO.puts prints ada, because the result of String.upcase was thrown away. The second prints ADA, because name = String.upcase(name) binds the name to the new value. The old string "ada" was never changed.

A missing end. Every do needs a matching end. Make a file without one:

Bash
cat > broken.exs <<'EXS'
for n <- 1..3 do
  IO.puts(n)
EXS
Bash
elixir broken.exs

Elixir refuses to run the file and reports something like missing terminator: end, with the line where the do opened. Add end on a new line after IO.puts(n) and it prints 1, 2 and 3.

Elixir and Erlang versions that do not match. Each Elixir release is built for a range of Erlang/OTP versions. If elixir --version warns about the OTP version, or programs fail to start, install the Elixir build made for your OTP (here 1.20.5-otp-29) and check that both PATH lines point at the same pair.

When you have finished, remove the practice folder:

Bash
cd ~
rm -rf ~/hello-elixir-demo

Check it worked

mix test in the demo folder fails with the Mix.Project error and works inside hello_app. immutable.exs prints ada and then ADA. broken.exs stops with a missing terminator error and runs once you add the end.

Next steps

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.