Make Jupyter Notebook a service on CentOS

Introduction

Make a note of the procedure for building a Python environment on Cent OS. This is just a memo about the construction procedure, so when actually building in the production environment, this information is not used to build and operate the production environment. The environment is as follows, and it is built with a virtual machine on VMware.

What I want to do is as follows.

--Install Python 3 --Installing Qiskit --Installing Anaconda --Installing Jupyter --Jupyter service

From the following procedure, it is assumed that the OS is already installed. As a convention, \ # is the root user, $ is the general user, and >>> is the prompt to indicate that python is running. Since it is the first time to carry out, the detour and setting cover are excellent.

First of all, update it.

OS update


# yum update 

Installation of Python 3

I want to use Python 3 instead of Python 2, which can be used as standard, so install it.

Execution command


# yum update python3

After installation, version information including installation confirmation is displayed. You can see that Python 3.6.8 is installed.

Python version check


# python3 --version
Python 3.6.8

Also, since we want to use python3 introduced this time with the python command, set it.

Profile editing


# vim ~/.bashrc

Add the following contents to the end of .bashrc. After adding, log out once and log in again.

/root/.bashrc


alias python="python3" 
alias pip="pip3"

As a test, check the version with the python command. You can see that python3 is specified.

Python version check


# python --version
Python 3.6.8

Install Anaconda

Since git is required to install pyenv, first install git.

git installation


# yum install git

Install pyenv.

Install pyenv


# git clone https://github.com/yyuu/pyenv.git ~/.pyenv
# pyenv install -l | grep anaconda
  anaconda-1.4.0
  anaconda-1.5.0
  anaconda-1.5.1
  anaconda-1.6.0
  anaconda-1.6.1
  anaconda-1.7.0
  anaconda-1.8.0
  anaconda-1.9.0
  anaconda-1.9.1
  anaconda-1.9.2
  anaconda-2.0.0
  anaconda-2.0.1
  anaconda-2.1.0
  anaconda-2.2.0
  anaconda-2.3.0
  anaconda-2.4.0
  anaconda-4.0.0
  anaconda2-2.4.0
  anaconda2-2.4.1
  anaconda2-2.5.0
  anaconda2-4.0.0
  anaconda2-4.1.0
  anaconda2-4.1.1
  anaconda2-4.2.0
  anaconda2-4.3.0
  anaconda2-4.3.1
  anaconda2-4.4.0
  anaconda2-5.0.0
  anaconda2-5.0.1
  anaconda2-5.1.0
  anaconda2-5.2.0
  anaconda2-5.3.0
  anaconda2-5.3.1
  anaconda2-2018.12
  anaconda2-2019.03
  anaconda2-2019.07
  anaconda3-2.0.0
  anaconda3-2.0.1
  anaconda3-2.1.0
  anaconda3-2.2.0
  anaconda3-2.3.0
  anaconda3-2.4.0
  anaconda3-2.4.1
  anaconda3-2.5.0
  anaconda3-4.0.0
  anaconda3-4.1.0
  anaconda3-4.1.1
  anaconda3-4.2.0
  anaconda3-4.3.0
  anaconda3-4.3.1
  anaconda3-4.4.0
  anaconda3-5.0.0
  anaconda3-5.0.1
  anaconda3-5.1.0
  anaconda3-5.2.0
  anaconda3-5.3.0
  anaconda3-5.3.1
  anaconda3-2018.12
  anaconda3-2019.03
  anaconda3-2019.07
  anaconda3-2019.10
  anaconda3-2020.02

Install the latest anaconda.

Installation of anaconda


# pyenv install anaconda3-5.3.1

Make settings.

anaconda settings


# pyenv rehash
# pyenv global anaconda3-5.3.1
# echo 'export PATH="$PYENV_ROOT/versions/anaconda3-5.3.1/bin/:$PATH"' >> ~/.bashrc
# source ~/.bashrc
# conda update conda

Check the version.

Check the version of conda


# conda --version
conda 4.8.4

Install Qiskit

Now that the installation of anaconda is complete, install Qiskit next.

Install Qiskit


# pip install qiskit

Check if it is installed.

Qiskit installation confirmation


# conda list | grep qiskit
qiskit                    0.21.0                   pypi_0    pypi
qiskit-aer                0.6.1                    pypi_0    pypi
qiskit-aqua               0.7.5                    pypi_0    pypi
qiskit-ibmq-provider      0.9.0                    pypi_0    pypi
qiskit-ignis              0.4.0                    pypi_0    pypi
qiskit-terra              0.15.2                   pypi_0    pypi

Next, check the installed version of Qiskit.

python run


# python

Check the version of Qiskit


>>> import qiskit
>>> qiskit.__qiskit_version__
{'qiskit-terra': '0.15.2', 'qiskit-aer': '0.6.1', 'qiskit-ignis': '0.4.0', 'qiskit-ibmq-provider': '0.9.0', 'qiskit-aqua': '0.7.5', 'qiskit': '0.21.0'}

When the confirmation is completed, press Ctrl-D to quit python.

Jupyter installation

Next, try installing jupyter. When I tried to complete the command, it seems that jupyter was already installed, so when I ran it, the following error was output.

jupyter execution error


# jupyter --version
Traceback (most recent call last):
  File "/root/.pyenv/versions/anaconda3-5.3.1/bin/jupyter", line 7, in <module>
    from jupyter_core.command import main
ModuleNotFoundError: No module named 'jupyter_core'

Try installing jupyter from conda once.

install jupyter


# conda install jupyter

Then, the following error is output and the installation is not completed.

Error when installing jupyter


failed with initial frozen solve. Retrying with flexible solve.
Solving environment: -forced termination

I tried google and executed the following command.

update of conda


# conda update --all

I got an error with the above command. Next, I executed the following command as a trial.

OpenCV installation


# conda install -c menpo opencv

This worked fine. Try installing jupyter again.

Jupyter installation re-challenge


# conda install jupyter
Collecting package metadata (repodata.json): done
Solving environment: done

# All requested packages already installed.

Looks like it's installed properly. Check the version information.

Check the version of jupyter


# jupyter --version
4.4.0

It is installed properly and works.

Set up Jupyter Notebook.

Preparing to start jupyter


# jupyter --path
config:
    /root/.jupyter
    /root/.pyenv/versions/anaconda3-5.3.1/etc/jupyter
    /usr/local/etc/jupyter
    /etc/jupyter
data:
    /root/.local/share/jupyter
    /root/.pyenv/versions/anaconda3-5.3.1/share/jupyter
    /usr/local/share/jupyter
    /usr/share/jupyter
runtime:
    /run/user/1000/jupyter
# mkdir ~/.jupyter
# touch ~/.jupyter/jupyter_notebook_config.py
# mkdir ~/jupyter_files
# touch ~/jupyter_files/test.py
# ipython

Setting login password


Python 3.7.0 (default, Oct  9 2018, 10:31:47) 
Type 'copyright', 'credits' or 'license' for more information
IPython 6.5.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from notebook.auth import passwd

In [2]: passwd()
Enter password: 
Verify password: 
Out[2]: 'sha1:da72b0981831:41b14f79bbfeda08322cfdb3a056a58fc70c65ea'

In [3]: exit

Make a note of the hash value of the Out [2] password in a text editor, etc., as it will be used in the next editing of the configuration file.

Edit config file


vim ~/.jupyter/jupyter_notebook_config.py

Add the following to the config file. For the hash value of the password, copy and paste the value output in the login password setting.

py:/root/.jupyter/jupyter_notebook_config.py


c = get_config()

c.NotebookApp.ip = '*'
c.NotebookApp.open_browser = False
c.NotebookApp.port = 8888
c.NotebookApp.password = u'sha1:da72b0981831:41b14f79bbfeda08322cfdb3a056a58fc70c65ea'
c.NotebookApp.notebook_dir = '/root/jupyter_files/'

Start Jupyter Notebook. This environment is a trial environment and is set by the root user, so execute the following command.

Launch notebook


# jupyter notebook --allow-root

After that, try accessing http: // localhost: 8888 / using a Web browser.

JN.png

Now that you have access, try logging in with the login password you set earlier.

JNlogined.png

I was able to log in successfully. You can also check the confirmation file test.py created earlier.

Next, make it accessible using the Web browser of the host machine. Allow port 8888 on the firewall.

Firewall permission settings


# firewall-cmd --add-port=8888/tcp --zone=public --permanent
success
# firewall-cmd --reload
success

Try accessing http://192.168.249.129:8888/ from the web browser of the host machine.

JN_host.png

I was able to access it safely.

Jupyter service

Set Jupyter so that it can be controlled by systemd. First, check the path of jupyter.

Jupyter path confirmation


# which jupyter
/root/.pyenv/versions/anaconda3-5.3.1/bin/jupyter

Creating a definition file


# vim /etc/systemd/system/notebook.service

* The following content cannot be executed successfully </ b>

/etc/systemd/system/notebook.service


[Unit]
Description = Jupyter Notebook

[Service]
Type=simple
PIDFile=/var/run/jupyter-notebook.pid
ExecStart=/root/.pyenv/versions/anaconda3-5.3.1/bin/jupyter notebook
WorkingDirectory=/root/
User=root
Group=root
Restart=always

[Install]
WantedBy = multi-user.target

Reload configuration file


# systemctl daemon-reload

Service start


# systemctl start notebook

When I checked the status of the service, it failed.

Service status check(Failure)


# systemctl status notebook
● notebook.service - Jupyter Notebook
   Loaded: loaded (/etc/systemd/system/notebook.service; disabled; vendor preset: disabled)
   Active: failed (Result: start-limit)since gold 2020-09-18 15:21:44 JST; 1min 8s ago
  Process: 18766 ExecStart=/root/.pyenv/versions/anaconda3-5.3.1/bin/jupyter notebook (code=exited, status=1/FAILURE)
 Main PID: 18766 (code=exited, status=1/FAILURE)

September 18 15:21:44 localhost.localdomain systemd[1]: notebook.service: main process exited, code=exited, status=1/FAILURE
September 18 15:21:44 localhost.localdomain systemd[1]: Unit notebook.service entered failed state.
September 18 15:21:44 localhost.localdomain systemd[1]: notebook.service failed.
September 18 15:21:44 localhost.localdomain systemd[1]: notebook.service holdoff time over, scheduling restart.
September 18 15:21:44 localhost.localdomain systemd[1]: Stopped Jupyter Notebook.
September 18 15:21:44 localhost.localdomain systemd[1]: start request repeated too quickly for notebook.service
September 18 15:21:44 localhost.localdomain systemd[1]: Failed to start Jupyter Notebook.
September 18 15:21:44 localhost.localdomain systemd[1]: Unit notebook.service entered failed state.
September 18 15:21:44 localhost.localdomain systemd[1]: notebook.service failed.

When I thought about the cause, I remembered that --allow-root was given at the first startup, but I forgot to give it in this configuration file. Re-edit the configuration file as follows.

/etc/systemd/system/notebook.service


[Unit]
Description = Jupyter Notebook

[Service]
Type=simple
PIDFile=/var/run/jupyter-notebook.pid
ExecStart=/root/.pyenv/versions/anaconda3-5.3.1/bin/jupyter notebook --allow-root
WorkingDirectory=/root/
User=root
Group=root
Restart=always

[Install]
WantedBy = multi-user.target

Read the configuration file again and start the service.

Reload configuration file and start service


# systemctl daemon-reload
# systemctl start notebook

Check the service status.

Service status check(success)


# systemctl status notebook
● notebook.service - Jupyter Notebook
   Loaded: loaded (/etc/systemd/system/notebook.service; enabled; vendor preset: disabled)
   Active: active (running)since gold 2020-09-18 15:29:38 JST; 3min 8s ago
 Main PID: 19068 (jupyter-noteboo)
    Tasks: 1
   CGroup: /system.slice/notebook.service
           └─19068 /root/.pyenv/versions/anaconda3-5.3.1/bin/python /root/.pyenv/versions/anaconda3-5.3.1/bin/jupyter-notebook --...

September 18 15:29:38 localhost.localdomain systemd[1]: Started Jupyter Notebook.
September 18 15:29:39 localhost.localdomain jupyter[19068]: [W 15:29:39.391 NotebookApp] WARNING: The notebook server is listeni...nded.
September 18 15:29:39 localhost.localdomain jupyter[19068]: [I 15:29:39.393 NotebookApp] The port 8888 is already in use, trying...port.
September 18 15:29:39 localhost.localdomain jupyter[19068]: [I 15:29:39.423 NotebookApp] JupyterLab extension loaded from /root/...erlab
September 18 15:29:39 localhost.localdomain jupyter[19068]: [I 15:29:39.423 NotebookApp] JupyterLab application directory is /ro...r/lab
September 18 15:29:39 localhost.localdomain jupyter[19068]: [I 15:29:39.426 NotebookApp] Serving notebooks from local directory:...files
September 18 15:29:39 localhost.localdomain jupyter[19068]: [I 15:29:39.426 NotebookApp] The Jupyter Notebook is running at:
September 18 15:29:39 localhost.localdomain jupyter[19068]: [I 15:29:39.426 NotebookApp] http://(localhost.localdomain or 127.0....8889/
September 18 15:29:39 localhost.localdomain jupyter[19068]: [I 15:29:39.427 NotebookApp] Use Control-C to stop this server and s...ion).
Hint: Some lines were ellipsized, use -l to show in full.

This time I want to see it stand up safely. Enable autorun.

Enable and verify autorun


# systemctl enable notebook
# systemctl is-enabled notebook
enabled

Automatic startup has been enabled. For confirmation, after restarting the guest OS, I tried to access it from the host machine with the above URL, but I was able to access it safely. With this, Jupyter Notebook is always running and can be used using a Web browser.

in conclusion

I tried this time and thought that execution as root user is not recommended, and commands also require options, which is troublesome, so it is better to create a service user and execute the service as this user. Felt. Also, as I wrote in another article, I felt that JupyterHub was better for doing this.

References

  • https://qiita.com/iamdaisuke/items/3671032a18f5c4bf37d1
  • https://qiskit.org/documentation/locale/ja/install.html
  • https://ideanotes.cloud/2020/01/29/ubuntu-anaconda-opencv-failed/

Recommended Posts

Make Jupyter Notebook a service on CentOS
Make Unity Accelerator a service on Linux
Build jupyter notebook on remote server (CentOS)
Run Jupyter notebook on a remote server
Simply display a line graph on Jupyter Notebook
Introduced Jupyter Notebook to CentOS 7
High charts on Jupyter notebook
View PDF on Jupyter Notebook
Make Responder a daemon (service)
Run Jupyter Notebook on windows
A very convenient way to give a presentation on Jupyter Notebook
Try clustering with a mixed Gaussian model on a Jupyter Notebook
A note when I can't open Jupyter Notebook on Windows
Formatting with autopep8 on Jupyter notebook
Post a Jupyter Notebook as a blog post
Let's make a spot sale service 2
Let's make a spot sale service 1
Create a SlackBot service on Pepper
Introducing Binder, a free Jupyter service
Run azure ML on jupyter notebook
Create a python environment on centos
Try running Jupyter Notebook on Mac
Build a python3 environment on CentOS7
Let's make a spot sale service 3
Try to make a kernel of Jupyter
Try SVM with scikit-learn on Jupyter Notebook
Make a Notebook Pipeline with Kedro + Papermill
Start jupyter notebook on GPU server (remote server)
A memo with Python2.7 and Python3 on CentOS
Clone the github repository on jupyter notebook
GPU check of PC on jupyter notebook
Display histogram / scatter plot on Jupyter Notebook
Use vim keybindings on Docker-launched Jupyter Notebook
How to set up a jupyter notebook on ssh destination (AWS EC2)
Install matplotlib and display graph on Jupyter Notebook
Try a state-space model (Jupyter Notebook + IR kernel)
Jupyter Notebook memo
[Jupyter Notebook / Lab] 3 ways to debug on Jupyter [Pdb]
Introducing Jupyter Notebook
How to make multiple kernels selectable on Jupyter
Build a python environment with ansible on centos6
Powerful Jupyter Notebook
Enable Jupyter Notebook with conda on remote server
Golang on jupyter
LINUX: How to make arrow keys correspond to 2,4,6,8 on a notebook without a numeric keypad
[Pythonocc] I tried using CAD on jupyter notebook
Jupyter on AWS
Jupyter notebook password
Jupyter Notebook memo
Build a PYNQ environment on Ultra96 V2 and log in to Jupyter Notebook
Try Apache Spark on Jupyter Notebook (on local Docker
Remotely open Jupyter notebook launched on the server
jupyter notebook does not start on mac fish
From installing Flask on CentOS to making it a service with Nginx and uWSGI
Easily launch jupyter notebook on AWS and access locally
Let's make a spot sale service 9 (Task Queue edition)
Run Tensorflow from Jupyter Notebook on Bash on Ubuntu on Windows
Looking back on creating a web service with Django 1
Monitor the training model with TensorBord on Jupyter Notebook
Make your cursor a photo of your choice on Linux
Make a parrot return LINE Bot on AWS Cloud9