How do I install a package globally?

Christian Köstlin christian.koestlin at gmail.com
Mon Nov 13 15:12:51 UTC 2023


On Saturday, 11 November 2023 at 23:28:18 UTC, Trevor wrote:
> On Saturday, 11 November 2023 at 07:12:21 UTC, Christian 
> Köstlin wrote:
>> 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
>
> Thanks for the detailed reply. I guess what I'd like to do is 
> not create a DUB package for every little project I work on. It 
> seems like most modern languages require a package/dependency 
> manager though. Being able to install libraries globally would 
> avoid this but I can how that can cause it's own set of issues. 
> It doesn't seem efficient in terms of bandwidth and hard disk 
> space to have a new copy of a library for each project that 
> uses it?

Did you try https://dub.pm/advanced_usage#single-file for your 
usecase.
This one installs the dependencies once in `$HOME/.dub` and 
compiles them on the fly.

- For small projects this is I think quite nice.
- For medium projects I like how dub works at the moment with a 
`dub.sdl/json` in the projects folder and the shared cache of 
dependencies in `$HOME/.dub`.
- For really big problems I would like to have a way to install 
the dependencies really locally to the project (just to have more 
control over them, e.g. like ruby's `bundle package` command 
works).

Kind regards,
Christian


More information about the Digitalmars-d-learn mailing list