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.
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
Get it from github.
$ cd /usr/local/
$ sudo git clone git://github.com/yyuu/pyenv.git ./pyenv
$ sudo mkdir -p ./pyenv/versions ./pyenv/shims
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
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"
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
$
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
$
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)
$
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