Computer >> Computer tutorials >  >> System >> Linux

How to Install Python in Ubuntu

Almost every Linux distribution comes with a version of Python included in the default system packages. But on occasion, due to some reasons, you might not find Python installed on an Ubuntu system.

Let's take a closer look at how you can install Python on Ubuntu, with a brief guide on updating the Python package as well.

How to Check If Python Is Installed on Your System

Python is a powerful, high-level scripting language that is used by many developers around the globe. The language is ideal for a variety of real-world applications including web development, web scraping, and penetration testing. You can even build a Telegram bot using Python.

To check if Python is installed on your system or not, open up your terminal by pressing Ctrl + Alt + T. Type in "python" and press Enter.

If you see the following output in your terminal, then you have Python installed on your computer.

Python 3.9.1 (default, Dec 13 2020, 11:55:53) 
[GCC 10.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

This output provides information on the version of Python your system is running along with the current date and time.

On the other hand, if you see an error that states "bash: python: command not found", then sadly your Ubuntu system doesn't have Python installed.

You can also check for the Python version by simply typing the following command in your terminal.

python --version

The output will give you details on which version of Python is installed on your computer.

How to Install Python on Ubuntu

Installing Python on a Linux-based operating system is easy. You can get the latest version of Python on your Ubuntu machine from multiple sources. Here are some of the recommended ways to do the same.

Install Python Using Apt

Apt, or Advanced Package Tool is the default package manager that you will find on Ubuntu. You can download the Python package from the official Ubuntu repository. Here's how to do it.

  1. Open up your terminal by pressing Ctrl + Alt + T.
  2. Update your local system's repository list by entering the following command:
    sudo apt-get update
  3. Download the latest version of Python:
    sudo apt-get install python
  4. Apt will automatically find the package and install it on your computer.

Use Deadsnakes PPA to Install Python 3 on Ubuntu

If for some reason, you are unable to download the Python package from the official Ubuntu repository, you can try adding the Deadsnakes PPA to your system repository list. PPAs or Personal Package Archives are repositories that are specially designed for Ubuntu users.

By default, you can't add PPAs to your system's package lists. The "software-properties-common" package provides you an efficient way to manage and add PPAs to your system.

  1. Install the above-mentioned package on your system by typing in the following command:
    sudo apt-get install software-properties-common
  2. Add the official Deadsnakes PPA link to your system's repository list:
    sudo add-apt-repository ppa:deadsnakes/ppa
  3. Update your system's package lists:
    sudo apt-get update
  4. Download the latest version of Python from the added PPA:
    sudo apt-get install python3

Since the Deadsnakes PPA has almost every version of Python in its database, you can install older versions of Python as well. Just replace the package name with the version of python you want to install on your computer.

sudo apt-get install python3.2
sudo apt-get install python3.3
sudo apt-get install python3.8

Install Python 3 on Ubuntu From Source Code

You can also download and build the latest version of Python from the official Python website. Although compiling the source code might seem a bit daunting to you at first, it'll become easier once you know the process.

  1. Update your system's local repository list:
    sudo apt-get update
  2. Install supporting dependencies on your system with Apt:
    sudo apt-get install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget
  3. Make a new directory to store the Python source files:
    mkdir /python && cd /python
  4. Download the Python source code from the official FTP server:
    wget https://www.python.org/ftp/python/3.9.1/Python-3.9.1.tgz
  5. Extract the TGZ file that you just downloaded:
    tar –xf Python-3.9.1.tgz
  6. You need to perform tests and optimizations before installing Python. This is important as it increases the execution of your code by 10-20 percent:
    cd python-3.9.1
    ./configure --enable-optimizations
  7. Build the package using the MakeFile present in the directory:
    sudo make install

After you've implemented these steps, check if Python has been installed on your computer by typing python --version in your terminal.

Note that Python modules are managed through PIP. PIP is a package management system that is used to download and add libraries from the Python Package Index. Installing Python PIP on your system is important if you want to use modules on your Python project.

Updating Python to the Latest Version

First of all, make sure that you have an outdated version of Python installed on your system. You can do this by entering python --version in your terminal. Note down the version details.

You can find out what's the latest version available by searching the internet. A quick Google search on "python latest version" would suffice. If the two version numbers don't match, then you are probably running an outdated version.

Upgrading to the latest version is easy with Ubuntu's Advanced Package Tool. If you have installed Python on your system using Apt or the Deadsnakes PPA, enter the following command to download the latest version of Python:

sudo apt-get install python

You can also use the --only-upgrade flag to update your packages.

sudo apt-get --only-upgrade install python

For those who have compiled the source code on their own, you can head over to the Python FTP and grab a copy of the latest version. You will have to follow the steps all over again, however.

Running Python on Ubuntu

Python comes preinstalled on almost every Linux system and is available on official distribution repositories as well. If you still don't get Python installed on your computer, then you can easily download it using Ubuntu's package manager.

The Python language is used in a variety of different sectors, and its applications are enough to demonstrate how powerful it is. Programming languages have become important as because of the growing demand for developers across industry.