Adding dependencies
Adding Dependencies
Let’s add a dependency. We’ll use a package called cowboy, an HTTP server for erlang/elixir and one called plug, a specification tool and connection adapter for web servers.
Let’s open up our Mixfile and add our dependencies. But first let’s look up the latest version for each dependency
Type the function
mix hex.info cowboy
defmodule MyApp.Mixfile do
# ...
defp deps do
[
{:cowboy, "~> 2.6"},
{:plug, "~> 1.7"},
{:plug_cowboy, "~> 2.0"}
]
end
end
How do we figure out what version to use? The command mix hex.info #{dependency}
will get info from the central package repo, called hex.
$ mix hex.info.cowboy
Small, fast, modular HTTP server.
Config: {:cowboy, "~> 2.3"}
Releases: 2.3.0, 2.2.2, 2.2.1, 2.2.0, 2.1.0, 2.0.0, 1.1.2, 1.1.1, ...
Maintainers: Loïc Hoguin
Licenses: ISC
Links:
GitHub: https://github.com/ninenines/cowboy
You can also go to the Hex site and browse packages: hex.pm.
Installing Dependencies
Mix has a build in command for installing dependencies, mix deps.get
. Let’s pull our dependencies we’ve just added:
$ mix deps.get
Resolving Hex dependencies...
Dependency resolution completed:
New:
cowboy 2.6.1
cowlib 2.7.0
mime 1.3.1
plug 1.7.1
plug_crypto 1.0.0
ranch 1.7.1
* Getting cowboy (Hex package)
* Getting plug (Hex package)
* Getting mime (Hex package)
* Getting plug_crypto (Hex package)
* Getting cowlib (Hex package)
* Getting ranch (Hex package)
You’ll see that it fetches your dependencies, and also the packages that they depend on.
Running Dependencies
For dependencies to work, some of them need to be included in the list of applications in your mix.exs
. We’ll change the definition of application/0
:
# mix.exs
def application do
[extra_applications: [:logger, :cowboy, :plug]]
end
Now, when the application is started, it will also start :cowboy
and :plug
.
Next step:
Go on to Testing.
Or:
Go back to Mix.