Score:1

Safely install Python 3.9 for non-root user?

au flag

(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.
pdh0710 avatar
au flag
@user535733 : It seems that something has been miscommunicated. I edited my post to express my intentions more clearly.
user535733 avatar
cn flag
One simple way is to use a Python venv. Are you asking how to do that? Or are you asking for more options?
pdh0710 avatar
au flag
@user535733 : I already wrote in my post that "And the only needed global module are `pip` and `venv`", which means I want to use Python 3.9's `pip` and `venv` without interfering the system default Python 3.8.
user535733 avatar
cn flag
In that case, this seems like a duplicate of https://stackoverflow.com/questions/1534210/use-different-python-version-with-virtualenv, which includes among its many answers step-by-step instructions and a link to a YouTube tutorial.
pdh0710 avatar
au flag
@user535733 : I don't know why you are so obsessed with using `venv` only. I'm already using different Python version with `venv`. My problem is that one day it interfered with the system Python 3.8. I don't think I'm the only one experiencing this problem. So I posted this question, and want to hear opinions on this problem. Not using `venv`.
user535733 avatar
cn flag
Please remember that we are not there. We cannot see what you are seeing. We do not know the history of whatever problem you might have. All we know is what you describe to us. This still seems like an [XY Problem](https://en.wikipedia.org/wiki/XY_problem) to me. You're not asking about the real problem you have; you are asking for your favored solution that seems perhaps unwise or infeasible to us. That's why you are not getting answers that you hope for.
pdh0710 avatar
au flag
@user535733 You still don't understand my problem?
pdh0710 avatar
au flag
@user535733 Then ask what information are needed to understand my problem more clearly.
Score:1
br flag

You can safely install Python as a Local / Non-root user by executing the below code with the required Python Version defined in the variable "PYTHON_VER"

# Install Python3 and Libraries as a local user. 
python_config() {
    export PYTHON_VER="3.10.5" 
    export PYTHON_VER_SHORT="$(echo ${PYTHON_VER} | cut -d '.' -f1,2)" 
    cd ~ 
    rm -rf ~/python && mkdir -p ~/python 
    echo "" >> ~/.bashrc 
    echo "export PATH=~/python/bin:$PATH" >> ~/.bashrc 
    source ~/.bashrc 
    wget --quiet --no-check-certificate "https://www.python.org/ftp/python/${PYTHON_VER}/Python-${PYTHON_VER}.tgz" 
    tar -zxvf ~/Python-${PYTHON_VER}.tgz 1>/dev/null 
    cd ~/Python-${PYTHON_VER}/ 
    echo "Python ${PYTHON_VER} - Installing in current logged-in user - $(whoami)" 
    echo "Python ${PYTHON_VER} - Installation in-progress. Please wait..." 
    ./configure --enable-optimizations --prefix=$HOME/python > /dev/null 2>&1; 
    echo "Python ${PYTHON_VER} - ETA: upto 5mins. Please wait..." 
    make altinstall > /dev/null 2>&1; 
    ln -s ~/python/bin/python${PYTHON_VER_SHORT} ~/python/bin/python3 
    ln -s ~/python/bin/pip${PYTHON_VER_SHORT} ~/python/bin/pip3 
    wget --quiet --no-check-certificate https://bootstrap.pypa.io/get-pip.py -O - | python3 - --prefix=$HOME/python 
    source ~/.bashrc 
    ~/python/bin/pip3 install --upgrade pip 
    ~/python/bin/pip3 install --upgrade pygithub 
    ~/python/bin/pip3 install --upgrade --no-cache-dir -r /tmp/requirements.txt --use-pep517 
    cd ~ && rm -rf ~/Python-${PYTHON_VER}* 
    ~/python/bin/python3 --version 
    ~/python/bin/pip3 --version 
    echo "Python ${PYTHON_VER} - Setup Completed!" 
}

# Function Call
python_config
Score:1
de flag
ob2

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

de flag
ob2
Sorry, after reading your question, I just realize I didn't answered to your question. I'm not sure about installing python as non system user, I'm not sure it's feasible.
pdh0710 avatar
au flag
In my Ubuntu server, `/usr/bin/python3 -> python3.8` . I don't why updating global modules makes such a problem.
de flag
ob2
What do you mean by upgrading global modules. You used pip ?
pdh0710 avatar
au flag
Yes. `pip3 install <package_name> --upgrade` and `pip3.9 install <package_name> --upgrade`
de flag
ob2
Consider using virtualenv in that case. Installing packages with pip is generally a bad idea unless you install a server for a specific product.
pdh0710 avatar
au flag
I'm using `venv`. `virtualenv` is better?
de flag
ob2
it's same ! basically, venv will store any package you install, so this will not alter your system.
pgoetz avatar
sa flag
@ob2 Not sure why you're saying using pip is a bad idea. You can have multiple versions of python installed on a system without problem as long as you use distinct command names; e.g. python3.9. The command described by the OP `pip3.9 install <package_name>` will install the package specifically for version 3.9 in a completely separate directory tree as long as 3.9 has been installed properly.
Score:1
sa flag

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:

pdh0710 avatar
au flag
O.K. I'll try this way. When I installed Python 3.9 version on my server, it was the first time I installed a different version of Python, other than the system default. So there may have been some mistakes I wasn't aware of. I'll re-install Python 3.9 without mistakes as you told.
karel avatar
sa flag
Please uninstall the old version of Python 3.9 before installing the new version of Python 3.9.
pdh0710 avatar
au flag
Of course. I'll re-install the whole Ubuntu server. Fortunately, my Ubuntu server has a simple structure.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.