Elixir 101

Elixir 101

ยท

3 min read

Elixir is a modern, functional programming language that is built on top of the Erlang Virtual Machine (BEAM). It is known for its robust concurrency model, which makes it an ideal choice for building high-performance, concurrent, and distributed systems.

This is the first blog post of this series on elixir and what better way to start than to outline what you would need and the steps required to bootstrap an elixir project from scratch

Installing Elixir and Hex

The first step in starting an Elixir project is to install the Elixir programming language and its package manager, Hex. You can download the latest version of Elixir from the official website (elixir-lang.org/install.html) and follow the installation instructions for your operating system.

For macOS using homebrew:

brew install elixir

Once you have Elixir installed, you can use the following command to check the version of Elixir and Hex that are currently installed on your machine:

elixir -v
hex -v

Creating a new Elixir project using Mix

The next step is to create a new Elixir project using the mix tool. mix is a build tool and task runner for Elixir projects, and it can be used to create new projects, manage dependencies, and run tasks such as tests and releases.

To create a new Elixir project, use the following command in your terminal:

mix new my_project

This will create a new directory with the name my_project and generate some basic structure for your project. The generated files include an application file, a test file and a config file.

You can change into the project directory by:

cd my_project

Adding dependencies to the project

Once you have created the new project, you can start adding dependencies to it. Dependencies in Elixir are managed using Hex, and you can add them to your project by editing the mix.exs file in the root of your project.

In the mix.exs file, you will see a block of code that looks like this:

defp deps do
  [
    {:some_dependency, "~> 1.0"}
  ]
end

You can add new dependencies by adding them to this list, for example:

defp deps do
  [
    {:some_dependency, "~> 1.0"},
    {:another_dependency, "~> 2.0"}
  ]
end

After adding new dependencies, you can run the following command to download and install them:

mix deps.get

Once you have set up your project and added dependencies, you can start building and running your application. The mix tool provides several commands for running tests, building releases, and starting the application.

To run tests, you can use the following command:

mix test

To start the application, you can use the following command:

mix run --no-halt

IEx

This is elixir's interactive shell, you can run the following at the root of your project:

iex -S mix

Conclusion

In this blog post, we have gone over the steps required to start a new Elixir project.

ย