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
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
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
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.
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.
Now that you have access, try logging in with the login password you set earlier.
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.
I was able to access it safely.
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.
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.
Recommended Posts