【手把手教你】如何在Linux系统搭建jupyter notebook

1、官网下载Anaconda2或3 for Linux

比如:Anaconda2-5.0.1-Linux-x86_64.sh  我下的这个

2、rz上传到linux,选一个未来你经常用到的目录

比如:我放/data/search/autotest/wj/Anaconda2-5.0.1-Linux-x86_64.sh目录下
[root@djt_37_152 wj]# pwd
/data/search/autotest/wj
[root@djt_37_152 wj]# ll 
Anaconda2-r--r--r--  1 root root 532375438 1Ղ  19 14:23 Anaconda2-5.0.1-Linux-x86_64.sh

3、执行chmod +x Anaconda2-5.0.1-Linux-x86_64.sh

[root@djt_37_152 wj]# chmod +x Anaconda2-5.0.1-Linux-x86_64.sh
[root@djt_37_152 wj]# ll 
Anaconda2-rwxr-xr-x  1 root root 532375438 1Ղ  19 14:23 Anaconda2-5.0.1-Linux-x86_64.sh

4、执行sh Anaconda2-5.0.1-Linux-x86_64.sh

[root@djt_37_152 wj]# sh Anaconda2-5.0.1-Linux-x86_64.sh 
.........(安装过程略,此处省略n行)..........

其实看控制台可以发现默认会安装在/root/anaconda2目录,且jupyter的配置文件在: /root/.jupyter/jupyter_notebook_config.py 这里。如果你没看到这些也没关系。可以通过如下这个命令找到(生成)这个配置文件。
如果是root用户:

[root@djt_37_152 wj]#jupyter notebook --generate-config --allow-root
#控制台输出类似信息:Writing default config to: /root/.jupyter/jupyter_notebook_config.py

非root用户:

[biztech@djt_37_152 wj]$jupyter notebook --generate-config
#控制台输出类似信息:同上

5、配置环境变量到path

[root@djt_37_152 wj]# export PATH="/root/anaconda2/bin:$PATH"

或修改/etc/profile文件

[root@djt_37_152 wj]# vim /etc/profile
export PATH="/root/anaconda2/bin:$PATH"
[root@djt_37_152 wj]# . /etc/profile #使生效即可

6、安装jupyter的相关插件:
先执行pip install jupyter_contrib_nbextensions
再执行jupyter contrib nbextension install --user

[root@djt_37_152 wj]# pip install jupyter_contrib_nbextensions
.........(看你的网络怎么样了,,哈哈,,一般都没问题)........
[root@djt_37_152 wj]# jupyter contrib nbextension install --user

7、执行命令查看jupyter的配置文件在哪:
如果是root用户:

[root@djt_37_152 wj]#jupyter notebook --generate-config --allow-root
#控制台输出类似信息:Writing default config to: /root/.jupyter/jupyter_notebook_config.py

非root用户:

[biztech@djt_37_152 wj]$jupyter notebook --generate-config
#控制台输出类似信息:同上

值得一提的是:windows环境下,并没有jupyter_notebook_config.py这个配置文件,怎么办呢?
可以打开Anaconda Promote界面,执行上面相同的命令即可生成。

(d:\anaconda2) C:\Users\XXXXXXX>jupyter notebook --generate-config
Writing default config to: C:\Users\XXXXXXX\.jupyter\jupyter_notebook_config.py

生成后即可和linux一样按照如下步骤修改了。哈哈~~

8、打开ipython,生成密码

[root@djt_37_152 wj]# ipython
In [1]:  from notebook.auth import passwd
In [2]: passwd()
Enter password: 123456Verify password: 123456Out[2]: 'sha1:f8b5f5dbeca8:d1f5b93d5e787e4bf1bf4ad2c48c177ba79f55dd'     #这个秘钥要copy备用

9、编辑配置文件jupyter_notebook_config.py

[root@djt_37_152 wj]# vim /root/.jupyter/jupyter_notebook_config.py 
#改几个地方:
c.NotebookApp.ip = 'xx.xxx.xx.xxx'            即对外提供访问的ip
c.NotebookApp.port = 9527                     即对外提供访问的端口
c.NotebookApp.open_browser = False            False即启动不打开浏览器
c.NotebookApp.password = u'sha1:f8b5f5dbeca8:d1f5b93d5e787e4bf1bf4ad2c48c177ba79f55dd'   这个就是上面生成的秘钥
c.NotebookApp.notebook_dir = u'/search/autotest/jupyter_dir' 即设置jupyter启动后默认的根目录

10、启动jupyter
如果用root启动:

[root@djt_37_152 wj]# jupyter notebook --allow-root

非root用户启动:

[biztech@djt_37_152 wj]$ jupyter notebook

值得一提的是:如果想让进程在后台运行,只需要在上面的命令后面加&即可,比如:

[biztech@djt_37_152 wj]$ jupyter notebook &

11、本地浏览器访问即可
在本地浏览器打开:http://xxx.xx.xx.xxx:9527 即可访问了–当然你得输入密码:123456

追加遇到的问题:

用阿里云服务器搭建的过程中遇到了这样的问题:

  • 安装jupyter插件失败
[root@XXXXXXXXXXXXXXX jupyter_dir]# pip install jupyter_contrib_nbextensions
..............(此处省略n行)..............
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-hWGASd/ipython/

网上找了很多方法都没呢解决…
(因为在云服务器安装anaconda3,且配置了环境变量,所以可以直接使用conda命令)
然后进入github 找到jupyter_contrib_nbextensions项目,然后对照github中的readme,再执行如下linux命令:
conda install jupyter_contrib_nbextensions
看结果貌似OK了

[root@XXXXXXXXXXXXXXX jupyter_dir]# conda install jupyter_contrib_nbextensions
[I 16:39:04 InstallContribNbextensionsApp] - Validating: OK
[I 16:39:04 InstallContribNbextensionsApp] Installing jupyter_contrib_nbextensions items to config in /root/anaconda3/etc/jupyter
Enabling: jupyter_nbextensions_configurator
- Writing config: /root/anaconda3/etc/jupyter
    - Validating...
      jupyter_nbextensions_configurator  OK
Enabling notebook nbextension nbextensions_configurator/config_menu/main...
Enabling tree nbextension nbextensions_configurator/tree_tab/main...
[I 16:39:04 InstallContribNbextensionsApp] Enabling notebook extension contrib_nbextensions_help_item/main...
[I 16:39:04 InstallContribNbextensionsApp]       - Validating: OK
[I 16:39:04 InstallContribNbextensionsApp] - Editing config: /root/anaconda3/etc/jupyter/jupyter_nbconvert_config.json
[I 16:39:04 InstallContribNbextensionsApp] --  Configuring nbconvert template path
[I 16:39:04 InstallContribNbextensionsApp] --  Configuring nbconvert preprocessors
[I 16:39:04 InstallContribNbextensionsApp] - Writing config: /root/anaconda3/etc/jupyter/jupyter_nbconvert_config.json
[I 16:39:04 InstallContribNbextensionsApp] --  Writing updated config file /root/anaconda3/etc/jupyter/jupyter_nbconvert_config.json                                                                                                                                                                                                                                                                        done
  • 搭建完,执行命令jupyter或jupyter notebook报错
# 报错1:
[root@XXXXXXXXXXXXXXX jupyter_dir]# jupyter
usage: jupyter [-h] [--version] [--config-dir] [--data-dir] [--runtime-dir]
               [--paths] [--json]
               [subcommand]
jupyter: error: one of the arguments --version subcommand --config-dir --data-dir --runtime-dir --paths is required

执行命令jupyter notebook

# 报错2:
[root@XXXXXXXXXXXXXXX jupyter_dir]# jupyter notebook --allow-root &
[1] 20545
[root@iz2ze1x4k98zgmhq7wqg4az jupyter_dir]# Traceback (most recent call last):
  File "/root/anaconda3/bin/jupyter-notebook", line 11, in <module>
    sys.exit(main())
  File "/root/anaconda3/lib/python3.6/site-packages/jupyter_core/application.py", line 266, in launch_instance
    return super(JupyterApp, cls).launch_instance(argv=argv, **kwargs)
  File "/root/anaconda3/lib/python3.6/site-packages/traitlets/config/application.py", line 657, in launch_instance
    app.initialize(argv)
  File "<decorator-gen-7>", line 2, in initialize
  File "/root/anaconda3/lib/python3.6/site-packages/traitlets/config/application.py", line 87, in catch_config_error
    return method(app, *args, **kwargs)
  File "/root/anaconda3/lib/python3.6/site-packages/notebook/notebookapp.py", line 1507, in initialize
    self.init_webapp()
  File "/root/anaconda3/lib/python3.6/site-packages/notebook/notebookapp.py", line 1297, in init_webapp
    self.http_server.listen(port, self.ip)
  File "/root/anaconda3/lib/python3.6/site-packages/tornado/tcpserver.py", line 142, in listen
    sockets = bind_sockets(port, address=address)
  File "/root/anaconda3/lib/python3.6/site-packages/tornado/netutil.py", line 197, in bind_sockets
    sock.bind(sockaddr)
OSError: [Errno 99] Cannot assign requested address

网上又查了一下,貌似配置文件哪里出错了
解决的方法是:把这一行的localhost改为ifconfig查出的ip即可(但是不能是云服务器的外网ip,切记啊,切记)

[root@XXXXXXXXXXXXXXX jupyter_dir]# vim /root/.jupyter/jupyter_notebook_config.py
## The IP address the notebook server will listen on.
#c.NotebookApp.ip = 'localhost'
c.NotebookApp.ip = 'XXX.XX.XXX.XX'

值得一提的是:上面的XXX.XX.XXX.XX是内网ip即在云服务器通过ifconfig查出的eth0所在的ip地址,非外网ip。

上面的问题都解决了,后台启动jupyter也不报错了:

[root@XXXXXXXXXXXXXXX jupyter_dir]# jupyter notebook --allow-root &
[I 16:47:46.799 NotebookApp] [jupyter_nbextensions_configurator] enabled 0.4.0
[I 16:47:46.827 NotebookApp] JupyterLab beta preview extension loaded from /root/anaconda3/lib/python3.6/site-packages/jupyterlab
[I 16:47:46.827 NotebookApp] JupyterLab application directory is /root/anaconda3/share/jupyter/lab
[I 16:47:46.830 NotebookApp] Serving notebooks from local directory: /home/jupyter_dir
[I 16:47:46.831 NotebookApp] 0 active kernels
[I 16:47:46.831 NotebookApp] The Jupyter Notebook is running at:
[I 16:47:46.831 NotebookApp] http://XXX.XX.XXX.XX:1234/
[I 16:47:46.831 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[W 16:47:46.831 NotebookApp] No web browser found: could not locate runnable browser.
[I 16:51:32.841 NotebookApp] 302 GET / (36.110.147.197) 0.59ms
[I 16:51:32.855 NotebookApp] 302 GET /tree? (36.110.147.197) 0.94ms
[I 16:52:32.499 NotebookApp] 302 GET / (223.167.152.22) 0.49ms
[I 16:52:32.589 NotebookApp] 302 GET /tree? (58.246.221.73) 0.91ms

但是从外网还是无法访问,即
浏览器打开http://YYY.YY.YYY.YY:1234/ (其中YYY.YY.YYY.YY是阿里云服务器的外网ip)
原因很简单,这个可以参考博客文章《阿里云 解决为什么不能使用公网IP地址访问部署的nginx项目》。用同样的方式开启端口1234即可。

  • 27
    点赞
  • 86
    收藏
    觉得还不错? 一键收藏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值