How To Install And Uninstall Python 3 on Ubuntu

Table of Content
    Add a header to begin generating the table of contents
    How to Install and Uninstall Python 3 on Ubuntu
      Add a header to begin generating the table of contents

      Python is an interpreted, high-level programming language developed by Guido van Rossum in 1991. It’s an excellent programming language for both beginners and professional developers. Moreover, it is widely utilized in web development, machine learning, automation, and system scripting.

      Over time, Python has undergone multiple major versions, with Python 2.x and Python 3.x being the most prominent. Python 3 is the modern version and is included by default in most major Linux distributions, including Ubuntu. However, if you’re using a version of Ubuntu older than 20.04 and want to use Python 3 instead of Python 2, you can install it and set it as the default Python.

      In this tutorial, you will learn how to install and uninstall Python 3 on Ubuntu, as well as how to set it as the default version and use it.

      How To Install Python 3 on Ubuntu

      Python 3 is pre-installed by default in the latest versions of Ubuntu, such as Ubuntu 22.04. However, if you wish to install it manually, there are several methods available, including:

      • Via apt Package Manager
      • Via PPA Repository
      • Via Source Code

      1. Via apt Package Manager

      It’s important to refresh the package repository before installing any application. To do so, execute the following apt update and upgrade command:

      $ sudo apt update && sudo apt upgrade

      Next, run the apt install command to install Python 3 on Ubuntu from its official repository:

      $ sudo apt install python3
      Install Python 3 on Ubuntu via official repository

      Afterward, verify the installation of Python 3 by running the following command to check its version:

      $ python3 —-version
      Check Python 3 Version in Ubuntu

      2. Via PPA Repository

      Another way to install Python 3 on Ubuntu is via a PPA (Personal Package Archive), which provides access to the latest Python versions unavailable in the default repositories.

      First, update your system’s package repository, then execute the apt install command to set up the software-properties-common package, which allows you to add PPA repositories:

      $ sudo apt install software-properties-common
      Install Software Properties Common in Ubuntu

      Next, add the PPA Deadsnake repository. To do this, execute the following add-apt-repository command:

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

      The Deadsnake PPA provides updated Python versions and allows you to install and manage newer or alternative versions of Python on Ubuntu.

      Addd PPA Repository

      After adding the repository, run the apt update command to refresh the system packages. Then, execute the following apt install command to install Python 3 on Ubuntu:

      $ sudo apt update
      $ sudo apt install python3.13
      Install Python 3.13 on Ubuntu

      During the installation, you’ll receive a prompt. Press Y to proceed with the installation:

      Press Y To Continue installing Python 3 in Ubuntu

      Finally, you’ve successfully installed Python 3.13 on your Ubuntu system.

      3. Via the Source Code

      You can also install Python 3 on Ubuntu using the source code as an alternative method.

      Start by installing the necessary libraries and utilities needed for Python 3:

      $ sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev libbz2-dev
      Install prerequisite Libraries for Python 3 in Ubuntu

      Afterward, head to the official Python website to download the latest version of Python 3. Then, click on the Gzipped source tarball file:

      Python Official Website

      Once the download is complete, extract the Gzipped source tarball by running the following tar -xvf command:

      $ tar -xvf Python-3.13.0.tgz
      Extract tar File of Python 3 setup

      Next, navigate to the Python-3.13.0 directory and run the configure script:

      $ cd Python-3.13.0 
      $ ./configure --enable-optimizationschecking
      Execute Configure Command

      Afterward, execute the make command to compile the Python-3.13.0 source files:

      $ make
      Run Make Command to compile Python 3 in Ubuntu

      This compilation will take some time.

      Next, execute this make altinstall command to install Python 3 in Ubuntu. The altinstall is used to prevent overwriting the system’s default Python version:

      $ sudo make altinstall
      Execute Make Altinstall Command to install Python 3

      Make Python 3 as Default in Ubuntu

      If you want to use the latest version of Python 3 (Python 3.13.0) instead of the system’s default version (Python 3.10.12), follow the instructions below:

      First, you can check your system’s default Python version:

      $ python3 —-version
      Check Python 3 Version

      The output shows Python 3.10.12 is the default version.

      Next, execute the update-alternatives commands mentioned below to apply the changes:

      $ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 1
      $ sudo update-alternatives --install /usr/bin/python3 python3 /usr/local/bin/python3.13 2

      The above commands set Python 3.10 and Python 3.13 as alternatives, with Python 3.13 having a higher priority:

      Execute Update Alternatives Command To Change Default Version

      Afterward, execute the update-alternatives –config command to view a list of Python versions installed on your system:

      $ sudo update-alternatives --config python3
      Excute Update Alternatives Command With Config Option

      Here, simply press Enter to keep the current selection [*].

      Now, run the python3 –version command again to verify the default version:

      Check Python 3 Version

      The output indicates that Python version 3.13.0 is now the system’s default version.

      How To Use Python 3 on Ubuntu

      As discussed earlier, Python is a high-level language that can be used for numerous programming projects. Below is a simple example of Python 3 code to display a message.

      First, create a Python 3 file (code.py) using the touch command and open it in your preferred editor. In this case, we’ll use the nano editor:

      $ touch code.py
      $ nano code.py

      Next, write any code in Python 3 file:

      print("Welcome to this Article to learn about Python3")

      Afterward, run the python3 command to execute the code of the Python 3 file (code.py):

      $ python3 code.py
      Execute Python File

      Above is the output of a simple Python 3 program.

      How To Uninstall Python 3 on Ubuntu

      If you want to remove the Python 3 on Ubuntu, execute the following apt remove command:

      $ sudo apt remove python3.13

      However, if you want to remove all Python 3 dependencies, use the following apt remove –purge command:

      $ sudo apt remove --purge python3.13

      This way, we’ve learned how to install and uninstall Python 3 in Ubuntu.

      Conclusion

      In this article, you’ve learned how to install Python 3 on Ubuntu using three different methods: via the apt command, via the source code, and through the PPA repository. You can choose any of these methods. Additionally, you explored how to use Python 3 with a simple example, and finally, you’ve learned how to uninstall Python 3 on Ubuntu.

      Latest Post

      Contents
        Add a header to begin generating the table of contents
        How to Install and Uninstall Python 3 on Ubuntu
        Latest Post
        Do You Enjoy My Blog

        Supports from reader you like keeps this blog running. By a cup of coffee for me , its a affordable way to show your appreciation

        Previous Post
        ABOUT THE AUTHOR
        Samina Manzoor

        I'm a technical writer with a Bachelor's in Computer Science. Through my research and writing, I aim to provide readers with comprehensive, informative articles that can assist them make informed decisions about their technological needs.

        Scroll to Top