DEV Community

Cover image for How to upgrade to Python 3.9.0 on Ubuntu 18.04 LTS without internal bleeding!
Mohammad Moallemi
Mohammad Moallemi

Posted on • Originally published at mmoallemi99.com

How to upgrade to Python 3.9.0 on Ubuntu 18.04 LTS without internal bleeding!

SPOILER ALERT:
Upgrading the Python version on Ubuntu breaks some packages like apt and pip in your system!
[Bleeding SysAdmin crawling]

In this article we’re going to upgrade python and fix the issues.

Start reading this article and executing the instruction:
How to upgrade to Python 3.9.0 on Ubuntu 18.04 LTS
It will guide you through updating the repo and basic updating stuff.

After that, try running sudo apt update.
See!

ModuleNotFoundError: No module named ‘apt_pkg’ModuleNotFoundError: No module named ‘apt_pkg’

or event a simple pip3 install virtualenv:

ImportError: cannot import name ‘sysconfig’ from ‘distutils’ (/usr/lib/python3.9/distutils/__init__.py)ImportError: cannot import name ‘sysconfig’ from ‘distutils’ (/usr/lib/python3.9/distutils/init.py)

Well, that’s a lot of errors!
BUT:
This is GNU/Linux bruh! We always have solutions! [D’Oh!]

At first, you need to resync old python packages to new corresponding paths, we’ll first navigate to python3 dist-packages folder and use ln command to make links.

Linking old python packages to new onesLinking old python packages to new ones

If you consider your old python version 3.6 and the new one 3.9 the ln command would be something like above: (change 36 and 39 numbers accordingly)

Then uninstall any existing python-apt packages using two apt purge commands and reinstall them just like a normal apt package

Reinstalling python-apt packageReinstalling python-apt package

BEWARE: using apt with --reinstall flag doesn't work!
Now run sudo apt update.

YaY! It’s fixed! [Mic Drop!]

But wait a second, I still can’t install pip packages!
Just run this command and you’ll be good to go:
sudo apt install python3.9-distutils python3.9-dev

Happy SysAdminning!

Thanks to the free software community for this beautiful world we’re living in!

Sources:
How to upgrade to Python 3.9.0 on Ubuntu 18.04 LTS
StackOverFlow: python-dev installation error: ImportError: No module named apt_pkg
AskUbuntu: ImportError: cannot import name ‘sysconfig’ from ‘distutils’ (/usr/lib/python3.9/distutils/init.py)

https://stackoverflow.com/questions/13708180/python-dev-installation-error-importerror-no-module-named-apt-pkg

Originally published at https://mmoallemi99.com on February 17, 2021.

Top comments (0)