Your first Elixir Project (Part 0)

Your first Elixir Project (Part 0)

Have you heard of Elixir, the incredibly scalable functional programming language that developers love, but haven't gotten around to trying it out?

If this is the first you've heard of the language, check out my previous post where I give an overview of elixir.

I'm writing this tutorial series as a way to introduce other developers to the language and get them up-and-running with a simple project.

Overview

In this post, you will...

  • Install Elixir
  • Use mix to create a project template
  • Understand the basic structure of an elixir project

After following all parts of this tutorial, you will have created a project that can convert between different units of measurement.

Installation

The first thing we need to do is install elixir. If you use a package manager, the process is as simple as running a command.

  • homebrew —> brew install elixir
  • alpine linux —> apk add elixir
  • chocolatey —> cinst elixir

If your package manager isn't listed here or you run into trouble, click here for the official installation instructions.

Creating the project template

When writing code in elixir you'll make frequent use of the build automation tool mix, which you should have access to after installing elixir.

  1. Open your terminal and navigate to the directory you'd like to work in.
  2. Run mix new unit_converter (You can think of this like npm init or create-react-app or ng new. This command creates the basic scaffolding for you to build a project in elixir)
  3. Run ls. You should now see the unit_converter directory.
  4. Open the unit_converter directory in your favorite code editor. If you use vscode, you can do this by running code unit_converter

The structure of your project should look something like this:

├── .formatter.exs
├── .gitignore
├── README.md
├── lib
|  └── unit_converter.ex
├── mix.exs
└── test
   ├── test_helper.exs
   └── unit_converter_test.exs

Now let's take a tour of the files that were generated...

If you want to jump straight into writing code, you can skip to the next part of this tutorial for now and come back here if you have questions later on

  • lib/unit_converter.ex —> This file contains your UnitConverter module and is where we will be writing most of our code for this tutorial. If you open it up, you should see an autogenerated hello world function.
  • README.md —> A markdown file for writing documentation. When you share code with others (through GitHub or other means) this is usually the first file that people will see.
  • mix.exs —> This file defines information about your project (such as the project name and the elixir version). It also includes other information such as your list of dependencies. Think of it like the package.json file in a node project.
  • .gitignore —> Provides git with a list of files and directories that should not be tracked. If you take a peek at this file, you can see the names of files and directories that might be generated later on (elixir provides some helpful comments for each file or directory being ignored)
  • test/test_helper.exs —> When testing your code with ExUnit, this file is run before your tests are executed. The autogenerated file just starts ExUnit, but for more complex testing workflows you can add more code to this file.
  • test/unit_converter_test.exs —> This is where your test cases for the UnitConverter module will live. There should be an autogenerated test case for the hello world function in this file. As we write code in this tutorial, we will be adding test cases for other functions.
  • .formatter.exs —> This file allows you to configure the auto-formatting of your code. We will take advantage of the auto-formatting in this tutorial, but we probably won't be making any modifications to this file.

Onward!

Now that you have your project template and understand it's structure, it's time to move on to part 1 of this tutorial and start writing some code.

More content

If you liked this, consider subscribing to my newsletter or reading one of my other posts: