Safely install Python 3.9 for non-root user?

(Please excuse my English)

My server's OS is Ubuntu 20.04. In Ubuntu 20.04, as we know, Python 3.8 is installed by default, and many system applications run on Python 3.8.
I additionally installed Python 3.9 in my Ubuntu server. And my server worked fine for a while.

However, when I upgraded the global modules of Python(3.8 and 3.9 both), the problem occurred. Some system programs, such as Software Updater and Software and Update, are not working. They are giving errors saying that cannot find some Python global modules. I tried to fix the problem by re-installing the python global modules, but could not fix.

I don't need Python 3.9 for the whole server system, only for few non-root user accounts. And the only needed global module are pip and venv. Other modules can be installed on project base.
So, I tried to install Python 3.9 as a non-root user. But some errors occurred, and failed.

How can I safely install Python 3.9(or above) for non-root users?

  • Here, the safe installation means the installation not interfering with the system default Python 3.8.
  • It doesn't matter whether Python 3.9(or above) is installed system-wide or for a specific user.

Solution 1:

You might need to check on which version python3 is mapped.

ls -l /usr/bin/python3

If the symlink is on python3.9, you can try to revert back onto version python3.8 with the following command

cd /usr/bin/
sudo ln -sf python3.8 python3

In fact, it's not a big problem as you can have several system python version. If your user need to use python3.9, just use python3.9 <your script>.py

Solution 2:

Ubuntu 20.04 and later has the python3.9 package in its default repositories. It can be installed alongside the default python3.8 package with sudo apt update && sudo apt install python3.9 Installing the python3.9 package from the default Ubuntu repositories simplifies package management.

If you are using Ubuntu 20.04 keep Python 3.8 as the default Python 3.x version and switch to Python 3.9 only when necessary using update-alternatives. After you are done using Python 3.9 you can switch the it back to the default Python 3 version.

  • List installed versions of Python: update-alternatives --list python

  • Switch between Python versions: update-alternatives --config python

    From the terminal command-line Press <enter> to keep the current choice[*], or type selection number: