[PYTHON] The story of sharing the pyenv environment with multiple users

Overview

In a shared development environment, if you put pyenv for each user, the capacity will be great, so A story to put one in common so that it can be reused by general users.

The point

The method that docker also adopts.

environment

You can use docker, but since it's a big deal, I'll use vagrant.

$ vagrant -v
Vagrant 2.2.3

Vagrantfile


Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
end

Host side

Start up

$ vagrant up

Enter vm

$ vagrant ssh
Last login: Wed Feb  5 03:53:55 2020 from 10.0.2.2
[vagrant@localhost ~]$

After that, run in vm

Be root

[vagrant@localhost ~]$ sudo su -
Last login:Wed February 5 03:50:50 UTC 2020 on pts/1
[root@localhost ~]#

Create pyenv environment

Prepare sh for setting environment variables

pyenv.sh


if [ -e /opt/pyenv ]; then
    export PYENV_ROOT=/opt/pyenv
    export PATH="${PYENV_ROOT}/bin:$PATH"
    eval "$(pyenv init -)"
fi

Install to / opt / pyenv.

$ yum install -y \
    @development zlib-devel bzip2 bzip2-devel readline-devel sqlite \
    sqlite-devel openssl-devel xz xz-devel libffi-devel findutils

$ curl https://pyenv.run | PYENV_ROOT=/opt/pyenv bash

$ groupadd pyenv \
    && chown -R :pyenv /opt/pyenv/ \
    && chmod g+r -R /opt/pyenv/

$ cp /vagrant/pyenv.sh /etc/profile.d/

Verification

Re-enter the vm

[vagrant@localhost ~]$ pyenv -v
pyenv 1.2.16

[vagrant@localhost ~]$ pyenv versions
* system (set by /opt/pyenv/version)

Insert python by version specification

Since pyenv install can only be done by root,

[root@localhost ~]# pyenv install 3.8.1
(..snip..)

[root@localhost ~]# pyenv install 3.7.6
(..snip..)

It has entered.

[root@localhost ~]# pyenv  versions
* system (set by /opt/pyenv/version)
  3.7.6
  3.8.1

Also confirmed by general users. I don't have write permission, so I get pyenv: cannot rehash: / opt / pyenv / shims isn't writable, but don't worry.

[root@localhost ~]# su - vagrant
Last login: Wed Feb  5 04:01:04 UTC 2020 from 10.0.2.2 on pts/0
pyenv: cannot rehash: /opt/pyenv/shims isn't writable
[vagrant@localhost ~]$ pyenv versions
* system (set by /opt/pyenv/version)
  3.7.6
  3.8.1

This is the preparation.

Operation check

Run in vm.

Add general user

Don't forget to belong to the pyenv group.

[root@localhost ~]# useradd foo -G pyenv

[root@localhost ~]# id foo
uid=1001(foo) gid=1002(foo) groups=1002(foo),1001(pyenv)

To add an existing user to group:

[root@localhost ~]# useradd bar
[root@localhost ~]# gpasswd -a bar pyenv

[root@localhost ~]# id bar
uid=1002(bar) gid=1003(bar) groups=1003(bar),1001(pyenv)

Use pyenv as a general user

Since pyenv global cannot be used, pyenv local is used for each user.

Those who want to use 3.7.6

[root@localhost ~]# su - bar
Last login: Wed Feb  5 03:55:34 UTC 2020 on pts/2
pyenv: cannot rehash: /opt/pyenv/shims isn't writable

[bar@localhost ~]$ pyenv local 3.7.6
[bar@localhost ~]$ python -V
Python 3.7.6

You can also use virtualenv in pyenv

[bar@localhost ~]$ python -m venv .venv
[bar@localhost ~]$ . .venv/bin/activate

(.venv) [bar@localhost ~]$ python -V
Python 3.7.6
(.venv) [bar@localhost ~]$ which python
~/.venv/bin/python
(.venv) [bar@localhost ~]$ pip install numpy==1.18.1

(.venv) [bar@localhost ~]$ python -c 'import numpy; print(numpy.__version__)'
1.18.1

Those who want to use 3.8.1

[root@localhost ~]# su - foo
Last login: Wed Feb  5 03:54:47 UTC 2020 on pts/1
pyenv: cannot rehash: /opt/pyenv/shims isn't writable

[foo@localhost ~]$ pyenv local 3.8.1
[foo@localhost ~]$ python -V
Python 3.8.1

Don't forget to add the --user option to pip install if you don't use virtualenv.

[foo@localhost ~]$ python -m pip install --user numpy==1.15

[foo@localhost ~]$ python -c 'import numpy; print(numpy.__version__)'
1.15.0

end.

It is unknown what will happen if you use it because it is not operating properly. At Your Own Risk.

I mean

Let's use docker.

Recommended Posts

The story of sharing the pyenv environment with multiple users
Use multiple versions of python environment with pyenv
The story of doing deep learning with TPU
Note: Prepare the environment of CmdStanPy with docker
Prepare the execution environment of Python3 with Docker
The story of sys.path.append ()
The story of stopping the production service with the hostname command
The story of building the fastest Linux environment in the world
Get the sum of each of multiple columns with awk
Predict the gender of Twitter users with machine learning
Preparing the execution environment of PyTorch with Docker November 2019
Adjust the ratio of multiple figures with the matplotlib gridspec
The story of building Zabbix 4.4
[Apache] The story of prefork
The story of implementing the popular Facebook Messenger Bot with python
Unify the environment of the Python development team starting with Poetry
The story of displaying images with OpenCV or PIL (only)
The story of rubyist struggling with python :: Dict data with pycall
The story of making a question box bot with discord.py
The story of debugging in the local environment because the compilation did not work with Read the Docs
A story stuck with the installation of the machine learning library JAX
The story of Python and the story of NaN
The story of not being able to run pygame with pycharm
How to handle multiple versions of CUDA in the same environment
Raise the version of pyenv itself
March 14th is Pi Day. The story of calculating pi with python
Django cannot be installed in the development environment of pipenv + pyenv
The story of participating in AtCoder
The story of making a standard driver for db with python.
Environment construction memo of pyenv + conda
The story of outputting the planetarium master in pdf format with Pycairo
The story of the "hole" in the file
The story of visualizing popular Qiita tags with Bar Chart Race
The story of remounting the application server
Periodically log the value of Omron environment sensor with Raspberry Pi
The story of writing a program
The story of making a module that skips mail with python
Prepare the development environment with anyenv
Environment construction with pyenv and pyenv-virtualenv
A story that visualizes the present of Qiita with Qiita API + Elasticsearch + Kibana
The story of a Parking Sensor in 10 minutes with GrovePi + Starter Kit
The story of making a university 100 yen breakfast LINE bot with Python
The story of having a hard time introducing OpenCV with M1 MAC
Manage multiple types of users with CustomUser in django-allauth (multi type user)
The story of making a sound camera with Touch Designer and ReSpeaker
Prepare the environment of Chainer on EC2 spot instance with AWS Lambda
Logging the value of Omron environment sensor with Raspberry Pi (USB type)
The story of trying to push SSH_AUTH_SOCK obsolete on screen with LD_PRELOAD
The story of using mysqlclient because PyMySQL cannot be used with Django 2.2
[Pyenv] Building a python environment with ubuntu 16.04
[Ubuntu 18.04] Python environment construction with pyenv + pipenv
The story of trying to reconnect the client
The story of an error in PyOCR
Use smbus with python3 under pyenv environment
Align the size of the colorbar with matplotlib
The story of verifying the open data of COVID-19
The story of adding MeCab to ubuntu 16.04
The story of making Python an exe
Check the existence of the file with python
About the virtual environment of python version 3.7
The story of making an immutable mold