Jupyter Notebook es una herramienta de desarrollo de código abierto y basada en la web que le permite crear y compartir códigos y ecuaciones en vivo. Es flexible, extensible y admite muchos lenguajes de programación, incluidos Python, Julia, Haskell, Ruby y más. Generalmente, se utiliza para datos y aprendizaje automático. Viene con el kernel de IPython que le permite escribir sus programas en Python.
En esta publicación, le mostraremos cómo instalar Jupyter Notebook en Ubuntu 20.04.
Requisitos
- Un Ubuntu 20.04 VPS (usaremos nuestro plan SSD 2 VPS)
- Acceso a la cuenta de usuario root (o acceso a una cuenta de administrador con privilegios de root)
Inicie sesión en el servidor y actualice los paquetes del sistema operativo del servidor
Primero, inicie sesión en su servidor Ubuntu 20.04 a través de SSH como usuario raíz:
ssh root@IP_Address -p Port_number
Una vez que haya iniciado sesión, actualice los paquetes de su sistema con el siguiente comando:
apt-get update -y
Instalar dependencias requeridas
Jupyter Notebook está basado en Python. Por lo tanto, deberá instalar Python y otras bibliotecas en su sistema. Puede instalarlos todos con el siguiente comando:
apt-get install python3 python3-pip python3-dev -y
A continuación, verifique la versión instalada de Python con el siguiente comando:
python3 --version
Salida:
Python 3.8.5
A continuación, actualice el PIP a la última versión con el siguiente comando:
pip3 install --upgrade pip
A continuación, verifique la versión de PIP con el siguiente comando:
pip3 --version
Salida:
pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)
A continuación, instale el paquete de entorno virtual con el siguiente comando:
pip3 install virtualenv
Instalar Jupyter Notebook
Primero, cree un usuario separado para Jupyter Notebook. Puedes crearlo con el siguiente comando:
adduser jupyter
A continuación, inicie sesión con el usuario de Jupyter con el siguiente comando:
su - jupyter
A continuación, cree un directorio de Jupyter y cree un entorno virtual para el cuaderno de Jupyter.
mkdir ~/Jupyter cd ~/Jupyter virtualenv notebook
A continuación, active el entorno virtual con el siguiente comando:
source notebook/bin/activate
A continuación, instale Jupyter con el siguiente comando:
(notebook) jupyter@jupyter:~/Jupyter$ pip install jupyter
Una vez instalado, ejecute el cuaderno Jupyter con el siguiente comando:
(notebook) jupyter@jupyter:~/Jupyter$ jupyter notebook
Deberías obtener el siguiente resultado:
[I 06:12:57.527 NotebookApp] Writing notebook server cookie secret to /home/jupyter/.local/share/jupyter/runtime/notebook_cookie_secret [I 06:12:57.786 NotebookApp] Serving notebooks from local directory: /home/jupyter/Jupyter [I 06:12:57.786 NotebookApp] Jupyter Notebook 6.3.0 is running at: [I 06:12:57.787 NotebookApp] http://localhost:8888/?token=9a7e565bbe40a36e7afbcd9cda890823288c61312e9f1eed [I 06:12:57.787 NotebookApp] or http://127.0.0.1:8888/?token=9a7e565bbe40a36e7afbcd9cda890823288c61312e9f1eed [I 06:12:57.787 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation). [W 06:12:57.791 NotebookApp] No web browser found: could not locate runnable browser. [C 06:12:57.791 NotebookApp] To access the notebook, open this file in a browser: file:///home/jupyter/.local/share/jupyter/runtime/nbserver-11919-open.html Or copy and paste one of these URLs: http://localhost:8888/?token=9a7e565bbe40a36e7afbcd9cda890823288c61312e9f1eed or http://127.0.0.1:8888/?token=9a7e565bbe40a36e7afbcd9cda890823288c61312e9f1eed
Presione CTRL+C para detener Jupyter Notebook.
A continuación, deberá generar un archivo de configuración para Jupyter Notebook. Puede generarlo con el siguiente comando:
(notebook) jupyter@jupyter:~/Jupyter$ jupyter notebook --generate-config
Deberías obtener el siguiente resultado:
Writing default config to: /home/jupyter/.jupyter/jupyter_notebook_config.py
A continuación, establezca la contraseña de Jupyter con el siguiente comando:
(notebook) jupyter@jupyter:~/Jupyter$ jupyter notebook password
Debería ver el siguiente resultado:
Enter password: Verify password: [NotebookPasswordApp] Wrote hashed password to /home/jupyter/.jupyter/jupyter_notebook_config.json
A continuación, desactívelo desde el entorno virtual de Python con el siguiente comando:
(notebook) jupyter@jupyter:~/Jupyter$ deactivate
A continuación, cierre la sesión del usuario de Jupyter con el siguiente comando:
exit
Crear un archivo de servicio de Systemd para Jupyter
A continuación, deberá crear un archivo de servicio systemd para administrar el Jupyter Notebook. Puedes crearlo con el siguiente comando:
nano /etc/systemd/system/jupyter.service
Agregue las siguientes líneas:
[Unit] Description=Jupyter Notebook [Service] Type=simple PIDFile=/run/jupyter.pid ExecStart=/home/jupyter/Jupyter/notebook/bin/jupyter-notebook --config=/home/jupyter/.jupyter/jupyter_notebook_config.py User=jupyter Group=jupyter WorkingDirectory=/home/jupyter/Jupyter/notebook Restart=always RestartSec=10 [Install] WantedBy=multi-user.target
Guarde y cierre el archivo, luego vuelva a cargar el demonio systemd con el siguiente comando:
systemctl daemon-reload
A continuación, inicie el servicio Jupyter y habilítelo para que se inicie al reiniciar el sistema con el siguiente comando:
systemctl start jupyter systemctl enable jupyter
También puede verificar el estado de Jupyter con el siguiente comando:
systemctl status jupyter
Salida:
● jupyter.service - Jupyter Notebook Loaded: loaded (/etc/systemd/system/jupyter.service; disabled; vendor preset: enabled) Active: active (running) since Tue 2021-04-06 06:32:47 UTC; 2min 33s ago Main PID: 14630 (jupyter-noteboo) Tasks: 1 (limit: 2353) Memory: 40.6M CGroup: /system.slice/jupyter.service └─14630 /home/jupyter/Jupyter/notebook/bin/python /home/jupyter/Jupyter/notebook/bin/jupyter-notebook --config=/home/jupyter/.jup> Apr 06 06:32:47 jupyter systemd[1]: Started Jupyter Notebook. Apr 06 06:32:48 jupyter jupyter-notebook[14630]: [I 06:32:48.023 NotebookApp] Serving notebooks from local directory: /home/jupyter/Jupyter/no> Apr 06 06:32:48 jupyter jupyter-notebook[14630]: [I 06:32:48.024 NotebookApp] Jupyter Notebook 6.3.0 is running at: Apr 06 06:32:48 jupyter jupyter-notebook[14630]: [I 06:32:48.024 NotebookApp] http://localhost:8888/ Apr 06 06:32:48 jupyter jupyter-notebook[14630]: [I 06:32:48.024 NotebookApp] Use Control-C to stop this server and shut down all kernels (twi> Apr 06 06:32:48 jupyter jupyter-notebook[14630]: [W 06:32:48.030 NotebookApp] No web browser found: could not locate runnable browser.
Configurar Nginx como proxy inverso para Jupyter Notebook
A continuación, deberá configurar Nginx como proxy inverso para Jupyter Notebook.
Primero, instale Nginx con el siguiente comando:
apt-get install nginx -y
Una vez instalado, cree un archivo de configuración de host virtual Nginx con el siguiente comando:
nano /etc/nginx/conf.d/jupyter.conf
Agregue las siguientes líneas:
upstream jupyter { 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; } }
Guarde y cierre el archivo, luego verifique el Nginx con el siguiente comando:
nginx -t
Salida:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
A continuación, reinicie Nginx para aplicar los cambios:
systemctl restart nginx
A continuación, agregue el usuario de Jupyter al grupo www-data con el siguiente comando:
usermode -g www-data jupyter
A continuación, edite el archivo de configuración de Jupyter Notebook y permita el acceso remoto.
nano /home/jupyter/Jupyter/.jupyter/jupyter_notebook_config.py
Cambie la siguiente línea:
c.NotebookApp.allow_remote_access = True
Guarde y cierre el archivo, luego reinicie el servicio Jupyter para aplicar los cambios:
systemctl restart jupyter
Acceder a Jupyter Notebook
Ahora, abra su navegador web y acceda a Jupyter Notebook usando la URL http://jupyter.example.com . Se le pedirá que proporcione una contraseña como se muestra a continuación:
Proporcione su contraseña y haga clic en Iniciar sesión botón. Debería ver el tablero de Jupyter Notebook en la siguiente pantalla:
¡Felicidades! Ha instalado correctamente Jupyter Notebook en Ubuntu 20.04 VPS.
Ahora que sabe cómo instalar Jupyter Notebook en Ubuntu 20.04, puede ver cómo nuestro alojamiento VPS Linux administrado puede ser fácil y asequible. Realmente no necesita instalar Jupyter Notebook en Ubuntu usted mismo, simplemente pídale a uno de nuestros administradores de sistemas que lo instale por usted, completamente gratis. Están disponibles las 24 horas del día, los 7 días de la semana, los 365 días del año y listos para ayudarlo con cualquier aspecto de la administración de su servidor.