温馨提示×

如何在Ubuntu上安装和配置Jupyter Notebook

小樊
90
2024-09-05 12:11:26
栏目: 智能运维

要在Ubuntu上安装和配置Jupyter Notebook,请按照以下步骤操作:

  1. 首先,确保系统已更新。打开终端并运行以下命令:
sudo apt update
sudo apt upgrade
  1. 安装Python3和pip(Python包管理器)。运行以下命令:
sudo apt install python3 python3-pip
  1. 使用pip安装Jupyter。运行以下命令:
pip3 install jupyter
  1. 启动Jupyter Notebook。在终端中输入以下命令:
jupyter notebook

这将在您的默认Web浏览器中打开Jupyter Notebook。

  1. (可选)为了能够从任何位置启动Jupyter Notebook,您可以创建一个启动脚本。创建一个名为jupyter_start.sh的文件,并在其中添加以下内容:
#!/bin/bash
jupyter notebook --no-browser --ip=0.0.0.0 --port=8888

保存文件后,给予该脚本可执行权限:

chmod +x jupyter_start.sh

现在,您可以通过运行./jupyter_start.sh来启动Jupyter Notebook。

  1. (可选)为了让Jupyter Notebook在系统启动时自动运行,您可以将其添加到systemd服务中。创建一个名为jupyter.service的文件,并将其放置在/etc/systemd/system/目录中。在文件中添加以下内容:
[Unit]
Description=Jupyter Notebook

[Service]
Type=simple
PIDFile=/run/jupyter.pid
ExecStart=/home/your_username/jupyter_start.sh
User=your_username
Group=your_username
WorkingDirectory=/home/your_username
Restart=always

[Install]
WantedBy=multi-user.target

your_username替换为您的用户名。然后,重新加载systemd配置并启用服务:

sudo systemctl daemon-reload
sudo systemctl enable jupyter.service

现在,Jupyter Notebook将在系统启动时自动运行。

  1. 若要访问远程计算机上的Jupyter Notebook,需要在启动Jupyter Notebook时生成一个密码。首先,生成一个密码文件:
jupyter notebook password

按提示输入密码。然后,编辑Jupyter配置文件。运行以下命令:

jupyter notebook --generate-config

这将在~/.jupyter/jupyter_notebook_config.py生成一个配置文件。使用文本编辑器打开此文件,并取消以下行的注释(删除行首的#号):

c.NotebookApp.allow_remote_access = True
c.NotebookApp.ip = '0.0.0.0'
c.NotebookApp.open_browser = False
c.NotebookApp.port = 8888

保存并关闭文件。现在,您应该能够通过远程计算机访问Jupyter Notebook。只需在Web浏览器中输入http://your_server_ip:8888,然后输入之前设置的密码即可。

现在,您已经在Ubuntu上成功安装并配置了Jupyter Notebook!

0