# Pkg.instantiate() after activating project?

**URL:** https://discourse.julialang.org/t/pkg-instantiate-after-activating-project/36603
**Category:** General Usage
**Tags:** package
**Created:** [March 27, 2020, 3:48pm UTC](https://discourse.julialang.org/t/pkg-instantiate-after-activating-project/36603 "2020-03-27T15:48:58Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)
#### Post date: [March 27, 2020, 3:48pm UTC](https://discourse.julialang.org/t/pkg-instantiate-after-activating-project/36603/1 "2020-03-27T15:48:58Z")

</div>

I run code on a cluster (using slurm). The `sbatch` file starts Julia in the package directory. For example:

`julia --project="." --startup-file=no "longleaf/run_simple.jl"`

Occasionally, the jobs crash with errors complaining that dependencies are not installed (even though they were when I ran the same code an hour ago). An example error message:

```julia
ERROR: LoadError: ArgumentError: Package Optim [429524aa-4258-5aef-a3af-852621145aeb] is required but does not seem to be installed:
 - Run `Pkg.instantiate()` to install all recorded dependencies.

Stacktrace:
 [1] _require(::Base.PkgId) at ./loading.jl:993
 [2] require(::Base.PkgId) at ./loading.jl:922
 [3] require(::Module, ::Symbol) at ./loading.jl:917
 [4] include at ./boot.jl:328 [inlined]
 [5] include_relative(::Module, ::String) at ./loading.jl:1105
 [6] include(::Module, ::String) at ./Base.jl:31
 [7] top-level scope at none:2
 [8] eval at ./boot.jl:330 [inlined]
 [9] eval(::Expr) at ./client.jl:425
 [10] top-level scope at ./none:3
[...]

```

I have tried to work around this by running `Pkg.instantiate()` at the very top of the Julia file that `slurm` calls (here: `run_simple.jl`). But this does not seem to work, perhaps because my package is `used` before the Julia file is run (by the `--project` switch).

How can I work around this problem?

---

<div class="post-metadata">

### Author: ![mfh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mfh/32/13787_2.png) [@mfh](https://discourse.julialang.org/u/mfh)
#### Post date: [March 27, 2020, 4:26pm UTC](https://discourse.julialang.org/t/pkg-instantiate-after-activating-project/36603/2 "2020-03-27T16:26:19Z")

</div>

My guess would be that you have an old `Manifest.toml` in your project’s directory. Since `Pkg.instantiate()` does not update the `Manifest.toml` afaik, it will in this case install an outdated dependency tree (e.g. corresponding to an earlier version of the `Projects.toml`).  
To avoid this, try to run `Pkg.resolve()` before `Pkg.instantiate()`. This rebuilds your Manifest according to potentially changed dependencies in `Project.toml` first and then installs the dependencies.

**Note:** This might, however, not be what you want if you want to make sure that the packages installed on the cluster are _exactly_ identical to the ones you are using locally on a laptop. In this case make sure to commit an up-to-date `Manifest.toml` (along with the `Project.toml` and the other julia files) into a git repo, which you use to keep the calculations on the cluster in sync with your laptop for example. That is my typical workflow 😄.

---

<div class="post-metadata">

### Author: ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)
#### Post date: [March 27, 2020, 4:42pm UTC](https://discourse.julialang.org/t/pkg-instantiate-after-activating-project/36603/3 "2020-03-27T16:42:52Z")

</div>

Thank you for the suggestion.

I upload `Manifest.toml` together with all the `.jl` files before each run. So my understanding is that `Pkg.resolve()` is therefore not needed (?). In fact, I may not want to run `resolve()` to keep dependency version unchanged (as the second part of your answer suggests).

What I’m wondering is: when I start Julia with `--project="."`, does it implicitly run `using MyPackage` before `instantiate()` or `resolve()` ever have a chance to run?

---

<div class="post-metadata">

### Author: ![mfh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mfh/32/13787_2.png) [@mfh](https://discourse.julialang.org/u/mfh)
#### Post date: [March 27, 2020, 4:54pm UTC](https://discourse.julialang.org/t/pkg-instantiate-after-activating-project/36603/4 "2020-03-27T16:54:17Z")

</div>

If locally the `Manifest.toml` is in line with your `Project.toml` and the `Project.toml` contains all dependencies you require in your code, than what you describe should work. Maybe, however, you are `using` a package, which is not contained in the `Manifest.toml`. Could you check if you have a reference to `Optim` in your `Manifest.toml` to rule that out?

To my understanding `--project="." ` is equivalent to plainly starting julia and then calling `Pkg.activate(".")`. So no `using` of your package.

---

<div class="post-metadata">

### Author: ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)
#### Post date: [March 27, 2020, 4:58pm UTC](https://discourse.julialang.org/t/pkg-instantiate-after-activating-project/36603/5 "2020-03-27T16:58:36Z")

</div>

Here is where truncating the stacktrace comes back to bite me.  
I am not directly using `Optim`; it only appears as a dependency of `NLopt` (which is in `Project.toml`). `Optim` does, however, appear in `Manifest.toml`.

Thanks again.

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [January 20, 2022, 8:35pm UTC](https://discourse.julialang.org/t/pkg-instantiate-after-activating-project/36603/6 "2022-01-20T20:35:39Z")

</div>

> [@mfh](#):
>
> To my understanding `--project="." ` is equivalent to plainly starting julia and then calling `Pkg.activate(".")` . So no `using` of your package.

So `--project="." ` doesn’t instantiate the Manifest? It only activates the dir project?

Then I guess that the workflow would be `julia --project=. myscript.jl`, and within `myscript.jl` the first line (before any other `using/import`) would have to be `using Pkg; Pkg.instantiate()`

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [January 20, 2022, 8:53pm UTC](https://discourse.julialang.org/t/pkg-instantiate-after-activating-project/36603/7 "2022-01-20T20:53:12Z")

</div>

> [@e3c6](#):
>
> So `--project="." ` doesn’t instantiate the Manifest? It only activates the dir project?

Yes, that’s correct.
