Skip to content

Instantly share code, notes, and snippets.

@kathoef
Last active July 31, 2021 17:23
Show Gist options
  • Save kathoef/a4745785bef0e58da9000ff68eed0899 to your computer and use it in GitHub Desktop.
Save kathoef/a4745785bef0e58da9000ff68eed0899 to your computer and use it in GitHub Desktop.
Jupyter conda kernel installer
#!/bin/bash
# Uninstall kernel.
# rm -rf ${HOME}/.local/share/jupyter/kernels/my-pykernel/
# rm -rf ${HOME}/my-pykernel/
# Specify a few targets.
if [[ -f environment.yaml ]]; then
JUPYTER_KERNEL_NAME=$(grep -e 'name:' environment.yaml | awk '{print $2}')
CONDA_INSTALL_DIR=${HOME}/${JUPYTER_KERNEL_NAME}
else
echo "environment.yaml not found..."; exit
fi
# Install "isolated" conda environment.
module purge
mkdir -p "${CONDA_INSTALL_DIR}"
wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh -b -f -p "${CONDA_INSTALL_DIR}"
${CONDA_INSTALL_DIR}/bin/conda env create -f environment.yaml
rm Miniconda3-latest-Linux-x86_64.sh
${CONDA_INSTALL_DIR}/bin/conda clean --all --yes
# Specify Jupyter kernel start-up script.
echo '#!/bin/bash
module purge
source '"${CONDA_INSTALL_DIR}"'/etc/profile.d/conda.sh
conda activate '"${JUPYTER_KERNEL_NAME}"'
exec python "$@"' > "${CONDA_INSTALL_DIR}"/kernel.sh
chmod +x "${CONDA_INSTALL_DIR}"/kernel.sh
# Manually register Jupyter kernel.
JUPYTER_USER_KERNEL_DIR=${HOME}/.local/share/jupyter/kernels/${JUPYTER_KERNEL_NAME}
mkdir -p "${JUPYTER_USER_KERNEL_DIR}"
echo '{
"argv": [
"'"$CONDA_INSTALL_DIR"'/kernel.sh",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"display_name": "my-pykernel",
"language": "python",
"metadata": {
"debugger": true
}
}' > "${JUPYTER_USER_KERNEL_DIR}"/kernel.json
# $ CONDA_INSTALL_DIR=${HOME}/my-pykernel JUPYTER_KERNEL_NAME=my-pykernel bash -c 'source ${CONDA_INSTALL_DIR}/etc/profile.d/conda.sh; conda activate ${JUPYTER_KERNEL_NAME}; ipython kernel install --name my-pykernel --prefix ${HOME}/tmp'
# $ cat ${HOME}/tmp/share/jupyter/kernels/my-pykernel/kernel.json
#
# {
# "argv": [
# "/home/jovyan/my-pykernel/envs/my-pykernel/bin/python",
# "-m",
# "ipykernel_launcher",
# "-f",
# "{connection_file}"
# ],
# "display_name": "my-pykernel",
# "language": "python",
# "metadata": {
# "debugger": true
# }
# }
# References
# https://docs.jupyter-jsc.fz-juelich.de/github/FZJ-JSC/jupyter-jsc-notebooks/blob/master/001-Jupyter/Create_JupyterKernel_conda.ipynb
# https://github.com/geomar-od/visualize-15-million-trajectories/tree/af46ce2bdfe94af84f2725e74a275d323cd92776/envs
# https://conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#creating-an-environment-from-an-environment-yml-file
# https://conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#creating-an-environment-file-manually
# https://ipython.readthedocs.io/en/stable/install/kernel_install.html#kernels-for-different-environments
name: my-pykernel
channels:
- conda-forge
dependencies:
- ipykernel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment