Come installare Jupyter Notebook su Ubuntu 22.04
Su questa pagina
- Prerequisiti
- Per iniziare
- Installa Python
- Installa Jupyter Notebook
- Genera la password del notebook Jupyter
- Crea un file di servizio Systemd per Jupyter Notebook
- Configura Nginx come proxy inverso per Jupyter Notebook
- Accedi a Jupyter Notebook
- Conclusione
Jupyter Notebook è una piattaforma di elaborazione interattiva gratuita, open source e basata sul Web che consente agli utenti di modificare ed eseguire documenti tramite un browser Web. È basato su Python e ti consente di creare e condividere documenti che contengono codice live, equazioni e visualizzazioni sul web. Il suo obiettivo è sviluppare software open source, standard aperti e servizi per l'elaborazione interattiva in più linguaggi di programmazione.
In questo tutorial, ti mostrerò come installare il software Jupyter Notebook su Ubuntu 22.04.
Prerequisiti
- Un server che esegue Ubuntu 22.04.
- Un nome di dominio valido viene indicato con l'IP del tuo server.
- Sul server è configurata una password di root.
Iniziare
Innanzitutto, si consiglia di aggiornare e aggiornare tutti i pacchetti software all'ultima versione. Puoi aggiornarli tutti eseguendo il seguente comando:
apt update -y
apt upgrade -y
Una volta aggiornati tutti i pacchetti, puoi procedere al passaggio successivo:
Installa Python
Innanzitutto, dovrai installare Python e altre dipendenze sul tuo server. Puoi installarli tutti eseguendo il seguente comando:
apt-get install python3 python3-pip -y
Una volta installati tutti i pacchetti, puoi verificare la versione di Python con il seguente comando:
python3 --version
Dovresti vedere il seguente output:
Python 3.10.6
Successivamente, aggiorna PIP all'ultima versione con il seguente comando:
pip3 install --upgrade pip
Successivamente, verifica la versione PIP utilizzando il seguente comando:
pip3 --version
Dovresti vedere il seguente output:
pip 22.2.2 from /usr/local/lib/python3.10/dist-packages/pip (python 3.10)
Quindi, installa il pacchetto dell'ambiente virtuale Python con il seguente comando:
pip3 install virtualenv
Al termine, puoi procedere al passaggio successivo.
Installa Jupyter Notebook
Innanzitutto, crea una directory per archiviare il tuo Notebook con il seguente comando:
mkdir ~/project
Successivamente, vai alla directory del progetto e crea un ambiente virtuale Python con il seguente comando:
cd ~/project
virtualenv notebookenv
Successivamente, attiva l'ambiente virtuale utilizzando il seguente comando:
source notebookenv/bin/activate
Successivamente, installa Jupyter Notebook con il seguente comando:
pip install jupyter
Una volta installato Jupyter Notebook, esegui Jupyter Notebook con il seguente comando:
jupyter notebook --allow-root
Se tutto va bene, otterrai il seguente output:
[I 14:23:26.729 NotebookApp] Serving notebooks from local directory: /root/project [I 14:23:26.729 NotebookApp] Jupyter Notebook 6.4.12 is running at: [I 14:23:26.729 NotebookApp] http://localhost:8888/?token=5dbdfdbf2e2dc72ccdaaa7361db8c55877ecfbc45676e625 [I 14:23:26.729 NotebookApp] or http://127.0.0.1:8888/?token=5dbdfdbf2e2dc72ccdaaa7361db8c55877ecfbc45676e625 [I 14:23:26.730 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation). [W 14:23:26.734 NotebookApp] No web browser found: could not locate runnable browser. [C 14:23:26.734 NotebookApp] To access the notebook, open this file in a browser: file:///root/.local/share/jupyter/runtime/nbserver-3214-open.html Or copy and paste one of these URLs: http://localhost:8888/?token=5dbdfdbf2e2dc72ccdaaa7361db8c55877ecfbc45676e625 or http://127.0.0.1:8888/?token=5dbdfdbf2e2dc72ccdaaa7361db8c55877ecfbc45676e625
Premere CTRL+C per arrestare Jupyter Notebook.
Al termine, puoi procedere al passaggio successivo.
Genera la password del notebook Jupyter
Si consiglia di proteggere Jupyter Notebook con una password. Innanzitutto, genera un file di configurazione di Jupyter Notebook con il seguente comando:
jupyter notebook --generate-config
Dovresti vedere il seguente output:
Writing default config to: /root/.jupyter/jupyter_notebook_config.py
Successivamente, imposta la password di Jupyter Notebook con il seguente comando:
jupyter notebook password
Imposta una password come mostrato di seguito:
Enter password: Verify password: [NotebookPasswordApp] Wrote hashed password to /root/.jupyter/jupyter_notebook_config.json
Successivamente, disattiva dall'ambiente virtuale Python con il seguente comando:
deactivate
Al termine, puoi procedere al passaggio successivo.
Crea un file di servizio Systemd per Jupyter Notebook
Successivamente, dovrai creare un file di servizio systemd per gestire Jupyter Notebook. Puoi crearlo con il seguente comando:
nano /etc/systemd/system/jupyter.service
Aggiungi i seguenti codici:
[Unit] Description=Jupyter Notebook [Service] Type=simple PIDFile=/run/jupyter.pid ExecStart=/root/project/notebookenv/bin/jupyter-notebook --config=/root/.jupyter/jupyter_notebook_config.py --allow-root User=root Group=root WorkingDirectory=/root/project/notebookenv Restart=always RestartSec=10 [Install] WantedBy=multi-user.target
Salva e chiudi il file, quindi ricarica il demone systemd con il seguente comando:
systemctl daemon-reload
Successivamente, avvia Jupyter Notebook e abilitalo per l'avvio al riavvio del sistema con il seguente comando:
systemctl start jupyter
systemctl enable jupyter
Per verificare lo stato di Jupyter Notebook, eseguire il comando seguente:
systemctl status jupyter
Dovresti vedere il seguente output:
? jupyter.service - Jupyter Notebook Loaded: loaded (/etc/systemd/system/jupyter.service; disabled; vendor preset: enabled) Active: active (running) since Tue 2022-10-11 14:27:01 UTC; 4s ago Main PID: 3649 (jupyter-noteboo) Tasks: 1 (limit: 2242) Memory: 53.4M CPU: 980ms CGroup: /system.slice/jupyter.service ??3649 /root/project/notebookenv/bin/python /root/project/notebookenv/bin/jupyter-notebook --config=/root/.jupyter/jupyter_noteb> Oct 11 14:27:01 ubuntu2204 systemd[1]: Started Jupyter Notebook. Oct 11 14:27:02 ubuntu2204 jupyter-notebook[3649]: [I 14:27:02.288 NotebookApp] Serving notebooks from local directory: /root/project/noteboo> Oct 11 14:27:02 ubuntu2204 jupyter-notebook[3649]: [I 14:27:02.289 NotebookApp] Jupyter Notebook 6.4.12 is running at: Oct 11 14:27:02 ubuntu2204 jupyter-notebook[3649]: [I 14:27:02.289 NotebookApp] http://localhost:8888/ Oct 11 14:27:02 ubuntu2204 jupyter-notebook[3649]: [I 14:27:02.290 NotebookApp] Use Control-C to stop this server and shut down all kernels (> Oct 11 14:27:02 ubuntu2204 jupyter-notebook[3649]: [W 14:27:02.293 NotebookApp] No web browser found: could not locate runnable browser.
Al termine, puoi procedere al passaggio successivo.
Configura Nginx come proxy inverso per Jupyter Notebook
A questo punto, Jupyter Notebook è avviato e in esecuzione sulla porta 8888. Ora, dovrai configurare Nginx come proxy inverso per accedere a Jupyter Notebook tramite la porta 80.
Innanzitutto, installa il pacchetto del server Web Nginx con il seguente comando:
apt-get install nginx -y
Una volta installato Nginx, crea un file di configurazione dell'host virtuale Nginx con il seguente comando:
nano /etc/nginx/conf.d/jupyter.conf
Aggiungi le seguenti configurazioni:
upstream notebook { server 127.0.0.1:8888; } server { listen 80; server_name jupyter.example.com; access_log /var/log/nginx/example.com.access.log; error_log /var/log/nginx/example.com.error.log; location / { proxy_pass http://localhost:8888; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $http_host; proxy_http_version 1.1; proxy_redirect off; proxy_buffering off; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_read_timeout 86400; } }
Salva e chiudi il file, quindi verifica Nginx per eventuali errori di sintassi utilizzando il seguente comando:
nginx -t
Dovresti vedere il seguente output:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Successivamente, riavvia il servizio Nginx per applicare le modifiche:
systemctl restart nginx
Puoi anche controllare lo stato di Nginx con il seguente comando:
systemctl status nginx
Otterrai il seguente output:
? nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2022-10-11 14:28:53 UTC; 54s ago Docs: man:nginx(8) Process: 4193 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Process: 4194 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Main PID: 4195 (nginx) Tasks: 2 (limit: 2242) Memory: 2.6M CPU: 40ms CGroup: /system.slice/nginx.service ??4195 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;" ??4196 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" Oct 11 14:28:53 ubuntu2204 systemd[1]: Starting A high performance web server and a reverse proxy server... Oct 11 14:28:53 ubuntu2204 systemd[1]: Started A high performance web server and a reverse proxy server.
Successivamente, aggiungi l'utente root al gruppo www-data utilizzando il seguente comando:
usermod -g www-data root
Successivamente, modifica il file di configurazione di Jupyter Notebook e abilita l'accesso remoto:
nano /root/.jupyter/jupyter_notebook_config.py
Modifica la riga seguente:
c.NotebookApp.allow_remote_access = True
Salva e chiudi il file, quindi riavvia il servizio Jupyter Notebook per applicare le modifiche:
systemctl restart jupyter
Al termine, puoi procedere al passaggio successivo.
Accedi a Jupyter Notebook
Ora apri il browser web e accedi a Jupyter Notebook utilizzando l'URL http://jupyter.example.com. Ti verrà chiesto di fornire la tua password Jupyter Notebook nella schermata seguente:
Fornisci la tua password e fai clic sul pulsante Accedi. Dovresti vedere la dashboard di Jupyter Notebook nella schermata seguente:
Conclusione
Congratulazioni! hai installato correttamente Jupyter Notebook con Nginx come proxy inverso su Ubuntu 22.04. Ora puoi creare, condividere ed eseguire il tuo Notebook su Internet. Non esitate a chiedermi se avete domande.