Setting up an Elixir project with and without Phoenix

Creating a barebones elixir project

For this I’m going to assume you have Elixir and Erlang installed as well as the Phoenix framework if that's what you want to use. You will also have to install node.js and Postgres the default database for Ecto. I won't be covering this here as those topics have been covered in more detail and by far better than I.

Elixir comes with a lovely built in tool called `mix` this allows you to do anything from formatting your project to running the compiler. It’s also used to create a new project. Yet it is quite confusing all the flags you can pass to mix to get it to include or not include certain things.

```mix new application_name```

```mix new application_other_name```

As you can see this is how you create the very basic project. You run mix new then you give it a name for the project and your away.

Which results in the following output

Screen Shot 2018-06-10 at 3.56.24 pm.png

You can run the two commands it tells you to run: “cd test_project” which will switch you into the directory then run “mix test” which will run all the tests for the project.

Screen Shot 2018-06-10 at 3.59.13 pm.png

With that you have set up your project with the basic Elixir. This creates a completely barebones project. How easy was that? You can immediately start messing around with Elixir.

 

Creating a project with Phoenix and Ecto

Creating a Phoenix project is extremely easy, very similar to the base Elixir setup, except you use the command “mix phx.new test_project” rather than “mix new”. Which gives you the following, it will ask you if you want to install the dependencies via node, hence why you have to have node installed. Say yes, otherwise your project won’t run.

Screen Shot 2018-06-10 at 4.13.56 pm.png

When that has completed run “cd test_project” to move into your new project. Make sure you have Postgres installed for this next bit as this is where you create the database.

Run “mix ecto.create” this creates the database in Postgres, without this you won’t have any database to talk to, not that, that really matters at this point as you have no tables. Yet you will have to do it as your project won’t run, because it needs to connect to the database even if there’s no data in it.

Screen Shot 2018-06-10 at 4.29.45 pm.png

Once you have created the database, you can run “mix phx.server” this runs the Phoenix server that will run your application.

Screen Shot 2018-06-10 at 4.30.50 pm.png

There you go, Phoenix is up and running on localhost 4000.

Screen Shot 2018-06-10 at 4.31.24 pm.png

You have just created an Elixir project that has Phoenix and Ecto, give you a base to create all sorts of applications.

Now there are a number of flags you can pass to the “mix phx.new” command, these do various things as I’ll discuss.


Flags

Add any of these flags to the end of the “mix phx.new”

--no-ecto

This will not include ecto in your project, this could be useful for a project that doesn’t need to talk to a database, such as a pure API

--no-brunch

Brunch is an asset build tool

--no-context

This will not create a context and schema for your resources

--no-schema

This won’t create a schema, but will still create a context.

All of these flags are important in their own right, but honestly if your using Phoenix there's a good chance you won’t need any of them, I just include them to let you know what is possible when creating a project

Previous
Previous

Elixir connecting to databases part: 1

Next
Next

Testing in Elixir