This tutorial will show how to install pyenv and virtualenv on Ubuntu and other Debian distributions, so you can quickly switch between Python versions. For windows, you can use pyenv-win.
1. Pyenv related
Install dependencies
$ sudo apt install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev git vim
Install pyenv
$ curl -L <https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer> | bash
or
$ curl -L <https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer> | bash
Addition, install pyenv on Mac
$ brew install pyenv
Validate installation
Use the following command to check the version of pyenv.
$ $HOME/.pyenv/bin/pyenv -v
Which in my case printed:
pyenv 2.4.16
If it doesn’t, then add the following lines at the end of your:
- .bashrc if you use bash
- .zshrc if you use zsh
export PYENV_ROOT="$HOME/.pyenv"
[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
The reload your shell and it should work.
$ source ~/.bashrc
Installing Python versions
Run the command below to list the available Python versions to be installed:
$ pyenv install --list
Which should yield something like:
Available versions:
2.1.3
2.2.3
2.3.7
...
2.7.17
2.7.18
3.0.1
3.1.0
3.1.1
...
3.12.6
3.12.7
3.13.0
3.13.0t
3.13-dev
3.13t-dev
3.14-dev
Note, If needed, we can use the Python mirrors for installation. For example, you can use the following environment variables in your .bashrc before you install Python.
export PYTHON_BUILD_MIRROR_URL="<https://registry.npmmirror.com/-/binary/python>"
export PYTHON_BUILD_MIRROR_URL_SKIP_CHECKSUM=1
Note, if this mirror doesn’t work, you can find more available mirrors from Internet. Such as: Python Mirrors or https://jedore.vercel.app/tools/python-mirrors .
Option 1: Online Installation of Python by pyenv
Next, choose a Python version and install it using the following command syntax:
pyenv install [options] <version>
-v
: Displays more detailed information during the installation process.
-f/--force
: Force installation of the version, even if it has already been installed.
-s/--skip-existing
: skip the installed version.
For example:
$ pyenv install 3.13.0
Downloading Python-3.13.0.tar.xz...
-> <https://registry.npmmirror.com/-/binary/python/3.13.0/Python-3.13.0.tar.xz>
Installing Python-3.13.0...
Installed Python-3.13.0 to /home/wrs/.pyenv/versions/3.13.0
$
$ pyenv install 2.7.18
Downloading Python-2.7.18.tar.xz...
-> <https://registry.npmmirror.com/-/binary/python/2.7.18/Python-2.7.18.tar.xz>
Installing Python-2.7.18...
patching file configure
patching file configure.ac
patching file setup.py
patching file Mac/Tools/pythonw.c
patching file setup.py
patching file Doc/library/ctypes.rst
patching file Lib/test/test_str.py
patching file Lib/test/test_unicode.py
patching file Modules/_ctypes/_ctypes.c
patching file Modules/_ctypes/callproc.c
patching file Modules/_ctypes/ctypes.h
patching file Modules/_ctypes/callproc.c
patching file setup.py
patching file Mac/Modules/qt/setup.py
patching file setup.py
Installed Python-2.7.18 to /home/wrs/.pyenv/versions/2.7.18
Option 2: Offline Installation of Python by pyenv
Sometimes we cannot install Python online due to some reasons. For example, the Internet access is disable in your corporate network. We can select the offline installation.
Check the cache folder in .pyenv. If it doesn't exist, create it with the following command:
$ cd ~/.pyenv
$ mkdir cache
Download the offline installation package and put the file in the cache folder. You can download the installation package from various sources. Here's an example:
$ cd ~/.pyenv/cache
$ wget <https://mirrors.aliyun.com/python-release/source/Python-3.14.0a1.tar.xz>
--2024-10-28 15:21:19-- <https://mirrors.aliyun.com/python-release/source/Python-3.14.0a1.tar.xz>
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 61.170.77.224, 61.170.77.225, 61.170.77.228, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|61.170.77.224|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 22613224 (22M) [application/octet-stream]
Saving to: ‘Python-3.14.0a1.tar.xz’
Python-3.14.0a1.tar.xz 100%[=============================================================================>] 21.57M 11.9MB/s in 1.8s
2024-10-28 15:21:21 (11.9 MB/s) - ‘Python-3.14.0a1.tar.xz’ saved [22613224/22613224]
Install Python with following command.
$ pyenv install 3.14.0a1
Installing Python-3.14.0a1...
Installed Python-3.14.0a1 to /home/wrs/.pyenv/versions/3.14.0a1
Note: Note that the downloading step is skipped in this case.
After installed Python, need to run “pyenv rehash” to refresh the information for pyenv.
$ pyenv rehash
You can download the python installation files and put
Uninstall Python versions
To uninstall a version of Python, the command syntax is pyenv [-f|--force] <version>
.
For example:
$ pyenv unstall 3.13.0
Check installed Python
After installing Python, use the following command to check version of Python:
$ pyenv versions
* system (set by /home/wrs/.pyenv/version)
2.7.18
3.13.0
Settings for local or global
Finally, set the installed version as the default local or global:
$ pyenv local 3.13.0
$ pyenv global 3.13.0
$
$ pyenv versions
system
2.7.18
3.12.7
* 3.13.0 (set by /home/wrs/.python-version)
Note: The "local" setting applies only to the current directory.
Unset the local or global.
$ pyenv global --unset
$ pyenv local --unset
List the current local or global used Python.
$ pyenv local
3.13.0
$ pyenv global
3.13.0
Settings for shell
Set and Unset the using Python under current shell.
$ pyenv shell 3.13.0
$ pyenv shell --unset
2. Pyenv-virtualenv related
Create Pyenv virtual environments
To create and activate virtual environments, use the following commands:
$ pyenv virtualenv 3.13.0 myenv
$ pyenv activate myenv
Replace myenv
with your preferred virtual environment name.
List the current virtual environment:
$ pyenv versions
* system (set by /home/wrs/.pyenv/version)
2.7.18
3.13.0
3.13.0/envs/myenv
3.13.0/envs/v3130
myenv --> /home/wrs/.pyenv/versions/3.13.0/envs/myenv
With pyenv, you can easily switch between Python versions and manage virtual environment for different projects. This can be especially helpful when working on projects with specific Python versions requirements.
List all Pyenv virtual environments
Use the command to list current all virtual environment:
$ pyenv virtualenvs
Activate and Deactivate virtual environments
use the following command:
$ pyenv activate myenv
(myenv) $ pyenv deactivate
Delete pyenv-virtual environments
To delete the virtual environment, use the following commands:
pyenv uninstall <virtualenv>
or pyenv virtualenv-delete <virtualenv>
For example:
$ pyenv uninstall myenv
pyenv: remove /home/wrs/.pyenv/versions/myenv? (y/N) y
pyenv-virtualenv: remove /home/wrs/.pyenv/versions/3.13.0/envs/myenv? (y/N) y
$
About more information of Pyenv-virtualenv, please refer to pyenv-virtualenv.