How to upgrade to Python 3.9.0 on Ubuntu 18.04 LTS without internal bleeding!
Tags: linux, python, ubuntu, devops
Published: 2021-02-25 15:45:04

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](https://www.itsupportwale.com/blog/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’](https://cdn-images-1.medium.com/max/2048/0*ROMItE-03xvCr9M0.png)*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)](https://cdn-images-1.medium.com/max/2048/0*LNQ8sgvO3VFpzr8M.png)*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 ones](https://cdn-images-1.medium.com/max/2048/0*F9tZYrdXTnSjGzDj.png)*Linking 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 package](https://cdn-images-1.medium.com/max/2048/0*iVshnNbMvXvKEw3F.png)*Reinstalling 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
](https://www.itsupportwale.com/blog/how-to-upgrade-to-python-3-9-0-on-ubuntu-18-04-lts/)StackOverFlow: [python-dev installation error: ImportError: No module named apt_pkg](https://stackoverflow.com/questions/13708180/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://askubuntu.com/questions/1292972/importerror-cannot-import-name-sysconfig-from-distutils-usr-lib-python3-9)

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

*Originally published at [https://mmoallemi99.com](https://mmoallemi99.com/blog/how-to-upgrade-to-python-3-9-0-on-ubuntu-18-04-lts-without-internal-bleeding/) on February 17, 2021.*