Kail's Blog

Somewhat HPC related blog

Running A Julia Kernel In Apptainer

In a previous article Singularity Jupyter Kernel I went through setting up an Apptainer container to run iPython kernels in Jupyterlab.

Recently I started looking into Julia and wanted to do the same setup. It turned out to not be as straightforward for me as I was not familiar with working with Julia and it's virtual environments, but did eventually get it worked out.

First we'll define a minimal apptainer definition file and install the one requisite package IJulia.

bootstrap: docker
From: julia:1.10-bookworm

%labels
    AUTHOR andrew@kail.io

%post
    export JULIA_DEPOT_PATH="/usr/local/julia/local/share/julia"
    julia --project="/opt/." -e 'using Pkg; Pkg.add("IJulia");'

One thing to note for those not familiar with Julia is --project="/opt/.". Julia by default installs packages in a user's home directory under .julia if on Linux. During the build process, this home directory is local to the container's root user, and later if the user launching the container has their own Julia packages installed, those may come into conflict with what is expected with the container. Specifying --project tells Julia to use that directory as its virtual environment and install packages local to the container and can be referenced later during the kernel launch.

Building the container is as straight forward as before:

apptainer build ipykernel.sif ipykernel.def

Finally, add the following to kernel specification under /home/<user>/.local/share/jupyter/kernels/<kernelname>/kernel.json

{
  "display_name": "Julia",
  "argv": [
    "apptainer",
    "exec",
    "--cleanenv",
    "/home/akail/Projects/Finance/julia/kernel.sif",
    "julia",
    "-i",
    "--color=yes",
    "-O3",
    "--project=/opt/",
    "/usr/local/julia/local/share/julia/packages/IJulia/Vo51o/src/kernel.jl",
    "{connection_file}"
  ],
  "language": "julia",
  "interrupt_mode": "signal"
}

Warning

This path /usr/local/julia/local/share/julia/packages/IJulia/Vo51o/src/kernel.jl is not guaranteed to be correct. I am still working to figure the Vo51o portion out.

Restart Jupyter or Jupyter lab and the kernel should now be available.