In elixir, How do i install packages globally?
Asked Answered
O

4

18

Can I use mix to install some packages globally? I'd like a behaviour like npm's global option or gem's install - it could be useful for packages I use everywhere like csv or yaml.

Oidea answered 5/11, 2015 at 15:8 Comment(2)
May you'd like to take a look at this: #33359276Pinero
Indeed, it is one of the biggest problems I have with Elixir (probably the biggest).Jaqitsch
C
18

There is no such thing in Elixir, you always use dependencies in the context of a project. Solutions like archives or escripts are meant to solve specific problems, they do not allow package sharing between projects.

However, there is no need to worry about sharing frequently used packages. Hex, the package manager, already cache those and it will take care of handling it for you.

Chlor answered 6/11, 2015 at 13:59 Comment(1)
I see! Thank you valiml.Oidea
F
13

Certain packages will provide an archive file that you can install globally.

http://elixir-lang.org/docs/v1.1/mix/Mix.Tasks.Archive.Install.html

For example Phoenix:

mix archive.install https://github.com/phoenixframework/phoenix/releases/download/v1.0.3/phoenix_new-1.0.3.ez

This allows access to the mix phoenix.new task globally. There is not anything specific for allowing the installation of libraries that are available in all your mix projects though.

Frothy answered 5/11, 2015 at 15:30 Comment(2)
Actual docs link here - hexdocs.pm/mix/Mix.Tasks.Archive.Install.htmlWittie
Yes, the Phoenix framework is one such archive that you can install the latest version with: mix archive.install hex phx_new. To list all your "global" archives and their versions you can use: mix archive.Barefaced
C
1

For Elixir scripts, you can virtually install global packages by using erun. By compiling erun with your dependencies, you can run

$ erun foo.exs

(erun is just an escript.)

https://github.com/s417-lama/erun

Collegiate answered 4/6, 2019 at 13:40 Comment(0)
D
0

Not global, but if you just want to use a dependency in a script outside of a Mix project, you can use Mix.install/2:

For example, this script:

Mix.install [:jason]

%{hello: "world!"}
|> Jason.encode!()
|> IO.puts()

Produces on the first run:

$ elixir script.exs
Resolving Hex dependencies...
Dependency resolution completed:
New:
  jason 1.3.0
* Getting jason (Hex package)
==> jason
Compiling 10 files (.ex)
Generated jason app
{"hello":"world!"}

Subsequent runs:

$ elixir script.exs
{"hello":"world!"}

The dependencies installed with Mix.install/2 are not global, they are cached, and each script specifies the dependencies (and the versions of which) it needs.

Deyo answered 9/6, 2022 at 12:29 Comment(1)
Elixir >= 1.12.Jaqitsch

© 2022 - 2024 — McMap. All rights reserved.