[PYTHON] Install pyenv and pyenv-virtualenv

Introduction

There is a lot of information about installing pyenv, but ubuntu leaves a solution to the problem that the path is overwritten with secure_path when using sudo and pyenv is not recognized.

The official GitHub repository is here [https://github.com/pyenv/pyenv)

environment OS: Debian 9.0 stretch Python: 3.6.3

update 2019.01.29 Changed apt-get to apt

git install

First you need to include git to install pyenv. ((Y / n) during installation should be Y (meaning = Yes))

sudo apt update
sudo apt install -y git

install pyenv

Clone the repository from github.

git clone https://github.com/pyenv/pyenv.git ~/.pyenv

Then add the path to .profile (.bash_profile). (By the way → ". Bash_profile? .Bashrc? There are various things, where and what?")

(For first-time users) If you use ls -al ~ / once to check if there is a .profile under / home / USERNAME, it may not be wasted.

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.profile
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.profile

This is the process of setting the environment variable * PYENV_ROOT * in the first line and adding * PYENV_ROOT / bin * to the environment variable * PATH * in the second line. Now apply the added settings by reloading .profile (.bash_profile).

source ~/.profile

Reloaded the .profile.

Of course, open .profile (.bash_profile) using some editor such as vim

sudo vim ~/.profile

It is the same even if the following is added.

export PYENV_ROOT=$HOME/.pyenv
export PATH=$PYENV_ROOT/bin:$PATH
if command -v pyenv 1>/dev/null 2>&1; then
  eval "$(pyenv init -)"
fi

afterwards

which pyenv

To run

/home/USERNAME/.pyenv/bin/pyenv

If you get a result like this, it's OK.

(For ubuntu) Add group and edit sudoers file

This is a solution to the problem that PATH is not inherited and an error occurs at `$ sudo pyenv```. There are various solutions, but the following seems to be the best, so I referred to it. The same thing happens in Debian, but pyenv install is possible without sudo, so I think the following should be handled only for ubuntu. [[Ubuntu] How to take over PATH when sudo](http://qiita.com/hidekuro/items/0f8acd0504baa6efde34) In addition, please change ** USERNAME``` part to your own user name. ** **

sudo groupadd developer
sudo gpasswd --add USERNAME developer
sudo visudo
Defaults exempt_group="developer"

After this, be sure to log in to ssh again.

python install

Before that, install the necessary tools.

sudo apt install -y gcc make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

And here, let's check what can be installed now.

pyenv install --list

The items that can be installed are listed.

Here we will install Python 3.6.3. ** (Note that mod_wsgi cannot be installed unless you add an option here) ** In case of ubuntu, please execute with sudo.

pyenv install 3.6.3
#Later mod as a python package_When installing wsgi, install with the following options
#env PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install 3.6.3

Make sure 3.6.3 is installed

pyenv versions

When you run

* system
  3.6.3 (set by /home/USER_NAME/.pyenv/version)

You can see that 3.6.3 has been added in.

I think it's a good idea to change python and update pip to the latest version. By the way, here is the difference between global and local (→ Difference between pyenv local and pyenv global in pyenv). Since it is a tool that divides the environment, I think that basically local is often used. In case of ubuntu, please add sudo.

pyenv global 3.6.3
pip install --upgrade pip

By the way, install pyenv-virtualenv

Following pyenv, also install a plugin called pyenv-virtualenv.

git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv

It is an addition to .profile (.bash_profile).

echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.profile

Apply the added settings by reloading .profile (.bash_profile) in the same way as pyenv.

source ~/.profile

Then try creating a new environment myenv. In ubuntu it is sudo.

pyenv virtualenv 3.6.3 myenv

#In addition, there is such an option
# --system-site-packages  <-Use installed modules
# --no-site-packages      <-A virtual environment is created with all installed modules removed

#For example, already 3.6.3 When inheriting the modules installed in the environment
# pyenv virtualenv --system-site-packages 3.6.3 myenv

You can see that myenv has been created by checking the list of versions.

pyenv versions
  system
* 3.6.3 (set by /home/USERNAME/.pyenv/version)
  3.6.3/envs/myenv
  myenv

Recommended Posts

Install pyenv and pyenv-virtualenv
Install python (pyenv, pyenv-virtualenv)
Install and configure `pyenv` Ansible playbook
Environment construction with pyenv and pyenv-virtualenv
pyenv + pyenv-virtualenv (CentOS7)
Install pyenv and Python 3.6.8 on Ubuntu 18.04 LTS
Install pyenv and rbenv on CentOS system-wide
Install pyenv on mac
Install pip, pyenv, BeautifulSoup4
Install and configure KVM
Install python with pyenv
Install pyenv on OSX
Install the latest stable Python with pyenv (both 2 and 3)
Install pyenv on Raspberry Pi and version control Python
python (pyenv + pyenv-virtualenv) + CentOS7 installation
[CentOS7] Install anaconda using pyenv
Modify and install ReadyMedia sources
Install Python and Flask (Windows 10)
When pyenv install BUILD FAILED
Notes on pyenv and Atom
Install Python 3.7 and Django 3.0 (CentOS)
Coexistence of pyenv and autojump
Install pyenv from Homebrew, install Python from pyenv
python with pyenv and venv
Catalina on Mac and pyenv
How to use pyenv and pyenv-virtualenv in your own way
Install pyenv on MacBook Air and switch python to use
Install Pyethapp and generate an account
Install Python 2.7.9 and Python 3.4.x with pip.
Install Mecab and mecab-python3 on Ubuntu 14.04
How to install and use Tesseract-OCR
Install and run dropbox on Ubuntu 20.04
Install OpenCV and Chainer on Ubuntu
(Windows10) Install Linux environment and gnuplot.
Install CUDA 8.0 and Chainer on Ubuntu 16.04
Build and install OpenCV on Windows
Resolved errors caused by pyenv install
Install pyenv on EC2 (Amazon Linux)
How to install and configure blackbird
How to install CUDA and nvidia-driver
How to install and use Graphviz
Unable to install Python with pyenv
Install fabric on Ubuntu and try
Install Python on CentOS using Pyenv
Compile and install Git from source.
[Solved] ImportError: No module named pyexpat and pyenv install is moss
Install python2.x on catalina with pyenv
Wget fails during install with pyenv
Install Python on CentOS using pyenv
How to install Anaconda with pyenv
Install easy_install and pip on windows
Until you install and run matplotlib
Python virtual environment construction (2017 version) pyenv and pyenv-virtualenv and virtualenv and virtualenv wrapper and pyvenv and venv
Install Python as a Framework with pyenv
[Django] Install radio buttons and shape markup
How to install and use pandas_datareader [Python]
Install wsl2 and master linux on windows
Install and launch k3s on Manjaro Linux
Install and Configure TigerVNC server on Linux
Notes on building Python and pyenv on Mac
Install Puppet Master and Client on Ubuntu 16.04