# Enable multiple Cores for Jupyter Lab

**URL:** <https://discourse.julialang.org/t/enable-multiple-cores-for-jupyter-lab/18658>\
**Category:** Julia at Scale\
**Tags:** jupyter\
**Created:** [December 14, 2018, 2:36pm UTC](https://discourse.julialang.org/t/enable-multiple-cores-for-jupyter-lab/18658 "2018-12-14T14:36:30Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Post date:** [December 14, 2018, 2:36pm UTC](https://discourse.julialang.org/t/enable-multiple-cores-for-jupyter-lab/18658/1 "2018-12-14T14:36:30Z")

</div>

I have found some resources on this online, but nothing which I actually is able to understand fully.

I want to enable multiple cores when I run JupyterLab (also notebooks).

This link ([Setting JULIA\_NUM\_THREADS in an IJulia kernel](https://discourse.julialang.org/t/setting-julia-num-threads-in-an-ijulia-kernel/8301)) tells be that I am supposed to set  
`ENV["JULIA_NUM_THREADS"] = 4`  
However, where and how? This seems to be an environmental variable. I can read about these here [Environment Variables · The Julia Language](https://docs.julialang.org/en/v1/manual/environment-variables/index.html#Parallelization-1). However, either that page is not supposed to tell me what I need to know, or it fails to do so. Finally it seems like the juliarc.jl file might be interesting.

However, I am still not sure where to find it and how to change it (and a confirmation whenever this is the one I am looking for). This link seems to contain something ([Update docs with new startup.jl file and location · Issue #26215 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/26215)), however, my .julia folder does not contain a config folder.  
(I tried simply creating one, but a juliarc.jl file in it with the single line `ENV["JULIA_NUM_THREADS"] = 4`, did not work).

Finally, this link gives me more info ([Where is juliarc (the Julia startup file)? - Stack Overflow](https://stackoverflow.com/questions/52175314/where-is-juliarc-the-julia-startup-file)). I can find the startup.jl file in the install folder (for julia-1.0.0, will this change when I update to e.g. julia-1.0.3. If so, will I have to redo stuff?). The startup.jl file looks like this:

```julia
# This file should contain site-specific commands to be executed on Julia startup;
# Users may store their own personal commands in `~/.julia/config/startup.jl`.
import Pkg

```

Is it this I should make changes to, or the non-existing “local” file in .julia/config?

Am I right that

1. There is some file, somewhere, to which I must make a change to enable multiple cores in Juyter (preferable without specifying something every time I start Jupyter). If so, where is this file?
2. In that file I should add a line `ENV["JULIA_NUM_THREADS"] = 4` (possibly replacing `4` with another integer, depending on how many cores I want to use).

Thanks

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [December 15, 2018, 2:01am UTC](https://discourse.julialang.org/t/enable-multiple-cores-for-jupyter-lab/18658/2 "2018-12-15T02:01:05Z")

</div>

Just open a REPL and type:

```julia
ENV["JULIA_NUM_THREADS"] = 4
using IJulia
notebook()

```

This will open a Jupyter session with 4 threads. Or if you want to open Jupyter with `jupyter notebook` in the terminal or Git bash, then use:

```julia
export JULIA_NUM_THREADS=4
jupyter notebook

```

---

<div class="post-metadata">

**Author:** ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Post date:** [December 17, 2018, 11:25am UTC](https://discourse.julialang.org/t/enable-multiple-cores-for-jupyter-lab/18658/3 "2018-12-17T11:25:37Z")

</div>

Thanks.

Is there a way to “permanent-ise” this, so that I do not have to type that every time I open jupyter?

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [December 17, 2018, 12:38pm UTC](https://discourse.julialang.org/t/enable-multiple-cores-for-jupyter-lab/18658/4 "2018-12-17T12:38:12Z")

</div>

Set an environment variable in your OS.

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [December 17, 2018, 12:38pm UTC](https://discourse.julialang.org/t/enable-multiple-cores-for-jupyter-lab/18658/5 "2018-12-17T12:38:33Z")

</div>

There are a few ways:

(1) A simple way to do this is to put `export JULIA_NUM_THREADS=4` in your `~/.bashrc` or whatever the shell configuration you need to use.

(2) Another way to do this is to put `ENV["JULIA_NUM_THREADS"] = 4` in `~/.julia/config/startup.jl`. If `~/.julia/config` does not exist, just try create one. However, note that:

> Some variables, such as `JULIA_NUM_THREADS` and `JULIA_PROJECT`, need to be set before Julia starts, therefore adding these to `~/.julia/config/startup.jl` is too late in the startup process.  
> — [Environment Variables · The Julia Language](https://docs.julialang.org/en/v1.2-dev/manual/environment-variables/)

For this reason, I don’t recommend this method. However, if you invoke `IJulia.notebook()` from standard REPL then `ENV["JULIA_NUM_THREADS"]` can still take an effect for the Julia processes _inside_ Jupyter notebook, but not the Julia process invoking `IJulia.notebook()`.

(3) If you want to set `JULIA_NUM_THREADS` _only_ for Julia process inside Jupyter notebook, you can edit the kernel spec `~/.local/share/jupyter/kernels/julia-1.0/kernel.json` to something like

```json
{
  "display_name": "Julia 1.0.0",
  "argv": [
    "env", # add this
    "JULIA_NUM_THREADS=4", # add this
    "/PATH/TO/bin/julia",
    "-i",
    "--startup-file=yes",
    "--color=yes",
    "/home/USERNAME/.julia/packages/IJulia/0cLgR/src/kernel.jl",
    "{connection_file}"
  ],
  "language": "julia"
}

```

It’s handy (say) if you want to add Julia kernel with different number of threads. For example, create `~/.local/share/jupyter/kernels/julia-1.0-one-thread/kernel.json` with

```json
{
  "display_name": "Julia 1.0.0 (one thread)",
  "argv": [
    "env",
    "JULIA_NUM_THREADS=1",
    "/PATH/TO/bin/julia",
    ...

```

Actual path to `kernel.json` depends on OS. See also [Making kernels for Jupyter — jupyter\_client 8.6.0 documentation](https://jupyter-client.readthedocs.io/en/latest/kernels.html#kernelspecs)

---

<div class="post-metadata">

**Author:** ![Juan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juan/32/7657_2.png) [@Juan](https://discourse.julialang.org/u/Juan)\
**Post date:** [December 17, 2018, 4:51pm UTC](https://discourse.julialang.org/t/enable-multiple-cores-for-jupyter-lab/18658/6 "2018-12-17T16:51:54Z")

</div>

You can just create that variable on your system.  
I did it on Windows 10 (environment variable) and it works.

---

<div class="post-metadata">

**Author:** ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Post date:** [December 17, 2018, 5:09pm UTC](https://discourse.julialang.org/t/enable-multiple-cores-for-jupyter-lab/18658/7 "2018-12-17T17:09:05Z")

</div>

Thanks for all the help everyone. This is useful.

---

<div class="post-metadata">

**Author:** ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Post date:** [April 10, 2020, 7:04pm UTC](https://discourse.julialang.org/t/enable-multiple-cores-for-jupyter-lab/18658/8 "2020-04-10T19:04:23Z")

</div>

For future reference, writing `export JULIA_NUM_THREADS=4` into the terminal also seems to work.

---

<div class="post-metadata">

**Author:** ![dashanan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dashanan/32/18909_2.png) [@dashanan](https://discourse.julialang.org/u/dashanan)\
**Post date:** [October 23, 2020, 8:55pm UTC](https://discourse.julialang.org/t/enable-multiple-cores-for-jupyter-lab/18658/9 "2020-10-23T20:55:44Z")

</div>

I have used this one

```julia
using IJulia
installkernel("Julia (n threads)", env=Dict("JULIA_NUM_THREADS"=>"n"))

```

n is the number of threads you want to use, I m using 8 you can increase it to whatever number you want

---

<div class="post-metadata">

**Author:** ![teriiju](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/teriiju/32/19029_2.png) [@teriiju](https://discourse.julialang.org/u/teriiju)\
**Post date:** [November 2, 2020, 6:55pm UTC](https://discourse.julialang.org/t/enable-multiple-cores-for-jupyter-lab/18658/10 "2020-11-02T18:55:49Z")

</div>

correct, sorry for that, thank you, once again, pointing us to a superquick solution !  
🤗

---

<div class="post-metadata">

**Author:** ![lewis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lewis/32/5217_2.png) [@lewis](https://discourse.julialang.org/u/lewis)\
**Post date:** [September 21, 2022, 3:58pm UTC](https://discourse.julialang.org/t/enable-multiple-cores-for-jupyter-lab/18658/11 "2022-09-21T15:58:24Z")

</div>

Not clear this is the right place for that setting.

Needs to be in the env dict within kernel.json:

```julia
"env": {
    "JULIA_NUM_THREADS": "4"
  },

```

Otherwise, Julia hangs when trying to open the kernel.

---

<div class="post-metadata">

**Author:** ![Andrea\_Vigliotti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrea_vigliotti/32/7635_2.png) [@Andrea\_Vigliotti](https://discourse.julialang.org/u/Andrea_Vigliotti)\
**Post date:** [April 8, 2024, 5:05pm UTC](https://discourse.julialang.org/t/enable-multiple-cores-for-jupyter-lab/18658/12 "2024-04-08T17:05:40Z")

</div>

this worked for me :

```julia
{
  "display_name": "Julia 1.10.2 with 4 threads",
  "argv": [ 
    "/PATH/TO/bin/julia",
    "-t 4",
    "-i",
    "--color=yes",
    "--project=@.",
    "/PATH/TO/kernel.jl",
    "{connection_file}"
  ],
  "language": "julia",
  "env": {},
  "interrupt_mode": "message"
}

```

---

<div class="post-metadata">

**Author:** ![pierre-haessig](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pierre-haessig/32/217129_2.png) [@pierre-haessig](https://discourse.julialang.org/u/pierre-haessig)\
**Post date:** [July 30, 2024, 8:25am UTC](https://discourse.julialang.org/t/enable-multiple-cores-for-jupyter-lab/18658/13 "2024-07-30T08:25:20Z")

</div>

I also prefer the simplicity of the command line argument (with [Julia 1.5+](https://docs.julialang.org/en/v1/manual/multi-threading/#Starting-Julia-with-multiple-threads)) compared to setting an environment variable.

However, I’m not sure your propose JSON fragement is correct: in the `"argv"` list of the kernel file:

- you added `"-t 4"`
- I’ve tested the long version `"--threads 8"`, but then the kernel start failed with `ERROR: unknown option `–threads 8`
- so I switched to `--threads=8` (same syntax used for the other options) and it works

So are you sure `"-t 4"` shouldn’t be instead `"-t=4"`?
