Ubuntu 服务器安装 Jupiter Notebook

发布时间 2023-06-30 05:11:50作者: 江浸月

Ubuntu 服务器安装 Jupiter Notebook

  1. 安装python3

    apt -update	//更新软件包
    apt install python3
    
  2. 确认python版本

    python3 --version
    Python 3.6.8
    
  3. 安装pip

    python3 -m pip install --upgrade pip
    
  4. 使用pip安装Jupyter以及所需的IPython

    pip3 install jupyter
    pip3 install ipython
    
  5. 生成Jupyter的配置文件,稍后将使用这个配置文件来修改Jupyter的默认启动配置

    jupyter notebook --generate-config
    
  6. 设置Jupyter登录密码

    [root@root ~]# ipython
    Python 3.6.8 (default, Aug  7 2019, 17:28:10) 
    Type 'copyright', 'credits' or 'license' for more information
    IPython 7.11.1 -- An enhanced Interactive Python. Type '?' for help.
    
    In [1]: from notebook.auth import passwd                                                                                                                                   
    
    In [2]: passwd()                                                                                                                                                           
    Enter password: 
    Verify password: 
    Out[2]:'argon2:$argon2id$v=19$m=10240,t=10,p=8$W+vJx98ZueTAYO0wv3Jdgw$xSiJau3So3l1a+VsfVxvL63bzAh1lMdtldbCv1lBcG0'
    In [3]:exit
    
  7. 创建Jupyter文件夹

    mkdir -p /home/jupyter
    
  8. 修改Jupyter配置文件

vim /root/.jupyter/jupyter_notebook_config.py

在文件末尾插入如下配置:

c.NotebookApp.ip = '*' # 设置Jupyter监听的ip地址,修改为*表示监听所有ip地址
c.NotebookApp.password = u'argon2:$argon2id$v=19$m=10240,t=10,p=8$W+vJx98ZueTAYO0wv3Jdgw$xSiJau3So3l1a+VsfVxvL63bzAh1lMdtldbCv1lBcG0' # 将该内容替换为上一步设置密码时生成的值
c.NotebookApp.open_browser = False # 禁止启动时自动打开浏览器(本来在桌面平台上安装使用时可以开启,在服务器上不需要此设置,因此设置为False)
c.NotebookApp.port = 1024 # 指定访问的端口,按照自己喜好设定,默认是8888,注意不要和已用端口冲突
c.NotebookApp.notebook_dir = '/home/jupyter' # 设置运行时的目录,因为以root身份运行时默认会在/root目录下,因此最好修改成自己喜欢的目录,例如'/home/jupyter'
  1. 启动Jupyter

    jupyter notebook --no-browser --allow-root # 我这里加入--allow-root是因为我是以root身份运行的,如果不添加就无法启动,非root用户启动时可以不加
    

在浏览器中输入ip+端口(127.0.0.1:1024)打开

输入密码即可

在服务器上编写脚本让Jupyter在后台运行

cd /home/jupyter
vim start.sh

将以下代码复制

#!/bin/bash
cd .
nohup jupyter notebook --no-browser --allow-root  > jupyter.log 2>&1 &
echo "Jupyter已启动!" 

运行

sh start.sh

即可启动在后台运行

*注(关闭Jupyter)

ps -ef | grep jupyter #找到jupyter进程号
kill -9 进程号