Every Ubuntu ships with a Python installation. Its version differs depending on the version of Ubuntu. Sometimes you may need to upgrade python in Ubuntu to use a specific library or software. In this article, we will look at how to upgrade python in Ubuntu.
How To Upgrade Python in Ubuntu
Here are the steps to upgrade python 2.7to 3.6, 3.7 in Ubuntu.
Please note : It is not advisable to upgrade python in Ubuntu as it might break other libraries that depend on it. Instead, it is recommended that you create a virtual environment in Ubuntu, and separately install python of your choice in the virtual environment. This way you can run your choice of Python without disturbing any system applications.
1. Install ppa
Open terminal and run the following command to install the right PPA for python 3.6, 3.7.
$ sudo add-apt-repository ppa:deadsnakes/ppa
Also read : How to Install Django in Ubuntu
2. Update system packages
Run the following command to update system packages
$ sudo apt-get update
Also read : How to Install Git in Ubuntu
3. Upgrade Python 2.x to 3.x
First we need to upgrade python to 3.6 and then python 3.7
$ sudo apt-get install python3.6 $ sudo apt-get install python3.7
Also read : How to Change SSH port in Ubuntu
4. Install pip
Install PIP for python 3.x
$ sudo apt install python3-pip
Also read : How to Set Up Automatic Updates in Ubuntu
5. Set Priority
Since we have multiple python versions on our system, we will assign priority for them.
$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1 $ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 2
Also read : How to Redirect Users to Mobile Website
That’s it. Now you will be able to use python 2.7, 3.6 as well as 3.7 in Ubuntu.