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
.
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.
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.
mix archive.install hex phx_new
. To list all your "global" archives and their versions you can use: mix archive
. –
Barefaced 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.)
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.
© 2022 - 2024 — McMap. All rights reserved.