Updated according to latest Ubuntu-LTS 22.04 and for python 3, however similar instructions can be followed for python 2 with appropriate version changes.
You can consider pyenv python environment manager which lets you configure different versions of python parallely without needing to create a separate virtualenv or requiring separate pip installed.
The installation instructions can be found here and it is pretty easy to configure pyenv. https://github.com/pyenv/pyenv#readme
This does however require you to install prerequisite libraries which can be installed as follows:
Redhat based systems
sudo yum install git gcc zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel tk-devel libffi-devel make curl llvm
Ubuntu/Debian based linux systems
sudo apt-get 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 python3-openssl git
After installing prerequisites follow the instructions in the README.md
Once installation is finished, to check python versions present in your system run the following in your terminal
pyenv versions
Note if you have installed python versions before setting up pyenv it may not detect the installed versions and you might have to remove them including dependencies. Take care to not remove important lib files while doing so.
To install a new version of python
pyenv install <version_number> # like 3.9 for installing python3.9
Once installed to set the python version as default run the following.
pyenv global <version_number> # to set it globally
pyenv local <version_number> # to set it in the local directory
You can switch between installed Python versions at any time using the pyenv global
, pyenv local
, or pyenv shell
commands with the desired version number.
Note that this arrangement is different from a virtual environment as it allows you to manage multiple versions of python at a system-wide level while using other system level libraries and packages. However pyenv does have plugins to enable virtualenv support which can be installed from instructions listed here https://github.com/pyenv/pyenv-virtualenv#readme
Pyenv can be removed by simply deleting the pyenv folder and reverting any changes made to .bashrc
, .bash_profile
and/or similar files.
Hope this helps.
pip34
andpip35
? – Prevaricatepip3.x
actually manages the python version that you want to install packages to (perhaps runpip3.x -V
to see). Or use @Hugo's solution to have better control over lots of python versions. – Meneau