How to update python on ubuntu

Upgrading to newer versions is always “fun”. Along with the new functionality, you will need to remove obsolete functions, update modules that have lost compatibility, and in general, do a lot of work.

Preparing data from the local version

First, we update the version locally, we test it. We fix the changes of all dependencies with the command:

pip freeze > requirements.txt

Updating Ubuntu on the server

Check Ubuntu version:

Update the system and packages in it:

sudo apt update && sudo apt upgrade -y

Updating python on the server

Check version:

If you immediately start the update, an error will occur:

sudo apt install python3.10
Reading Package Lists… Done
Building a dependency tree
Reading Status Information… Done
E: Unable to find package python3.10
E: Couldn’t find any package with pattern ‘python3.10’

First, let’s install the necessary components to add custom PPAs:

sudo apt install software-properties-common -y

Let’s add the deadsnakes/ppa PPA repository to the list of sources in the APT package manager:

sudo add-apt-repository ppa:deadsnakes/ppa -y

Let’s run an APT update to update the package manager – a new imported PPA will appear:

Now we can install the new version of python 3.10:

sudo apt install python3.10

Check the installed version:

Also check the current default version:

Install additional standard library modules (venv):

sudo apt install python3.10-venv -y

For C extensions:

sudo apt install python3.10-dev -y

Other modules may also be useful:

sudo apt install python3.10-distutils -y
sudo apt install python3.10-lib2to3 -y
sudo apt install python3.10-gdbm -y
sudo apt install python3.10-tk -y

Many additional python modules are needed for the libraries to work, without them they will not be installed and errors will come out.

Change the version of python in the “default” system

By default, python will point to the old version.

For example, Python 3 points to Python 3.8. This means that when we run python3 it will run as python 3.8, we want to run it as python 3.10.

To do this, add alternatives:

sudo update-alternatives –install /usr/bin/python3 python3 /usr/bin/python3.8 1
sudo update-alternatives –install /usr/bin/python3 python3 /usr/bin/python3.10 2

Select the required assembly:

sudo update-alternatives –config python3

Installing a new environment

Rename the old environment directory (we must be in the directory of the site with the virtual environment):

mv venv venv-old-python-3.8

Then in this directory we create a new virtual environment:

Pulling changes:

Activate the shell:

Install dependencies:

pip install –upgrade pip
pip install -r requirements.txt

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *