# How to run a Julia script from the command line which has package dependencies?

**URL:** <https://discourse.julialang.org/t/how-to-run-a-julia-script-from-the-command-line-which-has-package-dependencies/120813>\
**Category:** New to Julia\
**Created:** [October 2, 2024, 1:52pm UTC](https://discourse.julialang.org/t/how-to-run-a-julia-script-from-the-command-line-which-has-package-dependencies/120813 "2024-10-02T13:52:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon91151494](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@anon91151494](https://discourse.julialang.org/u/anon91151494)\
**Post date:** [October 2, 2024, 1:52pm UTC](https://discourse.julialang.org/t/how-to-run-a-julia-script-from-the-command-line-which-has-package-dependencies/120813/1 "2024-10-02T13:52:28Z")

</div>

I’m trying to run a Julia script like an executable program from the command line.

```julia
$ julia myscript.jl

```

If this script has dependencies on packages, such as the `Distributions` package, how can I run such a script from the command line?

This is how I have setup my _local_ environment and installed packages.

```julia
(@v1.10) pkg> activate test-env
(test-env)> add Distributions
(test-env)> CTRL^C
> CTRL^D
$ julia myscript.jl
ERROR: LoadError: ArgumentError: Package Distributions not found in current path.
- Run `import Pkg; Pkg.add("Distributions")` to install the Distributions package.

```

I’m not sure how to “connect” the two together?

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [October 2, 2024, 2:14pm UTC](https://discourse.julialang.org/t/how-to-run-a-julia-script-from-the-command-line-which-has-package-dependencies/120813/2 "2024-10-02T14:14:17Z")

</div>

```julia
julia --project=test-env myscript.jl

```

---

<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:** [October 2, 2024, 2:36pm UTC](https://discourse.julialang.org/t/how-to-run-a-julia-script-from-the-command-line-which-has-package-dependencies/120813/3 "2024-10-02T14:36:31Z")

</div>

Another option is to start the script with

```julia
using Pkg
Pkg.activate(@ __DIR__ )
Pkg.instantiate()

```

Compared to using the `--project` flag this has the disadvantage that the global environment is initially active and if you have a startup file which loads packages, you could get wrong versions loaded before activating the script environment.

The big advantage obviously, is that you don’t have to type the `--project` specification every time you run the script.

Notes:

- Line 2 assumes that the environment is in the same directory as the script. If that is not the case, you need to adjust it accordingly.
- Line 3 mostly matters if you, or someone else, wants to run the script on another computer and just have it work, even if the dependencies have not been previously installed.

---

<div class="post-metadata">

**Author:** ![anon91151494](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@anon91151494](https://discourse.julialang.org/u/anon91151494)\
**Post date:** [October 2, 2024, 3:04pm UTC](https://discourse.julialang.org/t/how-to-run-a-julia-script-from-the-command-line-which-has-package-dependencies/120813/4 "2024-10-02T15:04:53Z")

</div>

> [@GunnarFarneback](#):
>
> Another option is to start the script with

To check I understand this correctly - you would add these three lines at the top of a Julia script and then call that script from the command line as normal?

---

<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:** [October 2, 2024, 3:10pm UTC](https://discourse.julialang.org/t/how-to-run-a-julia-script-from-the-command-line-which-has-package-dependencies/120813/5 "2024-10-02T15:10:01Z")

</div>

Yes.
