How do I install a package globally?

Christian Köstlin christian.koestlin at gmail.com
Sat Nov 11 07:12:21 UTC 2023


On Saturday, 11 November 2023 at 01:50:54 UTC, Trevor wrote:
> I'm just getting in to D , coming from a C and Python 
> background. I've had a play with DUB and adding packages to my 
> project, but it seems like there should be a way to install 
> packages so they can be used in any D program I compile without 
> creating a whole package. For example in Python you can just go 
> "pip install abc" and then any script can use abc.
>
> How does one install packages globally, and how can I write 
> programs that use the third-party packages without being 
> wrapped in a dub file?

There is no way to install a dependency for all your d programs 
globally.
In general you also should be careful about which dependencies 
you pull in and control them tightly.

In general the approach taken by dub is slightly different.
The dependencies you are using in all your programs are put to 
`$HOME/.dub/packages/...` and are in theory available for all 
your programs, but you still have to tell dub, that you want to 
use one of them. This is similar to python in that, pip puts your 
dependencies somewhere in your `$PYTHONHOME` or `$PYTHONPATH` (I 
am not 100% sure about that).
You can tell dub (and dub then tells the compiler where to find 
the dependencies) in two way:
- Create a complete dub project with dub.json/sdl and add the 
dependency there (e.g. with `dub add` or while creating the 
project or by adding them in your editor to the dub.json/sdl)
- Create a self executing d file (that is run when you just call 
it like a script and compiled on the fly). See 
https://dub.pm/advanced_usage#single-file for details.

Another way dub is able to "install" something is by using `dub 
fetch` to pull a package with a binary in it (e.g. `dub fetch 
dfmt`). You then can run this executable by `dub run` (e.g. `dub 
run dfmt -- ... here go the arguments to the dfmt tool).

Kind regards,
Christian




More information about the Digitalmars-d-learn mailing list