Upgrade python version 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

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

Check if Python 3.10 is available by running

Читайте также:  Css display block row

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

sudo apt install python3.10 

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:

sudo nano /usr/bin/gnome-terminal 

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

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 Now run
sudo update-alternatives --config python3 

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

Traceback (most recent call last): File "/usr/lib/command-not-found", line 28, in <module> from CommandNotFound import CommandNotFound File "/usr/lib/python3/dist-packages/CommandNotFound/CommandNotFound.py", line 19, in <module> from CommandNotFound.db.db import SqliteDatabase File "/usr/lib/python3/dist-packages/CommandNotFound/db/db.py", line 5, in <module> import apt_pkg ModuleNotFoundError: No module named 'apt_pkg' 

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

sudo apt remove --purge python3-apt 

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

sudo apt install python3-apt 

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

ImportError: cannot import name 'sysconfig' from 'distutils' (/usr/lib/python3.10/distutils/__init__.py) 

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

sudo apt install python3.10-distutils 

Now you can install pip by running

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py sudo python3.10 get-pip.py 

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.

Error: Command -Imensurepip--upgrade--default-pipYou can fix this by reinstalling venv by running
sudo apt install python3.10-venv 

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

Now you can run your files with py or python .

Источник

How to Upgrade Python to 3.9

Every fresh Python release comes with bug fixes and new features. Python 3.9, the latest point release at the time of writing, comes with features such as improved time zone support, dictionary updates, and more flexible decorators.

This tutorial shows you how to upgrade Python to version 3.9 on all the major operating systems — Windows, macOS, and Linux.

How to Upgrade Python to 3.9

  • Administrative rights on the operating system you are using.
  • Knowledge of which Python version is currently on your system. If you need help finding out the version of your Python installation, check out How to Check Python Version.

Note: If you are upgrading from a Python 2 release and do not have Python 3 installed, read our comprehensive guides on how to install it on:

Upgrading Python on Windows OS

To upgrade Python on Windows, download the installer or search for the app in the Microsoft Store.

Upgrade to Python 3 with the Installer

1. In your browser, visit the Python Releases for Windows section on the official Python website.

2. Click the Download Python button to download the installation file on your computer.

Downloading the newest version of Python for Windows

3. Next, run the Python installer. If you are upgrading from another point release of Python 3 (for example, 3.8.10), the installer suggests to install Python 3.9. Select Install Now to install Python with recommended options, or select Customize Installation to pick the install location and features.

Starting installation of Python 3 in Windows

If you already have an older version of the same Python release (for example, 3.9.1), the installer offers to upgrade your Python installation. Proceed by selecting Upgrade Now.

Starting the upgrade of Python 3 in Windows

4. When the installation finishes, check whether the new version of Python has been installed successfully. Open Windows PowerShell and type:

The output should show the latest version of Python, as in the image below.

Confirming the successful Python 3 installation in Windows

Install Python 3.9 from the Microsoft Store

If you want to use Python 3.9 to learn the basics or test some simple concepts, find and install the Python 3.9 app from the Microsoft Store.

1. Go to Microsoft Store and type Python in the search field.

2. Select Python 3.9 from the search results that appear.

Searching for Python 3 in Microsoft Store

3. Click the Get button to start the installation.

Starting Python 3.9 installation in Microsoft Store

Start the interactive Python 3.9 experience by finding the app in the Start Menu.

Upgrading Python on macOS

On macOS, Python can be installed, upgraded, and maintained using the command line interface or the GUI.

Upgrade Python using Homebrew

Install Python in the macOS terminal using the Homebrew package manager. If you do not have Homebrew, install it by typing the following script in the terminal:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

Then, proceed with the steps:

1. Update Homebrew by running:

2. If you are upgrading from Python 2, install Python 3 with the command:

If you already have a version of Python 3 installed, upgrade the package with the brew upgrade command:

Upgrade Python with the Installer

1. In your browser, navigate to the Python Releases for macOS page, on Python’s official website.

2. Click the link to download the latest Python 3 release on your computer.

Downloading the Python installer for macOS from the official website

3. Run the installer. Go through the installation steps by clicking Continue, agreeing to the License, and confirming the installation location and type.

Starting a Python 3.9 installation in Mac OS using the installer

4. Once the installation is complete, select Close.

The installer confirms the successful installation of Python 3.9 on MacOS

5. Finally, confirm that the new Python version has been successfully installed by typing the following in terminal:

The output should display the latest version of Python.

Checking the Python version in macOS

Upgrading Python in Linux

This article uses Ubuntu and its APT package manager to upgrade Python. If you are using a different Linux distribution, replace the apt Linux command with the appropriate command featured for your package manager.

Warning: Many Linux systems have Python 2 installed as the system version. Removing Python 2 could cause a system error. If you are planning to install Python 3 on Linux, install it alongside Python 2 and invoke it with the python3 command.

1. Start by updating the repositories:

2. Next, install Python 3.9 by running:

sudo apt install python3.9

When prompted, type Y to start the installation.

Installing Python 3.9 in Ubuntu with apt

3. Once Python installs, invoke the 3.9 version by running:

4. However, checking the installation with the python3 —version command still returns the old version. To fix this, you need to create a list of update alternatives. First, add the old version to the list with the command:

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.[old-version] 1 

5. Now add the new version:

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2

Adding Python 3.9 to update alternatives

6. Next, type the following command to configure the priority status of the versions:

sudo update-alternatives --config python3

The output displays the available choices and their assigned number (in the example below, the numbers are 0, 1, 2). Type the number of the the version you wish to use and press Enter.

Configuring update-alternatives for Python 3 in Ubuntu

7. If you are not planning to use the old version of Python, remove the symlink that contained the previous Python 3 version with:

8. Then, replace the symlink with the new version:

sudo ln -s python3.9 /usr/bin/python3

9. Now, check the default version:

The output should confirm the successful installation and setup of the latest available version.

Confirming the successful change of Python 3 version in Ubuntu

Why Should You Upgrade Python?

Since Python 3 was not a backward-compatible release, for a long time Python 2 remained the version of choice for those who wanted a stable development environment. Some services like Google App Engine did not support Python 3 for a long time.

However, given that the official support for the final Python 2.7 release has ended, upgrading to Python 3 is now strongly recommended. Python 3 is faster, and its syntax is more user-friendly.

If you already work with Python 3, upgrading to the latest point release gives you all the security updates and bug fixes.

After reading this tutorial, you should know how to upgrade your Python 3 version on Windows, macOS, and Linux. If you want to learn more about Python, read our article on Python data types.

Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.

Windows does not come with the Python programming language by default. However, you can install Python on your Windows server or local machine in just a few easy steps.

Flask is one of the most popular web application frameworks written in Python. This article explains how to install Flask in a virtual testing environment and create a simple Flask application.

Python is a popular programming language often used to write scripts for operating systems. Learn how to install Python 3.8 on Ubuntu 18.04 or 20.04.

NumPy (Numerical Python) is an open-source library for the Python programming language. It is used for scientific computing and working with arrays. Learn how to install it in this tutorial.

Источник

Оцените статью