Как обновить python на ubuntu
Перейти к содержимому

Как обновить python на ubuntu

  • автор:

Upgrade Python to latest version (3.10) on Ubuntu Linux

Linux systems come with Python install by default, but, they are usually not the latest. Python also cannot be updated by a typical apt upgrade command as well.

To check the version of Python installed on your system run

python keyword is used for Python 2.x versions which has been deprecated

  1. Update Python to the latest version
  2. Fix pip & other Python related issues
  3. While doing the above two, ensure your Ubuntu which is heavily dependent on Python does not break

Updating Python to the latest version

Ubuntu’s default repositories do not contain the latest version of Python, but an open source repository named deadsnakes does.

Python3.10 is not officially available on Ubuntu 20.04, ensure you backup your system before upgrading.

Step 1: Check if Python3.10 is available for install

Check if Python 3.10 is available by running

This will produce the below result, if you see python3.10 it means you can install it

Step 2: Install Python 3.10

Now you can install Python 3.10 by running

Now though Python 3.10 is installed, if you check the version of your python by running python3 —version you will still see an older version. This is because you have two versions of Python installed and you need to choose Python 3.10 as the default.

Step 3: Set Python 3.10 as default

Steps beyond here are tested on Ubuntu 20.04 in VM & WSL2, but are experimental , proceed at your own risk.

Changing the default alternatives for Python will break your Gnome terminal. To avoid this, you need to edit the gnome-terminal configuration file.

Open the terminal and run:

In first line, change #!/usr/bin/python3 to #!/usr/bin/python3.8 . Press Ctrl +X followed by enter to save and exit.

Then save and close the file.

Next, update the default Python by adding both versions to an alternatives by running the below

Choose the selection corresponding to Python3.10 (if not selected by default).

Now run python3 —version again and you should see the latest Python as the output.

Fix pip and disutils errors

Installing the new version of Python will break pip as the distutils for Python3.10 is not installed yet.

Fix Python3-apt

Running pip in terminal will not work, as the current pip is not compatible with Python3.10 and python3-apt will be broken, that will generate an error like

To fix this first remove the current version of python3-apt by running

Then do some cleanup

DO NOT RUN sudo apt autoremove as it will remove several packages that are required. This may break your system if you’re using GUI, if you’re on WSL2 you can proceed.

Finally, reinstall python3-apt by running

Install pip & distutils

Running pip will still throw an error pip: command not found . We need to install the latest version of pip compatible with Python 3.10.

Also, if try to manually install the latest version of pip, it will throw an error like

Or you might also see an error stating No module named ‘distutils.util’ . This is because the distutils module is not installed yet, to install run the below command

Now you can install pip by running

If you get an error like bash: curl: command not found then you need to install curl first by running sudo apt install curl

Now you can run pip and you should see the output of pip —version

Fix pip-env errors when using venv

When you try to create a new virtual environment using python -m venv env , you may into the following error.

You can fix this by reinstalling venv by running

All should be done now. It is complicated, but this is how you update Python to latest version.

Extra

If you have oh-my-zsh installed, you can avoid typing out python3 by running

Install and upgrade Python

If the APT repositories don’t support the old or new version you’re after.

macOS

Note that upgrading Python with Homebrew causes all my virtual envs to break. Using Pyenv is safer.

Install

Using the python@3.9 formula.

Upgrading

I had 3 versions of PY3 installed.

The system default is Python3.7.

And Python 3.8 was known by Brew.

Python 3.9 got installed because sphinx-doc has it as a dependency, but it did not replace the existing one. See notes from the output.

Try this to sort out the linking:

Then you can add this to PATH — /usr/local/opt/python@3 .

Also I can’t just delete the old one as it is used by other Brew packages.

Doing an install of Python 3.9 made it the default without having to update my PATH .

And /usr/local/bin/python3 links to ../Cellar/python@3.9/3.9.0/bin/python3 now.

Dev Cheatsheets

  • Michael Currin

A collection of code snippets and CLI guides for quick and easy reference while coding

How to update python to latest version and set as default

Install Python normally with the deadsnake PPA:

Check if you can access Python:

If so, create a symlink to set it as "default":

If you don’t have it in apt then you can add special repo deadsnakes Python repo for Ubuntu.
And after adding repo you should have all versions in apt form 2.3 to 3.10

And later you can find path to actuall version

and replace is with python3.9 which you can also find with

But sometimes it is not good idea to replace it because system may need older version and it will not work with newer one. I prefer to use python3.9 when I need 3.9 and python3.7 when I really need 3.7 .

After adding this repo it will also inform you when there will be newer version 3.9.x

Get or update Python 3 on your computer

Anthony N. Carrillo

This is the fastest way to get python on your computer that I know of. And while there are many ways I suggest you use Anaconda. One of the best parts is that Anaconda offers installers for all OSs available. And since Python 2 is going to be killed off around 2020 by Python 3. It is best to get the 3.7 version installed.

1 — Download the installers for Windows, Mac, Linux. Although like anything else dealing with Linux I assume you already know all of this.

2 — Click through all of the default settings by clicking the “Continue” or “Install” buttons. While you can change the install settings on this page you should still select “install for me only” setting.

(I assume you are using YOUR computer, NOT a school or work one)

3 — Restart your computer.

And there you are. You should have python on your computer now. But first, read the next sections to make sure you did it right.

Where is the terminal?

If you are unaware of what the terminal is please follow the instructions from learn python the hard way to find out how to get to your terminal on your computer. No matter what OS it is.

To make sure you have the correct Python version

Go to the terminal (don’t type the ‘$’ sign EVER).

1 — In your terminal type $ python —version

You should get back something like Python 2.7.5

2 — If you get back version 2.whatever check for version 3

In your terminal type $ python3 —version

You should get back Python 3.7.0

If you got 3.7. whatever, YAY you did it.

UPDATE python 3

Use the Terminal for the following steps.

If you are in an environment with Python version 3. whatever, the following command updates Python to the latest version in the 3.whatever branch:

(bear in mind that it will take some time)

❌ But if that returns something like:

OR … (if the above fails the next command will do it)

✅ Now that you have done that you should finally see the following.

�� CONGRATS you finished the UPGRADE! ��

And you did not upgrade to the newest available anaconda 3 then try the next step.

The NEWEST Python 3 is 3.8 HOWEVER …
The installer is only up to 3.7 at the time of this writing. But that is OK for now don’t worry about it.

Congrats reader!
I hope that this short article helped you.
Welcome to a new world of possibilities.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *