Thread overview
How do I install a package globally?
Nov 11
Trevor
Nov 11
Trevor
Nov 12
bachmeier
Nov 11
Sergey
November 11

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?

November 11

On Saturday, 11 November 2023 at 01:50:54 UTC, Trevor wrote:

>

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?

Dub currently isn't able to do this. There's been some discussion about adding a dub install command, but it seems like no consensus was reached:

https://github.com/dlang/dub/issues/839

November 11

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

November 11

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?

And honestly it is not the best behavior. This is why in python world so popular solutions with conda/pip virtual environments.

November 11

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?

November 12

On Saturday, 11 November 2023 at 23:28:18 UTC, Trevor wrote:

>

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?

You can download the package to your computer, for instance by cloning the repo, and add a symbolic link to your project's repo. Then you can compile with the -i option and forget about Dub. That's how I do it (though depending on the project, I might do a full copy of the dependencies into the project repo to guarantee they're always available).

November 13

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

November 14

On Saturday, 11 November 2023 at 23:28:18 UTC, Trevor wrote:

>

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?

Dub has a local cache, so multiple projects don't download and take up additional space. There is also the --system option if you want to share cache with other users on the machine.