โ† Back to All Posts

Getting Started with Mix ๐Ÿ› ๏ธ

June 18, 2024
Tutorial
Beginner
Mix

As a budding Elixir developer, understanding the powerful tool called Mix is crucial to your success. Mix is Elixir's build tool, similar to Rake for Ruby or Make for C, and it's the go-to utility for managing your development tasks. In this comprehensive guide, I'll walk you through the ins and outs of Mix, from creating your first Elixir project to running tests and managing dependencies.

Step 1: Creating Your First Elixir Project ๐Ÿ†•

Let's dive right in and create our first Elixir project using Mix. Open your terminal, navigate to your desired directory (I'll use the desktop for this example), and run the following command:

mix new blork

This command will generate a new Elixir project called "blork" with the necessary files and directories. The three key files we'll focus on are:

โ€ข mix.exs: This file describes your project, including its name, version, and dependencies.

โ€ข lib/blork.ex: This is the main source file for your Elixir application, where you'll write your code.

โ€ข test/blork_test.exs: This file contains the tests for your application, which we'll explore in more detail later.

Step 2: Exploring the Project Structure ๐Ÿ”

Now that we have our project set up, let's take a closer look at the files and directories that Mix has created for us.

The mix.exs File

The mix.exs file is the heart of your Elixir project. It defines the project's name, version, and the Elixir version it requires. Most importantly, it's where you'll manage your project's dependencies.

For example, let's say we want to add the popular jason library to our project. We can do so by adding the following line to the deps function:

{:jason, "~> 1.3"}

Once you've saved the file, you can install the new dependency by running mix deps.get in your terminal.

The blork.ex File

The lib/blork.ex file is where you'll write the core of your Elixir application. This file includes a sample module and function to help you get started. Let's take a closer look:

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

	@doc """
	Hello world.

	## Examples

		iex> Blork.hello()
		:world

	"""
	def hello do
		:world
	end
end

This code defines a module called Blork with a single function, hello/0, that returns the atom :world. The @moduledoc and @doc attributes provide documentation for the module and function, respectively.

The blork_test.exs File

The test/blork_test.exs file contains the tests for your Elixir application. Mix automatically generates a sample test for the hello/0 function, which you can use as a starting point for your own tests.

defmodule BlorkTest do
	use ExUnit.Case
	doctest Blork

	test "greets the world" do
		assert Blork.hello() == :world
	end
end

This test checks that the hello/0 function returns the atom :world. You can add more tests as you develop your application to ensure its correctness and reliability.

Step 3: Interacting with Mix ๐Ÿค–

Mix provides a variety of commands that you can use to interact with your Elixir project. Let's explore a few of the most useful ones:

Running the Interactive Elixir Shell

To start the interactive Elixir shell (IEx) and load your project's code, run the following command in your terminal:

iex -S mix

This will compile your project and start the IEx shell, allowing you to test your functions and explore the available modules and functions.

Compiling Your Project

To compile your Elixir project, simply run:

mix compile

This command will check your code for errors and generate the necessary compiled files.

Running Tests

To run the tests for your Elixir project, use the following command:

mix test

This will execute all the tests defined in your test/blork_test.exs file and report the results.

Step 4: Mastering Mix Commands ๐Ÿง 

Mix offers a wide range of commands to help you manage your Elixir project. Here are a few more that you'll find useful:

mix help #Displays a list of all available Mix commands and their descriptions.
mix deps #Manages your project's dependencies, including installing, updating, and removing them.
mix run #Runs your Elixir application.
mix format #Automatically formats your Elixir code according to the community-accepted style guide.
mix phx.new #Creates a new Phoenix web application, which is a popular Elixir framework.

As you continue to work with Elixir, you'll become more familiar with these Mix commands and discover new ones that fit your specific needs.

Conclusion: Embracing the Power of Mix ๐Ÿš€

In this guide, you've learned the fundamentals of Elixir's Mix tool, from creating a new project to managing dependencies and running tests. Mix is a powerful tool that will be your constant companion as you build Elixir applications. By mastering Mix, you'll be well on your way to becoming a proficient Elixir developer.

Remember, the key to success with Elixir is practice, experimentation, and a willingness to learn. Keep exploring, building, and testing, and you'll be writing robust, scalable Elixir applications in no time. Happy coding! ๐ŸŽ‰