# Setting environment and instantiating packages in one step?

**URL:** https://discourse.julialang.org/t/setting-environment-and-instantiating-packages-in-one-step/56441
**Category:** New to Julia
**Created:** [March 3, 2021, 8:43pm UTC](https://discourse.julialang.org/t/setting-environment-and-instantiating-packages-in-one-step/56441 "2021-03-03T20:43:11Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![gleyland](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gleyland/32/15339_2.png) [@gleyland](https://discourse.julialang.org/u/gleyland)
#### Post date: [March 3, 2021, 8:43pm UTC](https://discourse.julialang.org/t/setting-environment-and-instantiating-packages-in-one-step/56441/1 "2021-03-03T20:43:11Z")

</div>

Hi,

Is it possible to use a single command to activate an environment and instantiate all of its packages?

It’s been a while since I played with rust, but I seem to remember that I could check out a repo, type `cargo run` (or `cargo test`) and have it install what I needed and run (or test) the project.

I haven’t found the equivalent in julia. `julia --project=@.` is close: it seems to activate the environment, but doesn’t seem to instantiate the packages?

My best guess so far is `julia --project=@. -e"using Pkg; Pkg.instantiate()" src/main.jl`, but `-e` and running a file seem to be mutually exclusive?

Is there an obvious option I’ve missed? This is not a big deal - it’s easy to write a shell script with a `setup` and a `run` option. I would just like to check that I’m making the most of Julia’s capabilities.

Thanks in advance!  
Geoff

---

<div class="post-metadata">

### Author: ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)
#### Post date: [March 3, 2021, 8:59pm UTC](https://discourse.julialang.org/t/setting-environment-and-instantiating-packages-in-one-step/56441/2 "2021-03-03T20:59:05Z")

</div>

I’m assuming these are your packages? I usually put a `run.jl` in the root folder that usually starts with:

```julia
using Pkg

Pkg.activate(".")

include("src/main.jl")

```

So to run my package I’m just executing `julia run.jl` in the project’s folder (rather than the src folder). If the package does something long running (like start a web server) Before the activate I’ll have something like:

```julia
if @isdefined(instance) && instance !== nothing
    Package.stop(instance)
    global instance = nothing
end

```

Where `Package` is the name of the module defined in `src/main.jl`. That way during development if I re-run `run.jl` it will stop the old code before the newer code is loaded and started.

---

<div class="post-metadata">

### Author: ![gleyland](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gleyland/32/15339_2.png) [@gleyland](https://discourse.julialang.org/u/gleyland)
#### Post date: [March 3, 2021, 9:09pm UTC](https://discourse.julialang.org/t/setting-environment-and-instantiating-packages-in-one-step/56441/3 "2021-03-03T21:09:56Z")

</div>

Thanks!

The `include` trick makes it work.

Yes, these are our packages. We use a similar pattern, but with a shell script, which is now, thanks to your response:

```julia
#!/bin/bash
case $1 in
    run)
        julia --project=@. -e"using Pkg; Pkg.instantiate(); include(\"src/main.jl\")" -- -l debug
        ;;
    # other options
esac

```

So you can just type `./admin run` and it should “just work”. What’s nice here is that the command-line options work!

Thanks again!  
Geoff

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [March 3, 2021, 9:44pm UTC](https://discourse.julialang.org/t/setting-environment-and-instantiating-packages-in-one-step/56441/4 "2021-03-03T21:44:19Z")

</div>

> [@gleyland](#):
>
> So you can just type `./admin run` and it should “just work”. What’s nice here is that the command-line options work!

This just works except for one corner case: if the user should happen to already have a copy of the General registry, which is outdated enough to completely miss one of the packages in the Manifest.

---

<div class="post-metadata">

### Author: ![gleyland](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gleyland/32/15339_2.png) [@gleyland](https://discourse.julialang.org/u/gleyland)
#### Post date: [March 3, 2021, 10:35pm UTC](https://discourse.julialang.org/t/setting-environment-and-instantiating-packages-in-one-step/56441/5 "2021-03-03T22:35:25Z")

</div>

Thanks! Is there a solution for that case?

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [March 3, 2021, 10:43pm UTC](https://discourse.julialang.org/t/setting-environment-and-instantiating-packages-in-one-step/56441/6 "2021-03-03T22:43:10Z")

</div>

You can do a `Pkg.Registry.update()` before instantiate, but it’s a tradeoff with speed.

---

<div class="post-metadata">

### Author: ![gleyland](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gleyland/32/15339_2.png) [@gleyland](https://discourse.julialang.org/u/gleyland)
#### Post date: [March 3, 2021, 11:37pm UTC](https://discourse.julialang.org/t/setting-environment-and-instantiating-packages-in-one-step/56441/7 "2021-03-03T23:37:31Z")

</div>

Oh, thanks!

Maybe a try-catch around the `instantiate`? But what I have hits the 80/20 rule, so I’m pretty happy with it.
