Custom Kernels using JupyterLab OnDemand
JupyterLab runs through the use of kernels, which act as an interpreter between hardware (such as CPU, RAM, GPU, etc.) and software (the commands/programs you attempt to run). OnDemand does not automatically import your user environments, and JupyterLab is very particular about the version of Python being used. If you attempt to connect to a kernel which uses a newer version of Python than JupyterLab, the kernel will most likely fail to connect whenever you attempt to activate it. In order to run Python versions past 3.11.4 or use other languages, you need to change how your OnDemand environment is initialized.
Setting up Environments using Mamba/Conda
Follow the instructions at BYU RC Wiki to set up Python environments using Mamba/Conda.
Python Kernels
Running a different version of JupyterLab
In order to create a Python kernel, we will need to make a new version of JupyterLab that matches the Python version that you wish to use. We will need to make a new Mamba environment, then create and install a new version of Python.
1. Create a new environment for JupyterLab named. You can name it anything you want, but for now we will call the environment "MyJupyterLab":
mamba create -n MyJupyterLab
2. Activate the new environment:
mamba activate MyJupyterLab
3. Install the desired Python version. If you want to use a newer version of python, replace "3.11.4" with the version you wish to install:
mamba install python=3.11.4
4. Install jupyterlab:
mamba install jupyterlab
jupyter lab
Configuring Environment Variables for OnDemand
In order to use the new version of Jupyter Labs, you need to change the environment variables for OnDemand. Check the box labeled Show Python/JupyterLab Environment Setup and replace what is in the text box with the following:
mamba init
source ~/.bashrc
mamba activate MyJupyterLab
Click Submit interactive job to start JupyterLab using the new version of Python you have installed. a Once you run JupyterLab with this environment setup, it will automatically detect Mamba environments that you have in your home directory.
Creating a new Python Environment
Using Mamba/Conda, we can create a new Python kernel, which will allow us to use previously unavailable modules such as numpy or pandas. If you did not set up an environment for JupyterLab using a different version of Python, the only version of Python you can use is Python 3.11.4.
1. Create a new environment for your Python kernel. You can name it whatever you like:
mamba create -n YourPythonNameHere
2. Activate the new environment:
mamba activate YourPythonNameHere
3. Install the desired Python version. Make sure that it is the same version that you used for making your new JupyterLab environment:
mamba install python=3.11.4
4. Install any other Python modules that you need. For example, numpy:
mamba install numpy
Making a new Python kernel
If you followed the setup instructions for running a new JupyterLab version, this step is unnecessary if you set up a new version of JupyterLab, but it will allow you to create a kernel and change the name displayed when selecting it in JupyterLab.
1. Install ipykernel. This will allow you to create a kernel to be used by JupyterLab:
mamba install ipykernel
2. Run the ipykernel command. You can change the name that the kernel is stored as (YourEnvName), as well as the name displayed to you when selecting it in JupyterLab (YourDisplayName):
python -m ipykernel install --user --name=YourEnvName --display-name=YourDisplayName
Non-Python Kernels
If you need a kernel that runs on a language other than Python, the installation will differ for each language. General guides can be found online, but below we will show how to install a kernel using the R language.
Installing R Kernel
1. Open your JupyterLab environment:
mamba activate MyJupyterLab
2. Install the desired Python version, as well as ipykernel. Make sure that it is the same version that you used for making your new JupyterLab environment:
mamba install python=3.11.4
mamba install ipykernel
3. Install R and IRKernel:
mamba install r-base
mamba install r-irkernel
4. Open an R terminal:
R
5. In your new R terminal, run the following command to set up a new kernel for R. You can set the environment name (YourEnvName), as well as the display name (YourDisplayName):
IRkernel::installspec(name = 'YourEnvName', displayname = 'YourDisplayName')
Last changed on Tue Apr 23 12:59:52 2024