Install pyenv and manage multiple python versions

Introduction

In this post, How to Install pyenv and manage multiple python versions.

  • Pyenv is a fantastic tool for installing and managing multiple Python versions. It also offers the ability to quickly switch from one version of Python to another.
  • Pyenv integrates with the Virtualenv plugin to support creating virtual environments (virtual environments), and library projects will be installed in isolation in this virtual environment without affecting the system.

Install Dependencies

On Rocky Linux / Centos

sudo yum install zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel xz xz-devel libffi-devel

On Ubuntu

sudo apt-get install make build-essential libssl-dev zlib1g-dev libbz2-dev \
  libreadline-dev libsqlite3-dev wget curl libncurses5-dev libncursesw5-dev \
  xz-utils tk-dev libffi-dev liblzma-dev

Install pyenv

You can view the installation instructions from Pyenv’s homepage on Github or use the command below

$ curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash

The output terminal install pyenv as below

vagrant@devopsroles:~$ curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   148  100   148    0     0    778      0 --:--:-- --:--:-- --:--:--   778
100  1687  100  1687    0     0   5477      0 --:--:-- --:--:-- --:--:-- 17214
Cloning into '/home/vagrant/.pyenv'...
remote: Enumerating objects: 882, done.
remote: Counting objects: 100% (882/882), done.
remote: Compressing objects: 100% (438/438), done.
remote: Total 882 (delta 493), reused 565 (delta 338), pack-reused 0
Receiving objects: 100% (882/882), 460.45 KiB | 1.92 MiB/s, done.
Resolving deltas: 100% (493/493), done.
Cloning into '/home/vagrant/.pyenv/plugins/pyenv-doctor'...
remote: Enumerating objects: 11, done.
remote: Counting objects: 100% (11/11), done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 11 (delta 1), reused 3 (delta 0), pack-reused 0
Receiving objects: 100% (11/11), 38.71 KiB | 1.49 MiB/s, done.
Resolving deltas: 100% (1/1), done.
Cloning into '/home/vagrant/.pyenv/plugins/pyenv-installer'...
remote: Enumerating objects: 16, done.
remote: Counting objects: 100% (16/16), done.
remote: Compressing objects: 100% (13/13), done.
remote: Total 16 (delta 1), reused 10 (delta 0), pack-reused 0
Receiving objects: 100% (16/16), 5.78 KiB | 5.78 MiB/s, done.
Resolving deltas: 100% (1/1), done.
Cloning into '/home/vagrant/.pyenv/plugins/pyenv-update'...
remote: Enumerating objects: 10, done.
remote: Counting objects: 100% (10/10), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 10 (delta 1), reused 5 (delta 0), pack-reused 0
Receiving objects: 100% (10/10), done.
Resolving deltas: 100% (1/1), done.
Cloning into '/home/vagrant/.pyenv/plugins/pyenv-virtualenv'...
remote: Enumerating objects: 61, done.
remote: Counting objects: 100% (61/61), done.
remote: Compressing objects: 100% (55/55), done.
remote: Total 61 (delta 11), reused 22 (delta 0), pack-reused 0
Receiving objects: 100% (61/61), 37.94 KiB | 2.11 MiB/s, done.
Resolving deltas: 100% (11/11), done.
Cloning into '/home/vagrant/.pyenv/plugins/pyenv-which-ext'...
remote: Enumerating objects: 10, done.
remote: Counting objects: 100% (10/10), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 10 (delta 1), reused 6 (delta 0), pack-reused 0
Receiving objects: 100% (10/10), done.
Resolving deltas: 100% (1/1), done.

WARNING: seems you still have not added 'pyenv' to the load path.


# See the README for instructions on how to set up
# your shell environment for Pyenv.

# Load pyenv-virtualenv automatically by adding
# the following to ~/.bashrc:

eval "$(pyenv virtualenv-init -)"

Then insert the following 3 lines into the shell’s config file ~/.bashrc

export PATH="/home/vagrant/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

The output terminal config file ~/.bashrc as below

[vagrant@devopsroles ~]$ cat .bashrc
# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# User specific environment
if ! [[ "$PATH" =~ "$HOME/.local/bin:$HOME/bin:" ]]
then
    PATH="$HOME/.local/bin:$HOME/bin:$PATH"
fi
export PATH

# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=

# User specific aliases and functions
export PATH="/home/vagrant/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
[vagrant@devopsroles ~]$ source .bashrc
[vagrant@devopsroles ~]$ pyenv --version
pyenv 2.2.4
[vagrant@devopsroles ~]$

use pyenv

For example, I will introduce how to use pyenv to set up a virtual environment using python 3.6.6 for the project demo. Path ~/projects/demo

$ pyenv install 3.6.6

The output terminal as below

[vagrant@devopsroles ~]$ pyenv install 3.6.6

Installing Python-3.6.6...
Installed Python-3.6.6 to /home/vagrant/.pyenv/versions/3.6.6
[vagrant@devopsroles ~]$

If the installation fails, maybe your system lacks the necessary libraries for compiling, install the missing libraries here

Create a virtual environment with virtualenv, which uses Python 3.6.6.

pyenv virtualenv 3.6.6 app

Enable environment settings

pyenv activate app

To deactivate environment settings

pyenv deactivate app

Useful Commands

Install pyenv and manage multiple python versions

Conclusion

You have to Install pyenv and manage multiple python versions. I hope will this your helpful. Thank you for reading the DevopsRoles page! Install pyenv, Install pyenv.

About HuuPV

My name is Huu. I love technology, especially Devops Skill such as Docker, vagrant, git, and so forth. I like open-sources, so I created DevopsRoles.com to share the knowledge I have acquired. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.