linux如何安裝Anaconda並配置遠程jupyter

下載Anaconda安裝使用anaconda配置jupyter支持遠程訪問配置jupyter使用密碼訪問後臺運行jupyter

前言

已經有超過三人像我反應使用網上的教程Anaconda有問題,有的裝不了,有的裝的直接整的自己yum命令用不了,linux服務器都被整費。爲此我給大家寫的簡單的安裝教程,避免大家可能踩的坑。

下載Anaconda

去 https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive或https://repo.anaconda.com/archive/選擇你要安裝的版本,

前者是清華源國內訪問相對網速非常快,但清華園是定期的不讓訪問,如果你也中獎就只能通過後者官網的地址去下載。

經過無數次的實踐證明,python3.6版本是最穩定的,另外我的linux服務器是64位,爲了更快的性能,所以我選擇下載64位,所以最終我通過以下命令,直接在linux服務器上下載:

wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-5.2.0-Linux-x86_64.sh

如果清華園失效無法下載,那你需要使用命令:

wget https://repo.anaconda.com/archive/Anaconda3-5.2.0-Linux-x86_64.sh

慢慢下。

安裝

執行:

 sh Anaconda3-5.2.0-Linux-x86_64.sh
 Please, press ENTER to continue
[root@VM_0_9_centos ~]# sh Anaconda3-5.2.0-Linux-x86_64.sh 

Welcome to Anaconda3 5.2.0

In order to continue the installation process, please review the license
agreement.
Please, press ENTER to continue
>>> 
===================================
Anaconda End User License Agreement
===================================
...

先直接回車,再按q。

詢問是否同意協議,填yes:

Do you accept the license terms? [yes|no]
[no] >>> yes

選擇安裝位置,直接回車就安裝在當前路徑,我選擇安裝在/opt/anaconda3目錄下:

Anaconda3 will now be installed into this location:
/root/anaconda3

  - Press ENTER to confirm the location
  - Press CTRL-C to abort the installation
  - Or specify a different location below

[/root/anaconda3] >>> /opt/anaconda3
PREFIX=/opt/anaconda3

installing: python-3.6.5-hc3d631a_2 ...
Python 3.6.5 :: Anaconda, Inc.
installing: blas-1.0-mkl ...
installing: ca-certificates-2018.03.07-0 ...
installing: conda-env-2.6.0-h36134e3_1 ...
installing: intel-openmp-2018.0.0-8 ...
...
installing: seaborn-0.8.1-py36hfad7ec4_0 ...
installing: anaconda-5.2.0-py36_3 ...
installation finished.

經過幾分鐘的等待後,主程序安裝完成。詢問是否將Anaconda3加入到環境變量,直接回車表示no,不添加:

Do you wish the installer to prepend the Anaconda3 install location
to PATH in your /root/.bashrc ? [yes|no]
[no] >>> 
You may wish to edit your .bashrc to prepend the Anaconda3 install location to PATH:

export PATH=/opt/anaconda3/bin:$PATH

Thank you for installing Anaconda3!

然後詢問是否安裝VSCode,填no並回車,表示不安裝:

Do you wish to proceed with the installation of Microsoft VSCode? [yes|no]
>>> no

此時安裝程序已經結束,然後我們執行:

echo ". /opt/anaconda3/etc/profile.d/conda.sh" >> ~/.bashrc
sudo ln -s /opt/anaconda3/etc/profile.d/conda.sh /etc/profile.d/conda.sh

注意:/opt/anaconda3換成你所安裝的位置。

使用anaconda

退出shell後,重新登陸,然後就可以使用anaconda了:

進入環境:conda activate
退出環境:conda deactivate
[root@VM_0_9_centos ~]# conda activate  
(base) [root@VM_0_9_centos ~]# python
Python 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56) 
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
(base) [root@VM_0_9_centos ~]# conda deactivate
[root@VM_0_9_centos ~]# python          
Python 2.7.5 (default, Jun 20 2019, 20:27:34) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
[root@VM_0_9_centos ~]# 
image-20200510190610880

配置jupyter支持遠程訪問

生成jupyter默認配置:

jupyter notebook --generate-config

(base) [root@VM_0_9_centos ipython]# jupyter notebook --generate-config
Writing default config to: /root/.jupyter/jupyter_notebook_config.py

修改配置:

(base) [root@VM_0_9_centos ipython]# vi ~/.jupyter/jupyter_notebook_config.py

取消c.NotebookApp.ip = 'localhost'的註釋並改成c.NotebookApp.ip = '*'即可。

運行jupyter:

jupyter notebook --allow-root
image-20200510191909321

然後本地順利訪問到遠程主機上的jupyter:

image-20200510192030110

jupyter當前主目錄取決於命令執行時所在的目錄。

配置jupyter使用密碼訪問

將要配置的密碼使用passwd函數處理:

(base) [root@VM_0_9_centos bak]# python
Python 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56) 
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from notebook.auth import passwd
>>> passwd()
Enter password: 
Verify password: 
'sha1:13c0e9e600be:b1caae19615389900ba30f072caf1c5f4dd388b1'
>>> 

上面Enter password和Verify password的輸入不會回想,兩次輸入你需要配置的密碼回車,即可得到sha1碼。

例如我配置的密碼是123456,則sha1碼是sha1:13c0e9e600be:b1caae19615389900ba30f072caf1c5f4dd388b1

再編輯下jupyter配置:

vi ~/.jupyter/jupyter_notebook_config.py

取消c.NotebookApp.password = ''的註釋,改爲c.NotebookApp.password = 'sha1:13c0e9e600be:b1caae19615389900ba30f072caf1c5f4dd388b1'

其他配置:

c.NotebookApp.allow_root = True : root用戶下執行不需要加--allow-root參數

c.NotebookApp.open_browser = False : 啓動默認不打開遊覽器

再次運行jupyter:

jupyter notebook
image-20200510194324065

此時再訪問jupyter,需要輸入密碼:

image-20200510194506678

後臺運行jupyter

讓jupyter即使關閉shell也後臺運行:

nohup jupyter notebook > jupyter.log 2>&1 &

關閉後臺運行的jupyter:

(base) [root@VM_0_9_centos bak]# ps -ef|grep jupyter
root      3646 28946  1 19:47 pts/1    00:00:00 /opt/anaconda3/bin/python /opt/anaconda3/bin/jupyter-notebook
root      3771 28946  0 19:48 pts/1    00:00:00 grep --color=auto jupyter
(base) [root@VM_0_9_centos bak]# kill 3646   
(base) [root@VM_0_9_centos bak]# ps -ef|grep jupyter
root      3861 28946  0 19:48 pts/1    00:00:00 grep --color=auto jupyter
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章