Python 3.x environment construction by Pyenv (CentOS, Ubuntu)

Introduction

Since I decided to make an application with python, I thought that python 2.x series, which is included in the OS by default, would be fine, but Which of Python 2 or Python 3 should be used for development? says that Python3.x is already stable, so you can use Python3.x system-wide with pyenv. I tried to build an environment.

At first, I thought it would be okay to manage only pyenv, but installing each python takes a lot of time and it seems that it is not possible to easily delete and rebuild the environment, so I also introduced virtualenv.

environment

Step1: Install the required packages

Installation of packages required when compiling python.

CentOS


$ sudo yum install gcc bzip2 bzip2-devel openssl openssl-devel readline readline-devel

Ubuntu


$ sudo apt-get install git gcc make openssl libssl-dev libbz2-dev libreadline-dev libsqlite3-dev

Step2: Install Pyenv

Get it from github.

$ cd /usr/local/
$ sudo git clone git://github.com/yyuu/pyenv.git ./pyenv
$ sudo mkdir -p ./pyenv/versions ./pyenv/shims

Step3: Install Pyenv-virtualenv

I want to prepare a separate environment for each version with virtualenv, so install the pyenv-virtualenv plugin.

$ cd /usr/local/pyenv/plugins/
$ sudo git clone git://github.com/yyuu/pyenv-virtualenv.git

Step4: Path setting

Create /etc/profile.d/pyenv.sh.

$ echo 'export PYENV_ROOT="/usr/local/pyenv"' | sudo tee -a /etc/profile.d/pyenv.sh
$ echo 'export PATH="${PYENV_ROOT}/shims:${PYENV_ROOT}/bin:${PATH}"' | sudo tee -a /etc/profile.d/pyenv.sh
$ source /etc/profile.d/pyenv.sh #Reflect settings
$ pyenv --version
pyenv 20141211-6-g995da2d
$

Since I want to inherit PATH and PYENV_ROOT at the time of sudo, edit as follows with visudo in the same way as How to inherit PATH at sudo.

#Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin
Defaults    env_keep += "PATH"
Defaults    env_keep += "PYENV_ROOT"

Step5: How to use Pyenv

As far as I can see the help, it seems that the commands that can be used with rbenv are the same.

$ pyenv --help
pyenv 20141211-6-g995da2d
Usage: pyenv <command> [<args>]

Some useful pyenv commands are:
   commands    List all available pyenv commands
   local       Set or show the local application-specific Python version
   global      Set or show the global Python version
   shell       Set or show the shell-specific Python version
   install     Install a Python version using python-build
   uninstall   Uninstall a specific Python version
   rehash      Rehash pyenv shims (run this after installing executables)
   version     Show the current Python version and its origin
   versions    List all Python versions available to pyenv
   which       Display the full path to an executable
   whence      List all Python versions that contain the given executable

See `pyenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/yyuu/pyenv#readme
$

Step6: Python installation

Check the version that can be installed.

$ pyenv install --list

Installed the latest 3.4.2 as of December 28, 2014.

$ sudo pyenv install -v 3.4.2
$ pyenv versions
* system (set by /usr/local/pyenv/version)
  3.4.2
$

Changed the default python version for each user to 3.4.2.

$ sudo pyenv global 3.4.2
$ pyenv versions
  system
* 3.4.2 (set by /usr/local/pyenv/version)
$ python --version
Python 3.4.2
$

Step7: Create virtualenv

You can create an environment using the existing python version with the following command. Packages installed with pip etc. differ depending on the environment, so you can easily install packages.

$ sudo pyenv virtualenv 3.4.2 dev01-3.4.2
$ pyenv versions
  system
* 3.4.2 (set by /usr/local/pyenv/version)
  dev01-3.4.2
$ sudo pyenv local dev01-3.4.2 #Current environment dev01-3.4.Change to 2
$ pyenv versions
  system
  3.4.2
* dev01-3.4.2 (set by /usr/local/pyenv/plugins/.python-version)
$

Try installing gunicorn with pip.

$ sudo pip install gunicorn
$ pip list
gunicorn (19.1.1)
pip (1.5.6)
setuptools (2.1)
$ sudo pyenv local 3.4.2 
$ pip list # 3.4.2 gunicorn is not installed in the environment.
pip (1.5.6)
setuptools (2.1)
$

You can delete dev01-3.4.2 with the following command.

$ sudo pyenv uninstall dev01-3.4.2
pyenv: remove /usr/local/pyenv/versions/dev01-3.4.2? y
$ pyenv versions
  system
* 3.4.2 (set by /usr/local/pyenv/plugins/.python-version)
$

in conclusion

If you use pyenv, you can easily install python3.x series without worrying about the python environment at the beginning of the system, and it seems that you can easily build operation verification of python2.x series and python3.x series on one OS.

There is also a wrapper for rbenv and pyenv called anyenv, which seems to be more convenient if you use env tools in multiple languages. Prepare the development environment with anyenv

That's it.

Recommended Posts

Python 3.x environment construction by Pyenv (CentOS, Ubuntu)
[Ubuntu 18.04] Python environment construction with pyenv + pipenv
Python environment construction (pyenv, anaconda, tensorflow)
Python environment construction (pyenv + poetry + pipx)
Python3 environment construction with pyenv-virtualenv (CentOS 7.3)
OpenCV3 & Python3 environment construction on Ubuntu
Python environment construction
Environment construction (python)
python environment construction
Python --Environment construction
python environment construction
[Pyenv] Building a python environment with ubuntu 16.04
Environment construction procedure: Ubuntu + Apache2 + Python + Pyramid
Python3 TensorFlow environment construction (Mac and pyenv virtualenv)
Build python environment with pyenv on EC2 (ubuntu)
python windows environment construction
homebrew python environment construction
pyenv + fish environment construction
Mac environment construction Python
Python environment construction @ Win7
[Python] Django environment construction (pyenv + pyenv-virtualenv + Anaconda) for macOS
Report environment construction by python (matplotlib, pandas, sphinx) + wkhtmltopdf
[Ubuntu 18.04] Tensorflow 2.0.0-GPU environment construction
Python + Anaconda + Pycharm environment construction
About Linux environment construction (CentOS)
python (pyenv + pyenv-virtualenv) + CentOS7 installation
Anaconda environment construction on CentOS7
Python environment construction (Windows10 + Emacs)
CI environment construction ~ Python edition ~
Put Python 3.x on Ubuntu
Build python3 environment with ubuntu 16.04
Python environment construction For Mac
Anaconda3 python environment construction procedure
Python3 environment construction (for beginners)
Python environment construction and TensorFlow
Python environment construction under Windows7 environment
[MEMO] [Development environment construction] Python
Ubuntu14.04 + GPU + TensorFlow environment construction
Environment construction of python2 & 3 (OSX)
How to create a Python 3.6.0 environment by putting pyenv on Amazon Linux and Ubuntu
Python environment construction on Mac (pyenv, virtualenv, anaconda, ipython notebook)
Build a python environment with pyenv (OS X El Capitan 10.11.3)
Until building a Python development environment using pyenv on Ubuntu 20.04
Environment construction of python and opencv
Python environment construction memo on Windows 10
Get started with Python! ~ ① Environment construction ~
Prepare Python development environment on Ubuntu
Anaconda python environment construction on Windows 10
Introducing Python using pyenv on Ubuntu 20.04
Python environment construction memo on Mac
[Python3] Development environment construction << Windows edition >>
Set up Python environment on CentOS
Create a python environment on centos
Environment construction of python3.8 on mac
From Ubuntu 20.04 introduction to environment construction
Python3 TensorFlow for Mac environment construction
Install Python on CentOS using Pyenv
Using Chainer with CentOS7 [Environment construction]
Emacs Python development environment construction memo
Ubuntu Desktop 20.04 development environment construction memo
pytorch @ python3.8 environment construction with pipenv