5

I installed python 3.9 following the steps in this link.

  1. sudo apt update
  2. sudo apt install python3.9
  3. python3.9
  4. sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.[old-version] 1
  5. sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2
  6. sudo update-alternatives --config python3

However, it's throwing an error that python3.9 not found on the 3rd point. Also, I noticed the python3.9 on installation using the 2nd point is showing Note, selecting 'postgresql-plpython3-9.5' for regex 'python3.9'.

Complete message is

Reading package lists... Done
Building dependency tree
Reading state information... Done
Note, selecting 'postgresql-plpython3-9.5' for regex 'python3.9'
The following packages were automatically installed and are no longer required:
  linux-aws-headers-4.4.0-1104 linux-aws-headers-4.4.0-1105 linux-aws-headers-4.4.0-1106 linux-aws-headers-4.4.0-1107 linux-aws-headers-4.4.0-1109 linux-aws-headers-4.4.0-1110 linux-aws-headers-4.4.0-1111
  linux-aws-headers-4.4.0-1112 linux-aws-headers-4.4.0-1113 linux-aws-headers-4.4.0-1114
Use 'sudo apt autoremove' to remove them.
The following NEW packages will be installed:
  postgresql-plpython3-9.5
0 upgraded, 1 newly installed, 0 to remove and 56 not upgraded.
Need to get 0 B/40.6 kB of archives.
After this operation, 166 kB of additional disk space will be used.
Selecting previously unselected package postgresql-plpython3-9.5.
(Reading database ... 362651 files and directories currently installed.)
Preparing to unpack .../postgresql-plpython3-9.5_9.5.25-0ubuntu0.16.04.1_amd64.deb ...
Unpacking postgresql-plpython3-9.5 (9.5.25-0ubuntu0.16.04.1) ...
Processing triggers for postgresql-common (173ubuntu0.3) ...
Building PostgreSQL dictionaries from installed myspell/hunspell packages...
Removing obsolete dictionary files:
Setting up postgresql-plpython3-9.5 (9.5.25-0ubuntu0.16.04.1) ...

Why is it setting up postgresql-plpython3-9.5 and how can I prevent it from doing so?

abhi
  • 337
  • 1
  • 3
  • 12
  • 1
    It's only selecting `postgresql-plpython3-9.5` because there _isn't_ a `python3.9` package available in your sources. – Charles Duffy Feb 23 '22 at 14:12
  • 1
    ...right now, to even start to try to understand why that's the case, we'd need to read your link. Please don't do that -- questions should be self-contained enough to answer even if links break. Among the things we would need to know for this to be answerable: Which specific distro release are you running? What's in your `sources.list`? Did `apt-get update` run without errors the last time you tried to retrieve package databases from all the locations in that `sources.list` file? Include answers to all these **inside the question itself**, not behind a link. – Charles Duffy Feb 23 '22 at 14:12

3 Answers3

11

The Problem:

The deadsnakes ppa is no longer available for Ubuntu Xenial. That's the reason you cannot install python3.9. See this issue. You will have to compile from source or upgrade your server to a supported version of Ubuntu.

Solution: build it yourself

If you are not able to upgrade your system, you could instead use pyenv to install any given python version as described below. For this a new version of openssl needs to be installed for python version >= 3.8

# install dependencies
apt update
apt install -y build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libsqlite3-dev libreadline-dev libffi-dev curl libbz2-dev liblzma-dev git

# download and compile openssl
curl -L https://www.openssl.org/source/openssl-1.1.1s.tar.gz | (cd /usr/src; tar xz)
cd /usr/src/openssl-1.1.1s && ./config --prefix=/usr/local && make -j4 && make install

# download and configure pyenv
curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash

echo >> ~/.bashrc # add new-line.
echo 'export PATH="/root/.pyenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
source ~/.bashrc

# build python 3.9.16 with pyenv
CONFIGURE_OPTS="--with-openssl=/usr/local --with-openssl-rpath=auto" pyenv install 3.9.16

# build python 3.10.9 with pyenv
CONFIGURE_OPTS="--with-openssl=/usr/local --with-openssl-rpath=auto" pyenv install 3.10.9
itsafire
  • 5,607
  • 3
  • 37
  • 48
  • added instructions how to build a current python (>=3.8) on Ubuntu Xenial – itsafire Jul 05 '22 at 15:36
  • Thank you. Works well with Ubuntu 14.04. The only caveat is that it can only run with current specific user (`root`). – fa wildchild Oct 31 '22 at 02:56
  • It worked most of the way but failed at the `pyenv install` step with `ModuleNotFoundError: No module named '_ssl'`. I had to pass the location of OpenSSL headers and libraries explicitly to complete: `LDFLAGS="-Wl,-rpath,/usr/local/lib" \ CONFIGURE_OPTS="-with-openssl=/usr/local" \ pyenv install -v 3.9.16` – Heelara Jan 31 '23 at 01:10
1

You will need to add the deadsnakes repo.

sudo add-apt-repository ppa:deadsnakes/ppa

After that you can follow the steps in your question.

If you still have the same issues after adding the deadsnakes repo, it probably means your running an unsupported version of linux. You might then have to install Python 3.9 from source, you can check this answer on how to do that.

nobody
  • 1,144
  • 1
  • 10
  • 20
0

You may have it already installed

try running $ python3 --version to see what python version your running. If its not installed try running $ sudo apt-get update then run $ sudo apt-get install python3.9 to install python3.9

Hopefully this will help

Mr. Day
  • 73
  • 2
  • 7