If you are exploring Python-based projects and packages, you may end up in a situation where you need a different Python version. I faced this situation while trying to install PyCoral library that works only with Python versions 3.6 to 3.9 and my system had Python 3.11.
The good thing is that you do not need to uninstall the existing Python version provided by your distribution and install the new one.
You can have multiple Python versions installed thanks to a tool called pyenv. In this tutorial, I'll show you how to install and use this tool to get more than one Python version simultaneously on your Ubuntu and Debian-based distributions.
Install pyenv on Ubuntu
Since pyenv builds Python from the source, youβll need to install build dependencies to use pyenv.
On Ubuntu and Debian based distros, you can use this command to get all the necessary packages.
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 python3-openssl
Some of the above-mentioned packages might already have been installed.
Once you have the dependencies, you can install pyenv using the official script:
curl https://pyenv.run | bash
It will download the script and run it.
When the script ends, it should give you some instructions to follow for loading pyenv automatically. This output is dependent on the shell you use. For me on bash, it showed:
# Load pyenv automatically by appending
# the following to
# ~/.bash_profile if it exists, otherwise ~/.profile (for login shells)
# and ~/.bashrc (for interactive shells) :
export PYENV_ROOT="$HOME/.pyenv"
[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
So, I edited the ~/.bashrc
file and put the mentioned code at the end of it.
Once done, either reload the shell with the command below or just restart your terminal:
exec "$SHELL"
Let's verify that pyenv is installed on your system by checking the version of pyenv:
pyenv --version
If you see a number and not an error, you have pyenv setup on your Ubuntu Linux. It's time to see how to use it.
Install different Python versions with pyenv
You can list various Python installations and virtual environments offered by pyenv with this command:
pyenv --list
However, it will show a huge output with all the offerings that might not be of interest to you.
If you are interested in various Python3 versions, use the magic of regex and grep. Let's say you are interested in Python versions from 3.6 to 3.9. For that, use:
pyenv install --list | grep " 3\.[6789]"
It will still be a big list but not as huge as the one you saw before.
I am doing it to try the Coral projects using the PyCoral libraries and it only works with Python 3.6 to Python 3.9. If I look at the status of Python versions, 3.9 seems like a good version to go with. I'll go with the 3.9.19:
pyenv install -v 3.9.19
Since Python is being built from source, it may take some time and the output will be huge. Just ensure that it doesn't result in error.
Python version location
Different versions of Python you install with pyenv are located in ~/.pyenv/versions/
directory.
abhishek@raspberrypi:~ $ ls ~/.pyenv/versions/
3.9.19
Listing installed Python versions
You can see which additional Python versions with:
pyenv versions
This should show the system version (Python version installed by your distro) and the additional versions installed by pyenv:
* system (set by /home/abhishek/.pyenv/version)
3.9.19
Using additional Python versions
If you just want to run something with a specific Python version, you can use it like this:
pyenv exec python3.9.19 test.py
Alternatively, you can change the default Python version to your choice:
pyenv global 3.9.19
You can confirm the active Python version has changed by looking at the entry with * in the versions list:
abhishek@itsfoss:~ $ pyenv global 3.9.19
abhishek@itsfoss:~ $ pyenv versions
system
* 3.9.19 (set by /home/abhishek/.pyenv/version)
abhishek@raspberrypi:~ $
Later on, you can switch back to the system version:
pyenv global system
Removing addition Python versions
That's quite simple as well. You can either remove the its folder:
rm -rf ~/.pyenv/versions/<python_version_to_delete>
Or, use the pyenv command in this fashion:
pyenv uninstall <python_version>
Prefer using virtual environments
Even if you install multiple Python versions, it would be a good idea to In this tutorial, I'll show you how to install and use this tool to get more than one Python version simultaneously on your Ubuntu and Debian-based distributions.. This way, you won't be messing up with packages at system level.
Let's say I want to use Python 3.9.19 for a project named coral. I'll create virtual environment for this project (name doesn't need to match):
pyenv virtualenv 3.9.19 coral
And now, I switch to this virtual environment:
pyenv local coral
You can verify that a virtual environment is in use:
abhishek@itsfoss:~ $ pyenv which python
/home/abhishek/.pyenv/versions/coral/bin/python
Remember that you can still not use 'apt install python-package-name', hoping that it gets installed with the different Python version in the virtual environment. You'll have to use pip for this purpose:
pip install python-package-name
This will install the mentioned Python package in the specific virtual environment with your specified Python version.
π¬ I hope you like this tutorial on managing different Python versions on Ubuntu. Although I am not a Python expert, but I'll try to help you if you have any questions.