How to set up one or more per user virtual environments in jupyterhub

I set up jupyterhub+lab following the instructions posted here, except for part 2 which would have involved conda. I’m much more familiar with pip and after bad experiences in the past, I’d like to avoid conda if possible.

When I log in as a user and open a shell I seem to be in the venv in which jupyterlab was set up. When I open a notebook the same seems to be the case.

Is it possible to set it up such that each user can create their own virtual (pip) environment or environments? If so, is there a guide to how to do so?

The only information I’ve found so far appears to be c.Spawner.cmd; which states that: This is usually set if you want to start the single-user server in a different python environment (with virtualenv/conda) than JupyterHub itself. but there are no examples.

Where would I find pointers/documentation/working examples for such a setup?
In Jupyterlab lingo, is a Python instance running inside a virtual env the same as a “kernel?”

Here’s what I tried with partial success.

  • create a virtual environment as usual.
  • install ipykernel in it and all other packages you want/need.
  • activate it. This should give you an executable ipython in your path
  • run ipython kernel install --prefix /tmp
  • this will give you a hierarchy /tmp/share/jupyter/kernels/python3 - that directory contains a kernel.json file

Then, leave the virtual environment and activate the virtual environment jupyterhub lives in.

  • then run jupyter kernelspec install --user /tmp/share/jupyter/kernels/python3 - this will copy the configuration into ~/.local/share/jupyter/kernels/python3 from where, it appears, Jupyterhub will read it.

This seems to work for Notebooks - however, when I open a shell, I’m still in the Jupyterhub environment.

It seems that you don’t want to use conda, so my solution might not work for you. But using conda and the package nb_conda_kernels works nicely.

You can then create additional conda environments with

conda create -n myenv ipykernel

and it shows up on the launcher page of JupyterHub.