# How to install a dependency when a package extension is loaded?

**URL:** <https://discourse.julialang.org/t/how-to-install-a-dependency-when-a-package-extension-is-loaded/103615>\
**Category:** Package Management\
**Tags:** question, pkg, package-extensions\
**Created:** [September 7, 2023, 3:12pm UTC](https://discourse.julialang.org/t/how-to-install-a-dependency-when-a-package-extension-is-loaded/103615 "2023-09-07T15:12:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tortar](https://avatars.discourse-cdn.com/v4/letter/t/6bbea6/32.png) [@Tortar](https://discourse.julialang.org/u/Tortar)\
**Post date:** [September 7, 2023, 3:12pm UTC](https://discourse.julialang.org/t/how-to-install-a-dependency-when-a-package-extension-is-loaded/103615/1 "2023-09-07T15:12:49Z")

</div>

Say I have a `Project.toml` structure as this one:

```julia
name = "MyPackage"

[deps]
FirstDep = "..."
SecondDep = "..."

[weakdeps]
FirstWeakDep = "..."

[extensions]
ExtModule = "FirstWeakDep"

[compat]
julia = "1.9"

```

and I want that when a user does `using MyPackage, FirstWeakDep`, when the extension is loaded the first time, a new package, say `ThirdDep`, gets installed. This way, I can import `ThirdDep` in `ExtModule` even though it wasn’t previously installed when the user did `]add MyPackage` . After the first time, it shouldn’t install it again clearly, just import it.

In practice this would allow not installing `ThirdDep` when `MyPackage` gets installed but only when the extension is used the first time, which could be useful if `ThirdDep` requires a lot of time to install and it is needed just by a few users. Is there a way to do this? Should the `Project.toml` be changed somehow to achieve this?

I didn’t find anything in the documentation of `Pkg.jl` about this in the presentation of package extensions [Conditional-loading-of-code-in-packages-(Extensions)](https://pkgdocs.julialang.org/v1/creating-packages/#Conditional-loading-of-code-in-packages-(Extensions))

---

<div class="post-metadata">

**Author:** ![progtw1](https://avatars.discourse-cdn.com/v4/letter/p/c5a1d2/32.png) [@progtw1](https://discourse.julialang.org/u/progtw1)\
**Post date:** [August 4, 2025, 7:30am UTC](https://discourse.julialang.org/t/how-to-install-a-dependency-when-a-package-extension-is-loaded/103615/2 "2025-08-04T07:30:30Z")

</div>

Is there a solution to those extension conditional dependencies in the mean time?

Here is my scenario: I have an extension that is loaded when Makie.jl is loaded, which provides a specific density plot. This plot requires the `KernelDensity` package (i.e. the `ThirdDep` in above terms).

- If I put KernelDensity into `weakdeps`, my extension is only loaded if KernelDensity is also imported by the user. but I want to load my extension already if only Makie is loaded.
- If I put KernelDensity as denpendecy of the main package, KernelDensity is loaded also if Makie is not loaded, which creates some unnecessary overhead.

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [August 4, 2025, 7:38am UTC](https://discourse.julialang.org/t/how-to-install-a-dependency-when-a-package-extension-is-loaded/103615/3 "2025-08-04T07:38:28Z")

</div>

The only solution I am aware of, is to write the extension to depend on both `FirstWeakDep` and `ThirdDep` – so that turns `ThirdDep` into a weak dependency as well and the extension Reads

`ExtModuleFirstWeakDepThirdDep = ["FirstWeakDep","ThirdDep"]`

The one difference to your prescribed scenario is, that this third dependency is not loaded, when the extension is loaded but the other way around: Only if you do `using MyPackage, FirstWeakDep, ThirdDep` – then the extension is loaded.

You can of course also do this as a second extension: So if `FirstWeakDep` and `MyPackage` are loaded with `using` you have a main `ExtModule` like the one you have and you load “a bit more” when `ThirdDep` is loaded.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [August 4, 2025, 9:42pm UTC](https://discourse.julialang.org/t/how-to-install-a-dependency-when-a-package-extension-is-loaded/103615/4 "2025-08-04T21:42:09Z")

</div>

The biggest risk of letting an extension load non-trigger packages and possibly other extensions is an _ad hoc_ dependency cycle, which is an error at best and a deadlock at worst: [cycle creation issue with new weakdeps code · Issue #48533 · JuliaLang/julia](https://github.com/JuliaLang/julia/issues/48533). I don’t know if this limitation has proven to be fundamental, but there hasn’t been much movement despite demand: [Dependencies of an Extension · Issue #3641 · JuliaLang/Pkg.jl](https://github.com/JuliaLang/Pkg.jl/issues/3641). It could very well be possible that this is just another capability of packages that extensions shouldn’t have, like making importable names.

---

<div class="post-metadata">

**Author:** ![progtw1](https://avatars.discourse-cdn.com/v4/letter/p/c5a1d2/32.png) [@progtw1](https://discourse.julialang.org/u/progtw1)\
**Post date:** [August 5, 2025, 5:42am UTC](https://discourse.julialang.org/t/how-to-install-a-dependency-when-a-package-extension-is-loaded/103615/5 "2025-08-05T05:42:47Z")

</div>

Thanks for explanation and the link to the discussion.
