How to update python ubuntu
How to update python ubuntu
How to upgrade to Python 3.9.0 on Ubuntu 18.04 LTS
In this article, we will upgrade to python 3.9.0 and configure it as the default version of python in Ubuntu 18.04 LTS.
The current stable version of python released on 14 Oct. 2020. Many ubuntu users are facing problems during upgrading python to the latest version. Python 3.8 available as default when we install Ubuntu 18.04 LTS. Python 3.9.0 is not available in default Ubuntu 18.04 repositories.
So let’s start with checking the currently installed version of Python on your system.
As seen in the image above, my currently installed Python version is 3.6.9 but yours may differ.
Table of Contents
Install Python 3.9.0
Follow the simple steps to install and configure Python 3.9.0
Step 1: Add the repository and update
Latest Python 3.9 not available in Ubuntu’s default repositories. So, we have to add an additional repository. On launchpad repository named deadsnakes is available for Python Packages.
Add the deadsnakes repository using the below commands.
Update the package list using the below command.
Verify the updated Python packages list using this command.
As seen in the image above, Now we have Python 3.9.0 available for installation.
Step 2: Install the Python 3.9.0 package using apt-get
install Python 3.9.0 by using the below command :
Step 3: Add Python 3.6 & Python 3.9 to update-alternatives
Add both old and new versions of Python to Update Alternatives.
Step 4: Update Python 3 for point to Python 3.9
By default, Python 3 is pointed to Python 3.6. That means when we run python3 it will execute as python3.6 but we want to execute this as python3.9.
Type this command to configure python3:
You should get the above output. Now type 2 and hit enter for Python 3.9. Remember the selected number may differ so choose the selection number which is for Python 3.9.
Step 5: Test the version of python
Finally, test the current version of python by typing this :
You should get Python 3.9 as output.
In this article, we learn how to upgrade python to the latest version that is 3.9.0 in Ubuntu 18.10.
Share your thoughts in the comment section. Happy Learning …!!
How to Update Python on Ubuntu?
First of all, you want to open the Ubuntu 20.04 shell terminal application on Ubuntu 20.04 desktop. For that, you have to utilize the everlasting shortcut key “Ctrl+Alt+T” on the desktop of Ubuntu 20.04.
The shell application will be opened. Now, you have to check the version of a Python language already installed on your system. For this, you will be executing a version command of Ubuntu 20.04 with the keyword python. The command has been shown in the attached screenshot of the terminal. It clearly shows that any version of Python language is not configured in our system and we need to install it by ourselves. Many commands can be used.
So, we have to install the python version on our system supported by Ubuntu 20.04. Before that, we have to update our system. The apt package will be utilized in the update command to do so. The command is displayed in the image as well. The execution of this apt command leads us to enter the sudo password of our system. After adding the password, our system will update itself and its packages. The command and the process have been shown in the image.
It’s time to install the python old version on our system first. This is necessary to be done because we have to then update the python to the newest version after that. We have been installing Python version 2 on our system using the apt installation command displayed below.
After making the package ready, it will pause the process of installation for a while to ask us to affirm our action. It will let us know that 16.5 MB of space will be occupied by this package and if we want to continue installing it. So, we tapped “Y” to continue the installation process.
The installation process will start progressing. After it reaches 100 percent, it will be completed. Let’s just update the Linux system once again with the “apt” package.
After the successful installation of Python, we will be checking the version of python that has been installed. The version command has been used here once again as illustrated from the image below. We have used the “python2” keyword to check the installed version of Python 2.
When we have checked for the Python 3 versions on your system, it shows that the Python 3.8.2 version has also been installed with Python 2. While Python has come up with the Python 3.9 version as well. So, we need to upgrade this latest version to Ubuntu 20.04.
Let’s begin with the update of Python 3. First of all, you need to install all the required packages and repositories that are prerequisites for Python 3.9. Instead of listing all packages in the installation command, we have to just use the keyword “software-properties-common” in the instruction. We have done it in the command and the process has been started.
It will also pause the installation of required packages to affirm your action. It’s been informed that this process will take 14.3 kb of space on your system and do you want to continue it. So, we have added “y” and tapped the “Enter” button to start the process of installation.
The packages will be installed and configured on our system one by one and may take up to 5 minutes. The completion of this process has been shown in the small image below.
Before the installation of Python 3.9, you must have to add the “ppa” repository to your system. Without “ppa”, you will be unable to install Python 3.9. You need to use the “add-apt-repository” command of an apt package to do so. You have to use the “ppa:deadsnakes/ppa” keyword within your command with sudo privileges. Execute the command shown in the image below and it will set your system for new Python versions.
After a few seconds, the process of adding ppa will ask you to press the Enter button to start adding ppa requirements and packages i.e., prerequisites. we pressed Enter and the system installed the ppa package on our system. The output is shown in the attached photo.
Now, we cannot use the simple apt installation command with the keyword “python3.9” to install the latest python version. We have to install the Python environment variables and development interface with the keywords “python3.9-venv” and “python3.9-dev” as below.
Again, the system has been asking to tap “y” to affirm the action of installation as it takes 114 MB space on our Ubuntu system. We have tapped “y” and pressed the “Enter” key to continue.
The system has started upgrading the python. The process will take some to complete as it will also install the development environment along with the environment variables of Python 3.9.
After the complete installation, we have utilized it to check the Python version. We have found that the Python 3.9 latest version is now configured on our Ubuntu 20.04 system fully.
Conclusion
This article contains all the information and process regarding the update of Python in the Ubuntu 20.04 system. We have started from the very basics by installing the old version of Python on our system i.e., python2. Then, we have shown the way to update the Python version and upgrade it to version 3.9 by utilizing some commands. We hope you liked this article.
About the author
Omar Farooq
Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.
Upgrade Python to latest version (3.10) on Ubuntu Linux
Last Updated: 2022-08-11
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
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
Step 3: Set Python 3.10 as default
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:
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).
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
Also, if try to manually install the latest version of pip, it will throw an error like
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
Fix pip-env errors when using venv
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
Updating Python on Ubuntu system
I want to update the Python build on my Linux box, but the only way I know how to do it is uninstalling the current version and installing the new one. My system is already up to date (I updated yesterday). I wanted to know if there is a way to update a specific program from the command line, like sudo apt-get update
. I know this command doesn’t exist, but I’m hoping something equivalent does.
10 Answers 10
As others already noted, bare sudo apt-get install package will install latest available version, replacing the older one if needed.
But with some software (among which is Python) the situation is somewhat different. Some major, very- and incompatibly-different versions get their own packages. For instance, Python 2.6, Python 2.7, Python 3.1 all live in separate packages on Ubuntu.
Of particular importance is the fact that one of Ubuntu policies is to extensively use Python for writing end-user software. So in fact, fairly large part of the system is written in Python. At the moment, the code runs on Python 2.6 — so this version is the default upon installation; and the code won’t easily run on, say, Python 2.7 — because incompatibilities exist. To switch the system to Python 2.7 there needs to be done a piece of work, consisting of updating and re-testing all the scripts. This can’t be done easily; that is, you can’t just «switch» your system to Python 2.7 and delete the older version.
But. If you don’t care about fancy gears of your system and just need newer Python — see no obstacles. Go and sudo apt-get install python3 and code for 3.x Python bravely; just remember to launch your scripts with python3 and use #!/usr/bin/env python3 shebang line.
Upd: I keep seeing this upvoted; notice that this is a 9-year old answer, things have changed.
What to learn next
From a superuser perspective (not Python developer’s), the next things I’d suggest learning to use:
The virtualenv tool. It’s 100% developer-oriented, but you’ll likely need to use it (perhaps underneath a few wrappers, such as tox ) to work with people’s source packages.
Ruby’s bundler or Cabal sandbox may be familiar analogues.
The conda tool — which is a totally separate python package repository and installer (think: fork of PyPi).
There’s humongous variety of tools in the Python ecosystem in 2020. At the very least, make yourself comfortable with pip before going deeper.
Basic pitfalls
For the brave but unwary, a few classic pitfalls when trying to manually set up a newer CPython on Ubuntu.
Finding a PPA is decent option too; keep in mind that a PPA is just someone else’s private build. Look for credible PPAs with CI/CD running.
How to upgrade to Python 3.10 on Ubuntu 18.04 and 20.04 LTS
In this article, we will upgrade to python 3.10 and configure it as the default version of python in Ubuntu 18.04 and 20.04 LTS.
The current stable version of python was released on 06 Dec. 2021. Many ubuntu users are facing problems during upgrading python to the latest version. Python 3.9 is available as default when we install Ubuntu 20.04 LTS and Python 3.8 is available in Ubuntu 18.04. Python 3.10.0 is not available in default Ubuntu 18.04 and 20.04 repositories.
So let’s start with checking the currently installed version of Python on your system.
As seen in the image above, my currently installed Python version is 3.8.10 but yours may differ.
Table of Contents
Install Python 3.10
Follow the simple steps to install and configure Python 3.10
Step 1: Add the repository and update
Latest Python 3.10 is not available in Ubuntu’s default repositories. So, we have to add an additional repository. On launchpad repository named deadsnakes is available for Python Packages.
Add the deadsnakes repository using the below commands.
Update the package list using the below command.
Verify the updated Python packages list using this command.
As seen in the image above, Now we have Python 3.10 available for installation.
Step 2: Install the Python 3.10 package using apt-get
install Python 3.10 by using the below command :
Step 3: Add Python 3.8 & Python 3.10 to update-alternatives
Add both old and new versions of Python to Update Alternatives.
Step 4: Update Python 3 for point to Python 3.10
By default, Python 3 is pointed to Python 3.8. That means when we run python3 it will execute as python3.8 but we want to execute this as python3.10.
Type this command to configure python3:
You should get the above output. Now type 2 and hit enter for Python 3.10. Remember the selected number may differ so choose the selection number which is for Python 3.10.
Step 5: Test the version of python
Finally, test the current version of python by typing this :
You should get Python 3.10 as output.
In this article, we learn how to upgrade python to the latest version that is 3.10 in Ubuntu 20.04 and 18.10.
Share your thoughts in the comment section. Happy Learning …!!
You May Also Like
How to Install Docker on Ubuntu 20.04 LTS
Install Laravel on Ubuntu 16.04 with Apache
How to Install Node on Ubuntu 18.04 or 20.04
9 Comments
Thank you! Thank you! Thank you! I had been struggling to do this for several days. Worked like a charm.
Thanks much. works!
Hi,
The instructions are nice but I’m hitting this error:
$ sudo update-alternatives –install /usr/bin/python3 python3 /usr/bin/python3.10 2
update-alternatives: error: alternative path /usr/bin/python3.10 doesn’t exist
and I don’t see Python3.10 executable here:
So I can add it into alternatives config:
$ sudo update-alternatives –config python3
There is 1 choice for the alternative python3 (providing /usr/bin/python3).
Selection Path Priority Status
————————————————————
0 /usr/bin/python3.8 1 auto mode
* 1 /usr/bin/python3.8 1 manual mode
and status of install command:
$ sudo apt-get install python3.10
Reading package lists… Done
Building dependency tree
Reading state information… Done
Note, selecting ‘libqgispython3.10.4’ for regex ‘python3.10’
Note, selecting ‘libpython3.10-stdlib’ for regex ‘python3.10’
libqgispython3.10.4 is already the newest version (3.10.4+dfsg-1ubuntu2).
0 upgraded, 0 newly installed, 0 to remove and 19 not upgraded.
$ apt list | grep python3.10
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
libqgispython3.10.4/focal,now 3.10.4+dfsg-1ubuntu2 amd64 [installed]
and OS is: Ubuntu 20.04.4 LTS”
I was able to solve this issue by running the following steps:
Step 1: Current state of the Python version:
$ sudo update-alternatives –config python3
There is 1 choice for the alternative python3 (providing /usr/bin/python3).
Selection Path Priority Status
————————————————————
0 /usr/bin/python3.8 1 auto mode
* 1 /usr/bin/python3.8 1 manual mode
Press to keep the current choice[*], or type selection number:
$
Step 3: Removing existing python3.10 that was attempted before:
$ sudo apt remove python3.10
[sudo] password for vkdevu:
Reading package lists… Done
Building dependency tree
Reading state information… Done
E: Unable to locate package python3.10
E: Couldn’t find any package by glob ‘python3.10’
$
$ sudo apt list | grep python3.10
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
libqgispython3.10.4/focal,now 3.10.4+dfsg-1ubuntu2 amd64 [installed]
$
$ sudo apt remove libqgispython3.10.4
Reading package lists… Done
Building dependency tree
Reading state information… Done
The following packages were automatically installed and are no longer required:
gdal-data libaec0 libarmadillo9 libarpack2 libblas3 libcfitsio8 libcharls2 libdap25 libdapclient6v5 libdouble-conversion3
libepsilon1 libfreexl1 libfwupdplugin1 libfyba0 libgdal26 libgeos-3.8.0 libgeos-c1v5 libgeotiff5 libgfortran5 libgif7
libhdf4-0-alt libhdf5-103 libkmlbase1 libkmldom1 libkmlengine1 liblapack3 libminizip1 libnetcdf15 libodbc1 libogdi4.1
libpcre2-16-0 libpoppler97 libproj15 libqca-qt5-2 libqca-qt5-2-plugins libqgis-core3.10.4 libqhull7 libqt5concurrent5
libqt5core5a libqt5dbus5 libqt5gui5 libqt5keychain1 libqt5network5 libqt5positioning5 libqt5printsupport5 libqt5qml5
libqt5quick5 libqt5sensors5 libqt5serialport5 libqt5sql5 libqt5sql5-sqlite libqt5svg5 libqt5webchannel5 libqt5webkit5
libqt5widgets5 libqt5xml5 libspatialindex6 libspatialite7 libsuperlu5 libsz2 liburiparser1 libxcb-xinerama0 libxcb-xinput0
libxerces-c3.2 libzip5 ocl-icd-libopencl1 odbcinst odbcinst1debian2 poppler-data proj-bin proj-data qt5-gtk-platformtheme
qttranslations5-l10n
Use ‘sudo apt autoremove’ to remove them.
The following packages will be REMOVED:
libqgispython3.10.4
0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
After this operation, 2379 kB disk space will be freed.
Do you want to continue? [Y/n] Y
(Reading database … 130630 files and directories currently installed.)
Removing libqgispython3.10.4 (3.10.4+dfsg-1ubuntu2) …
Processing triggers for libc-bin (2.31-0ubuntu9.9) …
$
Step 4: Ensure that your system is updated and the required packages installed:
$ sudo add-apt-repository ppa:deadsnakes/ppa
This PPA contains more recent Python versions packaged for Ubuntu.
Disclaimer: there’s no guarantee of timely updates in case of security problems or other issues. If you want to use them in a security-or-otherwise-critical environment (say, on a production server), you do so at your own risk.
Update Note
===========
Please use this repository instead of ppa:fkrull/deadsnakes.
Issues can be reported in the master issue tracker at:
https://github.com/deadsnakes/issues/issues
– Ubuntu 18.04 (bionic) Python2.3 – Python 2.6, Python 3.1 – Python 3.5, Python3.7 – Python3.11
– Ubuntu 20.04 (focal) Python3.5 – Python3.7, Python3.9 – Python3.11
– Ubuntu 22.04 (jammy) Python3.7 – Python3.9, Python3.11
– Note: Python2.7 (all), Python 3.6 (bionic), Python 3.8 (focal), Python 3.10 (jammy) are not provided by deadsnakes as upstream ubuntu provides those packages.
$ sudo update-alternatives –config python3
There is 1 choice for the alternative python3 (providing /usr/bin/python3).
Selection Path Priority Status
————————————————————
0 /usr/bin/python3.8 1 auto mode
* 1 /usr/bin/python3.8 1 manual mode
Press to keep the current choice[*], or type selection number:
$
—–> continuation for my previous post —->
Step 7: Download Python 3.10 with the single command below:
$ sudo apt install python3.10
Reading package lists… Done
Building dependency tree
Reading state information… Done
The following packages were automatically installed and are no longer required:
gdal-data libaec0 libarmadillo9 libarpack2 libblas3 libcfitsio8 libcharls2 libdap25 libdapclient6v5 libdouble-conversion3
libepsilon1 libfreexl1 libfwupdplugin1 libfyba0 libgdal26 libgeos-3.8.0 libgeos-c1v5 libgeotiff5 libgfortran5 libgif7
libhdf4-0-alt libhdf5-103 libkmlbase1 libkmldom1 libkmlengine1 liblapack3 libminizip1 libnetcdf15 libodbc1 libogdi4.1
libpcre2-16-0 libpoppler97 libproj15 libqca-qt5-2 libqca-qt5-2-plugins libqgis-core3.10.4 libqhull7 libqt5concurrent5
libqt5core5a libqt5dbus5 libqt5gui5 libqt5keychain1 libqt5network5 libqt5positioning5 libqt5printsupport5 libqt5qml5
libqt5quick5 libqt5sensors5 libqt5serialport5 libqt5sql5 libqt5sql5-sqlite libqt5svg5 libqt5webchannel5 libqt5webkit5
libqt5widgets5 libqt5xml5 libspatialindex6 libspatialite7 libsuperlu5 libsz2 liburiparser1 libxcb-xinerama0 libxcb-xinput0
libxerces-c3.2 libzip5 ocl-icd-libopencl1 odbcinst odbcinst1debian2 poppler-data proj-bin proj-data qt5-gtk-platformtheme
qttranslations5-l10n
Use ‘sudo apt autoremove’ to remove them.
The following additional packages will be installed:
libpython3.10-minimal libpython3.10-stdlib python3.10-minimal
Suggested packages:
python3.10-venv binfmt-support
The following NEW packages will be installed:
libpython3.10-minimal libpython3.10-stdlib python3.10 python3.10-minimal
0 upgraded, 4 newly installed, 0 to remove and 0 not upgraded.
Need to get 5171 kB of archives.
After this operation, 20.0 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu focal/main amd64 libpython3.10-minimal amd64 3.10.4-1 focal2 [820 kB]
Get:2 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu focal/main amd64 python3.10-minimal amd64 3.10.4-1 focal2 [2073 kB]
Get:3 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu focal/main amd64 libpython3.10-stdlib amd64 3.10.4-1 focal2 [1746 kB]
Get:4 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu focal/main amd64 python3.10 amd64 3.10.4-1 focal2 [532 kB]
Fetched 5171 kB in 5s (1098 kB/s)
Selecting previously unselected package libpython3.10-minimal:amd64.
(Reading database … 130626 files and directories currently installed.)
Preparing to unpack …/libpython3.10-minimal_3.10.4-1 focal2_amd64.deb …
Unpacking libpython3.10-minimal:amd64 (3.10.4-1 focal2) …
Selecting previously unselected package python3.10-minimal.
Preparing to unpack …/python3.10-minimal_3.10.4-1 focal2_amd64.deb …
Unpacking python3.10-minimal (3.10.4-1 focal2) …
Selecting previously unselected package libpython3.10-stdlib:amd64.
Preparing to unpack …/libpython3.10-stdlib_3.10.4-1 focal2_amd64.deb …
Unpacking libpython3.10-stdlib:amd64 (3.10.4-1 focal2) …
Selecting previously unselected package python3.10.
Preparing to unpack …/python3.10_3.10.4-1 focal2_amd64.deb …
Unpacking python3.10 (3.10.4-1 focal2) …
Setting up libpython3.10-minimal:amd64 (3.10.4-1 focal2) …
Setting up python3.10-minimal (3.10.4-1 focal2) …
Setting up libpython3.10-stdlib:amd64 (3.10.4-1 focal2) …
Setting up python3.10 (3.10.4-1 focal2) …
Processing triggers for man-db (2.9.1-1) …
Processing triggers for desktop-file-utils (0.24-1ubuntu3) …
Processing triggers for mime-support (3.64ubuntu1) …
Processing triggers for gnome-menus (3.36.0-1ubuntu1) …
$
Step 8: Verify the installation by checking the installed version:
$ python3.10 –version
Python 3.10.4
$
Step 9: Add Python 3.10.4 to alternatives config:
$ sudo update-alternatives –config python3
There is 1 choice for the alternative python3 (providing /usr/bin/python3).
Selection Path Priority Status
————————————————————
0 /usr/bin/python3.8 1 auto mode
* 1 /usr/bin/python3.8 1 manual mode
Press to keep the current choice[*], or type selection number:
$
Command ‘python’ not found, did you mean:
command ‘python3’ from deb python3
command ‘python’ from deb python-is-python3
$ 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
$
$ sudo update-alternatives –config python3
There are 2 choices for the alternative python3 (providing /usr/bin/python3).
Selection Path Priority Status
————————————————————
0 /usr/bin/python3.10 2 auto mode
1 /usr/bin/python3.10 2 manual mode
* 2 /usr/bin/python3.8 1 manual mode
Selection Path Priority Status
————————————————————
* 0 /usr/bin/python3.10 2 auto mode
1 /usr/bin/python3.10 2 manual mode
2 /usr/bin/python3.8 1 manual mode
Press to keep the current choice[*], or type selection number:
$