Home Raspberry Pi OS How to update Python on Raspberry Pi

How to update Python on Raspberry Pi

by Abraham
Published: Updated:
upgrade python raspberry pi

Raspbian is the default OS of a Raspberry Pi. At the time of writing this article, the latest version of Raspbian is 5.10. If your Raspbian OS version has not been updated, use the command line below to update it to the newest version:

sudo apt-get update

The default Raspbian OS comes with Python 3. However, we know that there are other new stable releases. The big question is, how do I update my current python version on Raspberry Pi?

The first step is to ensure python is installed in your OS. Even though the default Raspbian OS comes with a pre-installed python 3 version, there are some instances when the OS lacks python modules at all. In such cases, you will have to stick to this guideline to complete the installation.

Python packages are present in Raspberry Pi OS archives. To install them, we shall use the apt command as shown below:

sudo apt update
sudo apt install python3-picamera

This is the most usual and ideal method of installing software. The sudo apt update command updates your Raspbian OS while the sudo apt install python3-picamera installs python into your Rapsbian OS.

Note: The default Raspberry Pi comes with Python 2, which contains a python- prefix. Therefore, the picamera package for python version 2 is python-picamera. On the other hand, python3 version packages include a python 3- prefix. To complete a python3 installation, you will use:

sudo apt install python3-picamera

After this installation, some will be successful, but some won’t be. Don’t be worried if you are stuck since this tutorial is here to offer you the necessary assistance you need. Remember that not all python packages are included in the Raspberry Pi OS archives. Besides, the package that comes with this OS by default is predominantly out of date. To install the latest and suitable version to the archives, you will be required to use the Python Package Index (PyPI). This can only be fruitful with the aid of a pip tool.

You have no reason to worry about installing the pip tool since it comes pre-installed in the Raspberry Pi Desktop OS. You should note that the Raspberry Pi OS lite does not have pip pre-installed, and therefore you will install pip on your own as shown below:

sudo apt install pip

In this article, we shall use the apt command to install python3 as illustrated in the commands below:

sudo apt install python3-pip

To install python2, use:

sudo apt install python-pip

The commands will install python modules according to their versions. For instance, pip3 will install python3 modules while pip will install python2 modules.

If you no longer need the python modules, you can remove them using:

sudo pip3 uninstall 
sudo pip uninstall

This command will remove the modules depending on the python version installed on your system. This is shown by the commands above.

As we already know, the Raspberry Pi comes with python 3. However, other stable releases of python have been released, calling upon the user to update their python to the latest version. While installing the latest release, you are required to download and install the latest releases manually. This occurs whenever the Raspbian OS repositories have been updated. Follow the guides below to get the task done.

Downloading and Extracting

With the help of the commands below, download and extract the latest python release as root:

pi@ai-pi:~ $ sudo su
root@ai-pi:/home/pi# cd /usr/src
root@ai-pi:/usr/src# wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgz
root@ai-pi:/usr/src# tar -xf Python-3.7.0.tgz

Installing Dependencies

After completing the previous step, you need the dependencies to install the latest release to your machine. The dependencies are manually installed by executing the following commands:

apt-get update
apt-get install -y build-essential tk-dev libncurses5-dev libncursesw5-dev libreadline6-dev libdb5.3-dev libgdbm-dev libsqlite3-dev libssl-dev libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev libffi-dev

Configuring and installing the latest Python 3

In this step, you will run the configuration file and optimizations. Also, you will make the installation from the source.

Updating links

Upon successful installation, you will update the link to your newly installed python release. In other cases, some users might want to leave their default path to python2 and only update the python3 link as indicated below:

root@ai-pi:/usr/src/Python-3.7.0# ln -s /usr/local/bin/python3.7 /usr/local/bin/python3

Verifying the installed version

Upon completing, all the processes mentioned above, you will then be required to verify if your installation process was successful. The verification process is done by executing the command below:

root@ai-pi:/usr/src/Python-3.7.0# python3 --version

If this command shows Python 3.7.0, which by the release of this article is the latest python release, it means you successfully installed the latest python version. After verifying the installed version of the python release, some users love to clean the src. This process is simple, and you can quickly execute it by running the following commands:

root@ai-pi:/usr/src# rm -Rf Python-3.7.0
root@ai-pi:/usr/src# rm Python-3.7.0.tgz

Note: The process of cleaning the src is optional, and it does not have any impact on our main subject.

Some users have been inquiring how they can revert to the default python3 version? This process is quite simple, and you should not worry at all. All needed is to update the link we created above, and you are good to go. A point to note is that you should avoid updating Raspbian OS since it tends to break this operation, and hence, you might be called to recreate other links after updating Raspbian OS. To be on the safe side, you should shun away from regularly updating Raspbian OS.

Conclusion

We have learned the processes needed when updating python versions in Raspberry Pi. Make sure to copy and paste commands shown in the article to avoid running into errors. Most users tend to write the code by themselves, and at some point, they misspell the codes, thus running into unknown errors. It would also be best to consider that a wrong code could cause havoc to your operating system. Be safe by using the secure method ‘copy & paste’ mentioned above. Update your python version now with the aid of the simple guides and instructions covered in this tutorial.

You may also like

3 comments

Nawal June 1, 2021 - 7:20 am

root@ai-pi:/usr/src# ar -xf Python-3.7.0.tgz

With the above command i got this error:
ar: Python-3.7.0.tgz: file format not recognized

Can you help?

Reply
Lokesh Biswas M July 19, 2021 - 7:25 am

Bro did you get the answer??

Reply
Madalin August 5, 2021 - 7:41 am

It should be tar, not just ar (it’s the second article I read about this with the same mistake….)

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.