Skip to content

Instantly share code, notes, and snippets.

@trongnghia203
Last active October 28, 2024 15:22
Show Gist options
  • Save trongnghia203/9cc8157acb1a9faad2de95c3175aa875 to your computer and use it in GitHub Desktop.
Save trongnghia203/9cc8157acb1a9faad2de95c3175aa875 to your computer and use it in GitHub Desktop.
Install pyenv

Pyenv

Install Multiple Python Versions for Specific Project

1. Install pyenv in Linux

1.1. Install all the required packages

# On Debian/Ubuntu/Linux Mint ------------ 
sudo apt install curl git-core gcc make zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev libssl-dev

# On CentOS/RHEL ------------
sudo yum -y install epel-release
sudo yum -y install git gcc zlib-devel bzip2-devel readline-devel sqlite-devel openssl-devel

# On Fedora 22+ ------------
sudo yum -y install git gcc zlib-devel bzip2-devel readline-devel sqlite-devel openssl-devel

1.2. Grab the the latest pyenv source tree from its Github repository

git clone https://github.com/pyenv/pyenv.git $HOME/.pyenv

1.3. Set the environment variable PYENV_ROOT

vim $HOME/.bashrc
## pyenv configs
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

1.4. source $HOME/.bashrc file or restart the shell

source $HOME/.bashrc
# or:
exec "$SHELL"

2. How to install Multiple Python Versions in Linux

# View all available versions with this command.
pyenv install -l

# You can now install multiple Python version via pyenv, for example.
pyenv install 3.6.4
pyenv install 3.6.5

# List all Python versions available to pyenv
pyenv versions

# Check the global Python version
pyenv global

# Set the global python version using the pyenv command
pyenv global 3.6.5
pyenv global

# Set the local Python version on per-project basis
# For instance, if you have a project located in $HOME/python_projects/test,
# you can set the Python version of it using following command.
cd python_projects/test
pyenv local 3.6.5
pyenv version		#view local python version for a specific project, or:
pyenv versions

3. Extra:

Pyenv manages virtual environments via the pyenv-virtualenv plugin which automates management of virtualenvs and conda environments for Python on Linux and other UNIX-like systems.

3.1. Installing this plugin using following commands

git clone https://github.com/yyuu/pyenv-virtualenv.git $HOME/.pyenv/plugins/pyenv-virtualenv
source $HOME/.bashrc

3.1. Create a test virtual environment

called venv_project1 under a project called project1 as follows

cd python_projects
mkdir project1
cd project1
pyenv virtualenv 3.6.5 venv_project1
@ponchofiesta
Copy link

ponchofiesta commented May 24, 2023

On CentOS 7 installing 3.9.16 throws two errors about missing lzma and ffi. I assume libffi-devel.x86_64and xz-devel.x86_64is missing. ctypes will not work without ffi for example.

sudo yum install -y libffi-devel.x86_64 xz-devel.x86_64

Newer urllib3 module requires newer openssl. You might want to replace openssl-devel by openssl11-devel.

@CraigOpie
Copy link

CraigOpie commented Oct 18, 2023

For installing Python 3.12.0 on Fedora 38 ARM, I also had to install tkinter. The following worked for me:

sudo dnf install -y tk-devel python3.12-tkinter libffi-devel xz-devel

And then I was able to install Python 3.12.0 without issue:

$ pyenv install 3.12.0
Downloading Python-3.12.0.tar.xz...
-> https://www.python.org/ftp/python/3.12.0/Python-3.12.0.tar.xz
Installing Python-3.12.0...
Installed Python-3.12.0 to /home/<username>/.pyenv/versions/3.12.0

@gabigab117
Copy link

gabigab117 commented Dec 6, 2023

How to make Pycharm use the python versions of pyenv to create venv ?

I have fedora 39
Thks

@steven7mwesigwa
Copy link

Ubuntu 22.04.3 LTS
Required pyenv dependencies.

apt install build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev curl libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment