演示视频:https://www.bilibili.com/video/BV1nL411M7mP
# 安装jupyterhub基础环境
>>> apt install nodejs
>>> apt install npm
>>> apt install libffi-dev
>>> npm install -g configurable-http-proxy
# 安装jupyterhub
>>> pip3 install notebook
>>> python3 -m pip install jupyterhub
如果不安装notebook则会报错:
ERROR:asyncio:Task exception was never retrieved
future: <Task finished coro=<BaseHandler.spawn_single_user() done, defined at /usr/local/lib/python3.6/dist-packages/jupyterhub/handlers/base.py:697> exception=HTTPError()>
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/jupyterhub/handlers/base.py", line 889, in spawn_single_user
timedelta(seconds=self.slow_spawn_timeout), finish_spawn_future
tornado.util.TimeoutError: Timeout
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/jupyterhub/handlers/base.py", line 922, in spawn_single_user
% (status, spawner._log_name),
tornado.web.HTTPError: HTTP 500: Internal Server Error (Spawner failed to start [status=1]. The logs for gaoxiang may contain details.)
[W 2020-01-16 16:31:19.506 JupyterHub user:678] gaoxiang's server never showed up at http://127.0.0.1:40326/user/gaoxiang/ after 30 seconds. Giving up
[E 2020-01-16 16:31:19.519 JupyterHub gen:599] Exception in Future <Task finished coro=<BaseHandler.spawn_single_user.<locals>.finish_user_spawn() done, defined at /usr/local/lib/python3.6/dist-packages/jupyterhub/handlers/base.py:800> exception=TimeoutError("Server at http://127.0.0.1:40326/user/gaoxiang/ didn't respond in 30 seconds",)> after timeout
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/tornado/gen.py", line 593, in error_callback
future.result()
File "/usr/local/lib/python3.6/dist-packages/jupyterhub/handlers/base.py", line 807, in finish_user_spawn
await spawn_future
File "/usr/local/lib/python3.6/dist-packages/jupyterhub/user.py", line 654, in spawn
await self._wait_up(spawner)
File "/usr/local/lib/python3.6/dist-packages/jupyterhub/user.py", line 701, in _wait_up
raise e
File "/usr/local/lib/python3.6/dist-packages/jupyterhub/user.py", line 669, in _wait_up
http=True, timeout=spawner.http_timeout, ssl_context=ssl_context
File "/usr/local/lib/python3.6/dist-packages/jupyterhub/utils.py", line 234, in wait_for_http_server
timeout=timeout,
File "/usr/local/lib/python3.6/dist-packages/jupyterhub/utils.py", line 177, in exponential_backoff
raise TimeoutError(fail_message)
TimeoutError: Server at http://127.0.0.1:40326/user/gaoxiang/ didn't respond in 30 seconds
参考:https://github.com/jupyterhub/jupyterhub/issues/2517#issuecomment-482637697
# 安装jupyterthemes
>>> pip3 install --upgrade cython
>>> pip3 install --upgrade jupyterthemes
>>> jt -t oceans16 -f fira -fs 13 -cellw 90% -ofs 11 -dfs 11 -T
# 配置jupyterhub
>>> cd /etc
>>> mkdir jupyterhub
>>> cd jupyterhub
>>> jupyterhub --generate-config
>>> adduser gaoxiang
>>> cd /home
>>> chmod -R 700 /home/gaoxiang
#>>> chgrp -R gaoxiang notebook
#>>> chown -R gaoxiang notebook
# 运行
>>> nohup jupyterhub --ip 0.0.0.0 --port 8888 -f /etc/jupyterhub/jupyterhub_config.py > jupyterhub.log 2>&1 &
设置为系统服务以取代nohup
参考:https://github.com/jupyterhub/jupyterhub/wiki/Run-jupyterhub-as-a-system-service
>>> vim /etc/jupyterhub/jupyterhub_config.py
c.JupyterHub.ip = '0.0.0.0'
c.JupyterHub.port = 8888
2020-01-16
jupyterlab无论从外观还是功能上都比原始的jupyter要好很多
设置jupyterlab-hub
>>> python3 -m pip install jupyterlab
>>> jupyter labextension install @jupyterlab/hub-extension
>>> jupyter serverextension enable --py jupyterlab --sys-prefix
>>> vim /etc/jupyterhub/jupyterhub_config.py
c.Spawner.default_url = '/lab'
c.Spawner.cmd = ['jupyter-labhub']
参考:
- https://jupyterhub.readthedocs.io/en/stable/troubleshooting.html#how-do-i-use-jupyterlab-s-prerelease-version-with-jupyterhub
- https://github.com/jupyterhub/jupyterlab-hub
2021-11-05
添加用户
在jupyterhub配置文件中添加用户
>>> vim /etc/jupyterhub/jupyterhub_config.py
# If empty, does not perform any additional restriction.
c.Authenticator.whitelist = {'gaoxiang', 'songyu', 'xlxdsj', 'zhangpeng', 'liangyizhi'}
在Linux系统中添加用户
# -m参数会在/home下创建同名目录, 不加则不创建
>>> useradd -m liangyizhi
>>> ls
gaoxiang liangyizhi songyu xlxdsj zhangpeng
>>> passwd liangyizhi
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
>>> chmod -R 700 /home/liangyizhi
>>> ll
total 4
drwxr-xr-x. 1 root root 85 Nov 5 17:18 ./
drwxr-xr-x. 1 root root 261 Nov 5 17:19 ../
drwx------. 10 gaoxiang gaoxiang 203 Oct 23 17:11 gaoxiang/
drwx------ 2 liangyizhi liangyizhi 57 Nov 5 17:18 liangyizhi/
drwx------. 9 songyu songyu 180 Jan 19 2020 songyu/
drwx------. 19 xlxdsj xlxdsj 4096 Jul 30 09:31 xlxdsj/
drwx------. 7 zhangpeng zhangpeng 133 Jan 19 2020 zhangpeng/
2021-11-23
使用apt安装nodejs后发现
>>> jupyter labextension install @jupyterlab/hub-extension
An error occurred.
ValueError: Please install nodejs >=12.0.0 before continuing. nodejs may be installed using conda or directly from the nodejs website.
See the log file for details: /tmp/jupyterlab-debug-haosj25e.log
这是因为使用apt 安装的nodejs版本过低
需要从官网下载
# 下载
>>> wget https://nodejs.org/dist/v16.13.0/node-v16.13.0-linux-x64.tar.xz
# 解压缩
>>> tar -xvf node-v16.13.0-linux-x64.tar.xz
# 创建软连接
>>> ln -s /root/node-v16.13.0-linux-x64/bin/node /usr/local/bin/
>>> ln -s /root/node-v16.13.0-linux-x64/bin/npm /usr/local/bin/
这时再执行命令就正常了
如果你想指定一下根目录,默认会进入以该用户名作为名字命名的目录
>>> vim /etc/jupyterhub/jupyterhub_config.py
c.Spawner.notebook_dir = '/home'
>>> service jupyterhub restart
如果出现如下错误
>>> jupyterhub --ip 0.0.0.0 --port 8890 -f /etc/jupyterhub/jupyterhub_config.py
[C 2021-11-23 08:38:44.626 JupyterHub app:2755] Failed to start proxy
Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/jupyterhub/app.py", line 2753, in start
await self.proxy.start()
File "/usr/local/lib/python3.8/dist-packages/jupyterhub/proxy.py", line 707, in start
self.proxy_process = Popen(
File "/usr/lib/python3.8/subprocess.py", line 858, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/usr/lib/python3.8/subprocess.py", line 1704, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'configurable-http-proxy'
则:
>>> vim /etc/jupyterhub/jupyterhub_config.py
c.JupyterHub.proxy_cmd = ['/node-v16.13.0-linux-x64/bin/configurable-http-proxy']
2023-03-28更新
之前jupyerhub集成jupyterlab需要安装插件jupyterlab/hub-extension,但jupyter labextension install安装插件的方式已经弃用且该插件也已经弃用
>>> jupyter labextension install @jupyterlab/hub-extension
(Deprecated) Installing extensions with the jupyter labextension install command is now deprecated and will be removed in a future major version of JupyterLab.
调整方案:
修改jupterhub配置文件即可换成jupyterlab界面
>>> vim /etc/jupyterhub/jupyterhub_config.py
# - You can set `notebook_dir` to `/` and `default_url` to `/tree/home/{username}` to allow people to
# navigate the whole filesystem from their notebook server, but still start in their home directory.
# - Start with `/notebooks` instead of `/tree` if `default_url` points to a notebook instead of a directory.
# - You can set this to `/lab` to have JupyterLab start by default, rather than Jupyter Notebook.
# Default: ''
c.Spawner.default_url = '/lab'
2023-03-30更新
如果出现在jupyter内获取环境变量不一致的现象则:
# 生成配置文件
>>> jupyter notebook --generate-config
# 显示配置地址
>>> jupyter --config-dir
# 修改配置文件(在文件开头加入)
>>> vim /home/gaoxiang/.jupyter/jupyter_notebook_config.py
import os
c = get_config()
os.environ['mac_address'] = 'ff:ff:ff:ff:ff:ff'
c.Spawner.env.update('mac_address')
...
# 重启jupyter服务
>>> service jupyterhub restart
注意:如果有多账户,则需要使用每个账户依次执行上述命令
参考:
https://www.brothereye.cn/linux/565/
https://github.com/jupyter/notebook/issues/1290#issuecomment-204388895