How to Install Python 3 on Ubuntu: A Comprehensive Guide

How to Install Python 3 on Ubuntu Linux
How to Install Python 3 on Ubuntu Linux

To start developing applications using Python 3, we need to install Python 3 on our machines. Article Python 3 Installation on Windows provides comprehensive details about installation of Python 3 on Windows 10 or Windows 11.

Linux-based production environments are frequently used to host commercial applications, making it essential to understand the installation of Python 3 on Ubuntu. This article provides a step-by-step guide for installing Python 3 on Ubuntu Linux.

Python 3.6 Installation on Ubuntu 16.10

Follow the following steps to install Python 3 up-to Python 3.6 version and on Ubuntu 16.10 or newer version.

Check if Python already exists

Before proceeding with the installation, check if Python is already installed on your system by using the following commands:

python --version
python3 --version

If Python is already installed, it will show the Python version that is installed on your machine.

Update and Upgrade Ubuntu 16.10 Operating System

Before installing the latest Python 3 version on Ubuntu 16.10 Linux, we need to update and upgrade the Linux Operating System to its latest packages. To ensure your system is ready for the latest Python installation, update and upgrade your Ubuntu 16.10 OS using these commands:

sudo apt-get update
Updating Ubuntu Linux for Python installation
Updating Ubuntu Linux for Python installation
sudo apt-get -y upgrade
Ubuntu Linux Upgrade
Ubuntu Linux Upgrade

Install Python 3 on Ubuntu 16.10 or newer

Now, you can easily install Python 3 on Ubuntu 16.10 or newer versions with the following command:

sudo apt-get install python3
Python installation command in Ubuntu - Installing Python 3 on Ubuntu 16.10 or newer
Python installation command in Ubuntu – Installing Python 3 on Ubuntu 16.10 or newer

Python 3.7 or newer Installation on Ubuntu Linux

For installing a newer version of Python (3.8 or higher) on Ubuntu Linux, follow these steps:

sudo apt-get update
sudo apt-get -y upgrade

Add Deadsnakes PPA to System’s Package Source

The Deadsnakes PPA is recommended for installing Python versions newer than 3.6.

Install prerequisites for Deadsnakes PPA

sudo apt-get install software-properties-common -y

Add Deadsnakes PPA Repository

sudo add-apt-repository ppa:deadsnakes/ppa

Update Ubuntu Linux

sudo apt-get update

Install Python 3.7 or newer on Ubuntu

sudo apt-get install python3.12

Verify Python Installation on Ubuntu

python3 --version

Installation of PIP

PIP is a package manager for Python that allows you to install additional packages and libraries. When you install Python using the apt-get command on Ubuntu, you may need to install PIP separately.

Python comes with the ensurepip module, which can install PIP in a Python environment and is included with the Python 3 installation.

python -m ensurepip --upgrade
python get-pip.py

Summary

In this article we followed the steps to install Python 3 on Ubuntu Linux. Following steps were followed:

  • Python 3.6 Installation on Ubuntu 16.10
    • Check if Python already exists
    • Update and Upgrade Ubuntu 16.10 Operating System
    • Install Python 3 on Ubuntu 16.10 or newer
  • Python 3.8 or newer Installation on Ubuntu Linux
    • Add Deadsnakes PPA to System’s Package Source
    • Update Ubuntu Linux
    • Install Python 3.7 or newer on Ubuntu
    • Verify Installation of Python 3 on Ubuntu
  • Installation of PIP

Interview Questions & Answers

Q: How to install additional Python packages on Ubuntu after installing Python 3?

After installing Python 3, we can use Python Package Installer like pip, to install additional Python packages.

  • Ensure that pip is installed => sudo apt install python3-pip
  • Install package using pip => pip3 install package-name

Q: How to manage multiple Python versions on Ubuntu?

Following steps can be followed to manage multiple Python versions on Ubuntu:

  1. Using update-alternatives:
    • Install the necessary Python versions.
    • Configure update-alternatives to manage different versions: sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.x 1
    • Use the following command to select the default Python version: sudo update-alternatives --config python
  2. Using pyenv:
    • pyenv is a tool that allow to easily switch between multiple version of Python
    • Install pyenv => curl https://pyenv.run | bash
    • Follow the setup instructions provided by pyenv.
    • Install and switch between Python versions => pyenv install 3.x.x OR pyenv global 3.x.x

Q: How to set up a virtual environment in Python 3 on Ubuntu?

Virtual environments (or venv) are important for a individual project. Virtual environment isolate dependencies and libraries for different projects, avoiding conflicts between packages required by different applications. Follow the following steps to configure the virtual environment in Python 3 on Ubuntu.

  1. Install the venv module => sudo apt install python3-venv
  2. Create a Virtual Environment for you project
    • Navigate to your project directory and create a virtual environment => python3 -m venv myenv
    • A directory called myenv with a standalone Python environment will be created.
  3. Activate the Virtual Environment:
    • Activate the environment using => source myenv/bin/activate
    • This will update the prompt, indicating that the virtual environment is now active.
  4. Deactivate the Virtual Environment:
    • To deactivate, simply run from the project directory => deactivate

Q: What could be the possible reasons if Python 3 installation fails on Ubuntu?

Similar to Windows, Python 3 installation on Ubuntu can fail because of multiple reasons:

  1. Outdated Package Index – Current package list on Ubuntu might be outdated. That is why we always better to update Ubuntu before installing any software. Run sudo apt update to update and to resolve issue.
  2. Conflicting Python Versions – If another version of Python already installed and new installation get conflicted with previous Python version. Remove previous Python version and re-install the new version.
  3. Lack of Disk Space – Insufficient disk space can cause the installation to fail. Ensure there is sufficient disk space before starting the installation.
  4. Repository Issues – If the apt package manager is unable to find the Python package, it could be due to misconfigured repositories or network issues.

Q: How to uninstall all Python packages at once with pip?

Follow the following steps to uninstall all Python packages at once with pip.

  1. First step is to list all the Python packages using pip. Use the following command => pip freeze > packages.txt
  2. If we specifically want to filter some packages, then we can use grep with pip command => pip freeze | grep python > packages.txt 
  3. We can change the grep text with any other text that we want to filter.
  4. Now we can uninstall all the packages listed in packages.txt using uninstall command => pip uninstall -r packages.txt -y
  5. In this command we are using -r for recursive, file name which contains package list to uninstall and -y for yes (so it will not ask user permission to uninstall every package).
  6. Finally, we can delete the package.txt file => rm packages.txt

Q: What are the risks of using pip to install packages in Python?

pip is the most common package manager to install packages in Python. But, it involves some risks in using pip to install packages in Python.

  • Dependency resolution – Many packages are dependent on other packages as dependency. pip package manager is not able to resolve underlying dependencies automatically. This can lead to broken or corrupt installation of the packages. For smooth installation of the package, we have to install package dependencies separately.
  • Version Conflicts – Some package versions are very much dependent on very specific version of its dependent package. pip package manager is not able to resolve the dependency version conflicts.
  • Malicious Packagepip sometimes install packages from unofficial sources. As package from unofficial sources can be malicious or may include bugs in it. This creates security risks.
  • Interference with other projects – It is generally safe to install packages within project specific virtual environments. If it is not the case, then pip package manager will install packages on global environment. This can lead to interference (version conflicts of packages) with other projects.
  • Python Versionpip package manager does not choose package version based on the underlying Python version. Since all package versions are not compatible with all versions of Python. This can lead to conflict of package version with Python version.
  • Unwanted Package Installationpip package manager can install large number of dependencies with a package installation. These many dependencies may not be required for the project. This leads to extra dependency management and increase in disk space usage. This also reduces the performance of the application.
  • Incompatible Licensespip package manager does not verify the licenses of the package to be installed. This Installation of packages without checking their licenses can lead to legal issues.
  • Outdated and Deprecated packagespip package manager does not verify if the package has active community and can support in case of any issues. If we install any package which is outdated, deprecated and not active support, then it can have bugs and can expose our project to security vulnerabilities.

Test Your Knowledge: Practice Quiz

How to Install Python 3 on Ubuntu Linux

Quiz on How to Install Python 3 on Ubuntu

Test Your Knowledge: Install Python 3 on Ubuntu Quiz

1 / 3

1. What is the command to install Python 3 on Ubuntu 16.10 or newer?

2 / 3

2. What command adds the Deadsnakes PPA to the system's package source?

3 / 3

3. How can you check if Python is already installed on an Ubuntu system?

Your score is

0%

Related Topics

  • Understanding Scope in Python Programming (with Example Programs)
    In previous articles, we learned about following topics: Understanding Scope in Programming Generally, in the scope of a program, a variable can be declared only once with the same name. But, it’s possible to declare variables with the same name in different scopes within the same program. Changing the value of a variable within a…
  • Understanding Number Datatype in Python: Decimal, Octal and Hexadecimal Numbers
    Understanding the Number Datatype in Python: A Comprehensive Overview Number datatype in Python is a combination of different number format classes like Integer, Float, and Complex. Python supports the Decimal, Octal decimal, and Hexadecimal number systems. Program – Integer Number datatype Output of the Integer Number datatype program: Note from the output: Exploring Float Number…
  • Introduction to Variables in Python & Variable Data Types
    What are Variables in Python In Python, variables act as references to reserved memory locations where actual values are stored. Each variable name points to a specific memory address that holds the assigned value. In this case: How Variable Memory Works When we define variables, like height = 175, Python allocates a specific memory location…
  • What Are Comments and Docstring in Python (With Programs)?
    Before we start learning about Comments and Docstring in Python and its concepts, we should setup our Python Integrated Development Environment (IDE) on our local machine. Read through the following articles to learn more about What is an IDE, Popular IDEs for Python and Python installation on your machine. Introduction to Comments in Python In…
  • Popular IDEs for Python in 2024 – Best Python Development Environments
    IDEs are very important tool (application) for the software development. Working with good IDE improves the performance and working capability of a software developer. That is why it’s important to choose a good IDE for software development. Different developers choose different IDEs as per their working. Before we start looking into details of the popular…

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *