Steps to Install Python on ubuntu 20.04 LTS With Examples

Python is a free & open source high-level object-oriented programming language. Its simplicity and easy-to-learn syntax. Using Python, we can build all types of applications, software, automate tasks, and conduct data analysis.

There are few steps to install Python on ubuntu:

Step 1: Update the System.

apt-get update

Step 2: Install the software & add the repository.

apt install software-properties-common
add-apt-repository ppa:deadsnakes/ppa

  • Here is the command output.

root@ip-172-31-25-143:/home/ubuntu# 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.
Reporting Issues
================
Issues can be reported in the master issue tracker at:
https://github.com/deadsnakes/issues/issues
Supported Ubuntu and Python Versions
====================================
Packages
========
The packages provided here are loosely based on the debian upstream packages with some modifications to make them more usable as non-default pythons and on ubuntu. As such, the packages follow debian's patterns and often do not include a full python distribution with just `apt install python#.#`. Here is a list of packages that may be useful along with the default install:
- `python#.#-dev`: includes development headers for building C extensions
- `python#.#-venv`: provides the standard library `venv` module
- `python#.#-distutils`: provides the standard library `distutils` module
- `python#.#-lib2to3`: provides the `2to3-#.#` utility as well as the standard library `lib2to3` module
- `python#.#-gdbm`: provides the standard library `dbm.gnu` module
- `python#.#-tk`: provides the standard library `tkinter` module
Third-Party Python Modules
==========================
==============
For nightly builds, see ppa:deadsnakes/nightly https://launchpad.net/~deadsnakes/+archive/ubuntu/nightly
More info: https://launchpad.net/~deadsnakes/+archive/ubuntu/ppa
Press [ENTER] to continue or Ctrl-c to cancel adding it.

  • Press Enter.
  • Update the packages.

apt-get update

Step 3: Install the Python3 on system.

apt install python3.8

  • To install different version of Python.

apt install python3.9
or
apt install python3.10

  • Check the python version.

python3 --version
or
python3.9 --version
or
python3.10 --version

  • Here is the command output.

root@ip-172-31-25-143:/home/ubuntu# python3 --version
Python 3.8.10

Step 4: Install Python Package manager (PIP).

apt install python3-pip

  • To install python modules using pip.

pip install module-name

For example:

pip install beautifulsoup4

  • Here is the command output.

root@ip-172-31-25-143:/home/ubuntu# pip install beautifulsoup4
Collecting beautifulsoup4
Downloading beautifulsoup4-4.10.0-py3-none-any.whl (97 kB)
|████████████████████████████████| 97 kB 5.6 MB/s
Collecting soupsieve>1.2
Downloading soupsieve-2.2.1-py3-none-any.whl (33 kB)
Installing collected packages: soupsieve, beautifulsoup4
Successfully installed beautifulsoup4-4.10.0 soupsieve-2.2.1

  • To list the python packages.

pip list

  • Here is the command output.

root@ip-172-31-25-143:/home/ubuntu# pip list
Package Version
---------------------- --------------------
attrs 19.3.0
Automat 0.8.0
beautifulsoup4 4.10.0
blinker 1.4
certifi 2019.11.28
chardet 3.0.4
Click 7.0
cloud-init 21.1
colorama 0.4.3
command-not-found 0.3
configobj 5.0.6
constantly 15.1.0
cryptography 2.8
dbus-python 1.2.16
distro 1.4.0
distro-info 0.23ubuntu1
ec2-hibinit-agent 1.0.0
entrypoints 0.3
hibagent 1.0.1
httplib2 0.14.0
hyperlink 19.0.0
idna 2.8
importlib-metadata 1.5.0
incremental 16.10.1
Jinja2 2.10.1
jsonpatch 1.22
jsonpointer 2.0
jsonschema 3.2.0
keyring 18.0.1
language-selector 0.1
launchpadlib 1.10.13
lazr.restfulclient 0.14.2
lazr.uri 1.0.3
MarkupSafe 1.1.0

Step 5: Run the python3 command to launch python on system.

python3

  • Here is the command output.

root@ip-172-31-25-143:/home/ubuntu# python3
Python 3.8.10 (default, Sep 28 2021, 16:10:42)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

  • Python Syntax & Examples:

print("Hello, World!")

  • Here is the command output.

>>> print("Hello, World!")
Hello, World!

  • Create a file.

vim test.py

  • Add the following lines:

x = 5
y = "Hello, World!"
print(x)
print(y)

  • Run the .py file using python.

python3 file-name
python3 test.py

  • Here is the command output.

root@ip-172-31-25-143:/home/ubuntu# python3 myfile.py
5
Hello, World!

Leave a Reply