8

I want to install Python 3.10 on Ubuntu 18.04 (I'm currently on Python 3.8) from the deadsnakes repository with the following set of commands I found on the internet:

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.10

But I got the error sudo: add-apt-repository: command not found.

More net research led me to this set of commands at "ModuleNotFoundError: No module named 'apt_pkg'" appears in various commands - Ask Ubuntu:

sudo apt remove python3-apt
sudo apt autoremove
sudo apt autoclean
sudo apt install python3-apt

Other web sources said the same thing, so I did that, but I still get the error message when I run sudo add-apt-repository ppa:deadsnakes/ppa.

Then I found How to Fix 'add-apt-repository command not found' on Ubuntu & Debian - phoenixNAP, which advised this set of commands:

sudo apt update
sudo apt install software-properties-common
sudo apt update

so I did that, but when I run sudo add-apt-repository ppa:deadsnakes/ppa I now get this error message:

~$ sudo add-apt-repository ppa:deadsnakes/ppa
Traceback (most recent call last):
  File "/usr/bin/add-apt-repository", line 12, in <module>
    from softwareproperties.SoftwareProperties import SoftwareProperties, shortcut_handler
  File "/usr/lib/python3/dist-packages/softwareproperties/SoftwareProperties.py", line 28, in <module>
    import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'

I have found some web links that show a wide variety of solutions with earlier versions of Python. I'm currently on Python 3.8.

Before I do anything more I want to ask what is the best way to solve the ModuleNotFoundError: No module named 'apt_pkg' error message when trying to install the deadsnakes repository to install Python 3.10, given the number of possible solutions I have seen.

Thanks very much.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
RTC222
  • 2,025
  • 1
  • 20
  • 53
  • 2
    maybe first you should check if you have `add-apt-repository` and eventually use `/full/path/to/add-apt-repository`. You may have problem with `apt_pkg` because you run `remove python3-apt` and later `install python3-apt` - as for me it was very wrong idea. – furas Nov 12 '21 at 23:28
  • 1
    I use Linux Mint 20 (based on Ubuntu 20.04) and I can't test it with Ubunut 18.04 but I have the same command with two names `add-apt-repository` and `apt-add-repository` - maybe you have `apt-add-repository` instead of `add-apt-repository` – furas Nov 12 '21 at 23:33
  • 1
    I'm not sure but older Ubuntu could use `Python 3.6` to run system functions - and if you even had `Python 3.8` then it still could need to install `apt` modules in `Python 3.6` – furas Nov 12 '21 at 23:36
  • 1
    BTW: Python `3.10` is very fresh version and some modules may not work with this version so better wait few months and still use `3.8` – furas Nov 12 '21 at 23:37
  • Thanks for your comments. I will try your first two ideas. If I can't get it to install I can build from source, but I would like to use deadsnakes if I can. – RTC222 Nov 12 '21 at 23:47
  • I have /usr/bin/add-apt-repository and /usr/share/man/man1/add-apt-repository.1.gz – RTC222 Nov 12 '21 at 23:49
  • I also use `deadsnakes` but I add it using `GUI` - program `synaptic` or `mintupdate` (it is update-manager in Linux Mint which displays icon in status bar when there are updates) – furas Nov 13 '21 at 00:23
  • SO is for programming questions only, so OS support is [off-topic](/help/on-topic). You can ask on [ubuntu.se] instead. I realize the question involves Python, but it's not related to code that you've written, so the question is not about programming per se. – wjandrea Apr 18 '22 at 01:43
  • Did you get the steps mixed up? The first error is about `add-apt-repository`, but the link you mentioned is about `apt_pkg`. – wjandrea Apr 18 '22 at 01:47
  • I bet the root problem is that you changed the symlink `/usr/bin/python3` to point to `python3.8` instead of `python3.6`. You're not supposed to do that on Ubuntu since it's a core system component. [This existing question seems to be a duplicate](https://askubuntu.com/q/1229095/301745). – wjandrea Apr 18 '22 at 02:02

1 Answers1

23

This worked for me:

sudo apt-get install python3-apt --reinstall
cd /usr/lib/python3/dist-packages
sudo cp apt_pkg.cpython-38-x86_64-linux-gnu.so apt_pkg.so

The 38 in the filename above can be different for you.

Community
  • 1
  • 1
Radjin
  • 246
  • 3
  • 4
  • I had same error message in a different situation, but the first-line alone fixed it for me thanks – MattH Jul 24 '22 at 18:46