How to Auto-Create Users for SimpleLocalProcessSpawner on 3rd Party Login alongside Conda?

I am trying to setup JupyterHub (on a single EC2 server, HTTPS is provided via a reverse Nginx proxy URL) such that a small number of users authenticate with a 3rd party Solution (KeyCloak) and upon redirect back to the JupyterHub/Lab will automatically have a user created for them locally.

I have already correctly configured KeyCloak Authentication and a user is able to login with Keycloak and is successfully redirected back to the JupyterHub URL. However, I get 500 : Internal Server Error Unhandled error starting server <username_from_keycloak_login>. This is expected, because the user that authenticated with KeyCloak does not yet exist on the Server hosting JupyterHub.

This is the reason I need the user created locally, automatically. I don’t want to have to SSH into the Server hosting the JupyterHub and manually create an account for someone each time, before they plan to authenticate with KeyCloak.

Attempted Solution:
I am following this guide: “Install JupyterHub and JupyterLab from the ground up” which creates Conda environments per user where they can install whatever dependencies they want for their Python JupyterLab/Notebook.

I found the following snippet of config (for from another thread here, which automatically creates a user upon login such that a Spawner like jupyterhub.spawner.SimpleLocalProcessSpawner can spawn a server for that user:

def pre_spawn_hook(spawner):
    username =
        import pwd
    except KeyError:
        import subprocess
        subprocess.check_call(['useradd', '-ms', '/bin/bash', username])

c.Spawner.pre_spawn_hook = pre_spawn_hook

Does this play nicely with the section on user Conda environments from the guide I referenced above? Or would creating users via the code above not work with Conda? I frankly don’t care if each user that logs into Jupyter (via KeyCloak) doesn’t have a dedicated space to install any Python dependencies they may need, I just need them to have functioning accounts.

Additional Info:

I got the snippet of config from a link posted here: Setup Jupyterhub setup with NativeAuthenticator - #11 by lambdaTotoro

I’m not following what exactly you need help with. Are you getting an error with your pre_spawn_hook? If so what do your hub logs say?

I was more asking this as a preemptive question rather than an actual bug. Will adding users in this manner (via a pre-spawn hook) not interact with conda at all? I.e. will users added in this fashion merely have a local folder that doesn’t have an associated conda environment with it?

Okay now I’m actually getting a bug:The hub logs say “JupyterHub utils:91] Unexpected error connecting to :8000 [Errno 22] Invalid argument”

I think this is because I’m logging into the JupyterHub website without having run the following commands via SSH as that user on the server (EC2) hosting JupyterHub:
/path/to/kernel/env/bin/python -m ipykernel install --user --name 'python-my-env' --display-name "Python My Env"

It’s legitimately strange that Jupyter expects users of the JupyterHub to just SSH into the server and configure their conda environment how they see fit and only then can they spawn a server for themselves.

Normally people don’t use websites like that. They should be able to simply log in, and have Python environment created for them (without them even remotely touching the actually server itself via SSH) which they can run Python code inside.

Is there a way to achieve that?

Solved. Cannot spawn server for user authenticated with 3rd party (KeyCloak)?