Prepare Python3 environment with CentOS7

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.

--Installing Anaconda --Installing Qiskit --Building JupyterHub --JupyterHub 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.

First of all, update it.

Execution command


# yum update 

Next, create a user for jupyter.

Create user


# useradd jupyter
# passwd jupyter
Change password for user jupyter.
new password:
Please re-enter your new password:
passwd:All authentication tokens have been successfully renewed.

Install Anaconda

First, download the version of the installer you want to install.

Download Anaconda installation shell


# wget https://repo.continuum.io/archive/Anaconda3-5.3.1-Linux-x86_64.sh

Next, run the downloaded installer. At the time of initial setting, you will be asked for the installation directory, but set / opt / anaconda3 / instead of the initial setting. Other than that, proceed with yes.

Run the installation shell


# bash Anaconda3-5.3.1-Linux-x86_64.sh
...
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/  <-Change the installation directory

If you do not reload the shell once, the path is not loaded and the conda command may not be available, so reload it.

Reload shell profile


# source ~/.bashrc

After that, check the version as a trial.

Anaconda version check


# conda --version
conda 4.8.4

Anaconda is installed and version information is displayed.

Install Qiskit

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

First, install gcc.

gcc installation


# yum install gcc

Next, start the virtual environment of Anaconda and install qiskit there.

Install Qiskit


# conda create -n myenv python=3
# source activate myenv
(myenv) # pip install qiskit

Check if it is installed.

Qiskit installation confirmation


(myenv) # 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


(myenv) # python
Python 3.8.5 (default, Sep  4 2020, 07:30:14) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.

Import Qiskit and execute the version confirmation command.

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.

Install JupyterHub

Install JupyterHub


# conda install -c conda-forge jupyterhub
# conda install notebook

After the installation is complete, create a config file.

Configuration file generation


(myenv) # cd /opt/
(myenv) # jupyterhub --generate-config
Writing default config to: jupyterhub_config.py
# ls -l
44 in total
drwxr-xr-x.26 root root 4096 September 18 18:49 anaconda3
-rw-r--r--.1 root root 40275 September 18 19:00 jupyterhub_config.py
drwxr-xr-x.2 root root 6 October 31 2018 rh

Once the config file is created, edit it.

Edit config file


(myenv) # vim jupyterhub_config.py 

Add the following contents. The bottom two lines are the settings required for service conversion.

/opt/jupyterhub_config.py


c.Spawner.notebook_dir = '~/notebook'

c.Authenticator.whitelist = {'jupyter'}

c.Authenticator.admin_users = {'root'}

c.ConfigurableHTTPProxy.command = '/opt/anaconda3/envs/myenv/bin/configurable-http-proxy'

c.Spawner.cmd = ['/opt/anaconda3/envs/myenv/bin/jupyterhub-singleuser']

Next, create a directory for users who use JupyterHub.

Create a directory for users


# mkdir -p /home/jupyter/notebook
# chown jupyter:jupyter /home/jupyter/notebook

Start JupyterHub.

Launch Jupyter Hub


# jupyterhub &

JupyterHub runs on port 8000, so allow port 8000.

Firewall permission settings


# firewall-cmd --add-port=8000/tcp --permanent 
success
# firewall-cmd --reload 
success

Try to access http://192.168.249.130:8000/ from the host machine.

jupyterhub.png

I was able to access it.

Next, try logging in as a jupyter user.

jupyterhub_loggedin.png

I was able to log in.

Check if Qiskit can be executed.

jupyterhub_qiskit.png

I was able to execute it.

Making JupyterHub a service

Finally, make it a service.

Make various settings.

settings for jupyterhub service


# conda install --quiet --yes notebook jupyterhub nodejs configurable-http-proxy
# npm install -g configurable-http-proxy
# ln -s /usr/bin/nodejs /usr/bin/node
# npm -v
# npm cache clean -f
# npm install -g n
# n stable

Check the path of the jupyterhub command to create the service registration definition file.

Check the path of jupyterhub


(myenv) # which jupyterhub
/opt/anaconda3/envs/myenv/bin/jupyterhub

Once the information is obtained, create a definition file.

Creating a definition file


(myenv) # vim /etc/systemd/system/jupyterhub.service

Add the following contents.

/etc/systemd/system/jupyterhub.service


[Unit]
Description = JupyterHub

[Service]
Type=simple
PIDFile=/var/run/jupyterhub.pid
ExecStart=/opt/anaconda3/envs/myenv/bin/jupyterhub
WorkingDirectory=/opt/
Restart=always

[Install]
WantedBy = multi-user.target

When the definition file is completed, start reading.

Read definition file


# systemctl daemon-reload

After reading, start the service.

Service start


# systemctl start jupyterhub.service

Check the service status. There were some errors, but I was able to start up and log in. Qiskit is also working fine.

Service status check


(myenv) # systemctl status jupyterhub.service
● jupyterhub.service - JupyterHub
   Loaded: loaded (/etc/systemd/system/jupyterhub.service; enabled; vendor preset: disabled)
   Active: active (running)since gold 2020-09-18 21:27:20 JST; 7min ago
 Main PID: 3291 (python)
    Tasks: 7
   CGroup: /system.slice/jupyterhub.service
           └─3307 node /opt/anaconda3/envs/myenv/bin/configurable-http-proxy --ip --port 8000 --api-ip 127.0.0.1 --api-port 8001 --error-target http://...
           ‣ 3291 /opt/anaconda3/envs/myenv/bin/python /opt/anaconda3/envs/myenv/bin/jupyterhub

September 18 21:27:21 localhost.localdomain jupyterhub[3291]: 21:27:21.575 [ConfigProxy] info: 201 POST /api/routes/
September 18 21:27:21 localhost.localdomain jupyterhub[3291]: [I 2020-09-18 21:27:21.577 JupyterHub app:2422] JupyterHub is now running at http://:8000
September 18 21:27:29 localhost.localdomain jupyterhub[3291]: [I 2020-09-18 21:27:29.928 JupyterHub log:174] 200 GET /hub/spawn-pending/jupyter (jup...29.88ms
September 18 21:27:33 localhost.localdomain jupyterhub[3291]: [I 2020-09-18 21:27:33.252 JupyterHub login:43] User logged out: jupyter
September 18 21:27:33 localhost.localdomain jupyterhub[3291]: [I 2020-09-18 21:27:33.262 JupyterHub log:174] 302 GET /hub/logout -> /hub/login (jupy...16.03ms
September 18 21:27:33 localhost.localdomain jupyterhub[3291]: [I 2020-09-18 21:27:33.276 JupyterHub log:174] 200 GET /hub/login (@::ffff:192.168.249.1) 8.34ms
September 18 21:27:40 localhost.localdomain jupyterhub[3291]: [I 2020-09-18 21:27:40.212 JupyterHub base:663] User logged in: jupyter
September 18 21:27:40 localhost.localdomain jupyterhub[3291]: [I 2020-09-18 21:27:40.213 JupyterHub log:174] 302 POST /hub/login?next= -> /hub/spawn...12.63ms
September 18 21:27:40 localhost.localdomain python[3291]: pam_loginuid(login:session): Error writing /proc/self/loginuid: Operation not permitted
September 18 21:27:40 localhost.localdomain python[3291]: pam_loginuid(login:session): set_loginuid failed
Hint: Some lines were ellipsized, use -l to show in full.

Finally, enable service automation. With this, even if the OS shuts down, the service will start as soon as it starts, and you can access Jupyter from your Web browser at any time as before.

Enable and verify autorun


# systemctl enable jupyterhub
# systemctl is-enabled jupyterhub
enabled

In fact, after restarting the guest OS, I tried to access it from the Web browser of the host machine, and I was able to access and log in successfully.

in conclusion

I am 90% satisfied with the fact that I was able to create an environment in which all users can use Qiskit, which I wanted to do this time, and have them use it through authentication from a Web browser. However, there are some errors and deficiencies in the environment construction, so I would like to work on those areas in the future.

While I wasn't motivated, I was able to put it all together in half a day, so it was just right for warming up after tomorrow. Although the service of Jupyter Notebook went smoothly, the service of JupyterHub caused errors one after another and it was difficult to solve one, so I felt that I would not want to implement it without anything again. (There is this article for that)

Future tasks

--Investigation and resolution of the cause of the error --JupyterHub TLS conversion --Implementation of settings that do not cause any problems in the production environment (from a setting survey in the first place)

[Reference] Errors that occurred

References

Recommended Posts

Prepare Python3 environment with CentOS7
Prepare the environment of CentOS 8 with Sakura VPS
Prepare Java development environment with Atom
[Vagrant] Prepare LAMP development environment with Vagrant (centos + apache + MySQL + PHP)
Build an environment where pip3 can be used with CentOS7 + Python3
Prepare Java development environment with VS Code
Prepare the format environment with "Rails" (VScode)
[Memo] Create a CentOS 8 environment easily with Docker
Prepare the JVM language development environment with WSL
Prepare a scraping environment with Docker and Java
Use CentOS with LXD
Install Python 3 on CentOS 7
Build Growai with Centos7
[Note] Build a Python3 environment with Docker in EC2
Prepare the environment for java11 and javaFx with Ubuntu 18.4
Comfortable Docker environment created with WSL2 CentOS7 and Docker Desktop
Pytorch execution environment with Docker
Network install CentOS 8 with Kickstart.
Build softether VPN with Centos7.
[Docker] Rails 5.2 environment construction with docker
Build docker environment with WSL
Build a CentOS 8 virtual environment on your Mac with VirtualBox
Exciting environment check with mkmf
React environment construction with Docker
Offline environment construction CentOS edition
Prepare a transcendentally simple PHP & Apache environment on Mac with Docker
Rails + MySQL environment construction with Docker
Create a Vue3 environment with Docker!
Node.js environment construction with Docker Compose
Build Couchbase local environment with Docker
Let's create Ubuntu environment with vmware
Environment construction with Docker for beginners
Create RUNTEQ's environment with Windows DockerDesktop
Build PlantUML environment with VSCode + Docker
Install CentOS Stream 8 in Hyper-V environment
Build environment with vue.js + rails + docker
Build Rails environment with Docker Compose
Create SolrCloud verification environment with Docker
WordPress with Docker Compose on CentOS 8
Create Laravel environment with Docker (docker-compose)
Build jooby development environment with Eclipse
[Environment construction with Docker] Rails 6 & MySQL 8
CentOS8 + Anaconda 4.9.2 + jupyterhub environment construction procedure
Build docker + laravel environment with laradock
Switch with CentOS7 series + JDK-9.0.1 + JDK1.8.0 + alternatives
Build a development environment on AWS EC2 with CentOS7 + Nginx + pm2 + Nuxt.js