What is Python? Python is one of the most used programming languages both by the learners and experienced developers. It is easy to understand and uses a more readable language than any other programming language, making it suitable for beginners and makes it easy to develop web applications. Python has also been used widely as a scripting language, enabling automated processes in Linux systems. In technical terms, python is defined as an object-oriented, high-level programming language with integrated dynamic semantics primarily for web and app development.
Python2 vs Python3
To be able to write Python programs, one is required to have installed Python interpreter in their systems. Python 2 was released in the year 2000, and later version 2.7 in 2010. In 2008, Python3 was released and it started gaining more popularity over python2. Currently, Python 3 is the default Python interpreter.
Ubuntu 20.04 and other versions of Debian Linux ship with Python 3 pre-installed. That being said, Python 2 is legacy while Python3 is the future and most developers are currently writing libraries to be used strictly with Python3. However, some companies still use Python2 for legacy reasons.
How do I install Python2 on Ubuntu 20.04? In this guide, we are going to look at how to install Python2 on Ubuntu 20.04. By default, Ubuntu 20.04 only comes with Python3 pre-installed.
Install Python2 on Ubuntu 20.04
Before any installations, first update and upgrade your system apt. Run the commands below:
sudo apt-get update sudo apt-get upgrade sudo reboot
In this guide, we are going to be using PIP (a Python Package Manager) to install various Python packages. We, therefore, need to install pip for Python2. We require Universe repository and get-pip.py script for installing Python 2 PIP tool
sudo add-apt-repository universe sudo apt update
Now, let’s install python2 on Ubuntu 20.04
sudo apt install python2
Next, we are going to download get-pip.py script for installing Python 2 PIP tool using the below command.
curl https://bootstrap.pypa.io/get-pip.py --output get-pip.py
Install Python 2 pip tool on Ubuntu 20.04
Run the command as shown below to install python2 pip tool
sudo python2 get-pip.py
You should seen an output as below when pip is successfully installed.
Collecting pip Using cached pip-20.2.1-py2.py3-none-any.whl (1.5 MB) Installing collected packages: pip Attempting uninstall: pip Found existing installation: pip 20.2.1 Uninstalling pip-20.2.1: Successfully uninstalled pip-20.2.1 Successfully installed pip-20.2.1
You can verify PIP installation by checking on the installed version.
$ pip -V pip 20.2.1 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7)
Install Python2 Virtualenv on Ubuntu 20.04
What is Python virtual environment? Virtualenv is a tool for creating Python isolated virtual environments. It enables managing of python projects packages to avoid installing them globally which could tamper with system tools or other projects. To install virtualenv on Ubuntu 20.04, run the below command
sudo apt install virtualenv
Python2 Virtualenv basic usage
Having installed virtualenv, we need to create a directory for virtual environment in our home directory.
$ mkdir ~/.virtualenvs
Change to the virtualenv created directory
$ cd ~/.virtualenvs
In the directory, create a virtual environment for your project. You can do this by specifying the Python interpreter you wish to use.
$ virtualenv --python=python2 env
The command will create a new directory with a name same as the virtual environment. The directory contains all of the isolated files, packages, modules, and executables required by the new environment. You should see an output as below.
created virtual environment CPython2.7.18.candidate.1-64 in 2445ms creator CPython2Posix(dest=/root/env, clear=False, global=False) seeder FromAppData(download=False, progress=latest, wheel=latest, CacheControl=latest, retrying=latest, six=latest, distlib=latest, distro=latest, contextlib2=latest, webencodings=latest, setuptools=latest, appdirs=latest, html5lib=latest, lockfile=latest, msgpack=late st, ipaddr=latest, pep517=latest, certifi=latest, urllib3=latest, pip=latest, requests=latest, pytoml=latest, chardet=latest, colorama=latest, packaging=latest, pkg_resources=latest, pyparsing=latest, idna=latest, via=copy, app_data_dir=/root/.local/share/virtualenv/seed -app-data/v1.0.1.debian) activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator
To confirm that the environment is installed with the right version of Python interpreter, run the command as shown.
$ ls env/lib python2.7
To activate the newly created virtual environment, use the command below:
$ source env/bin/activate
You should notice that the name of your virtual environment appears on the left of terminal line and in parentheses.
(env) [email protected]:~/.virtualenvs$
You can go ahead to install your required packages for your project using pip command. As an example, let’s install ‘requests module’. It is a python module that enables sending http requests using python.
$ pip install requests
Once done with working in the virtual environment, you can deactivate it by running the below command:
Enjoy using Python2 with Virtualenv on Ubuntu 20.04. Remember to check more interesting guides below:
- Install Python 3.8 on CentOS 7/ CentOS 8
- How to install pip Python package manager on FreeBSD 12
- How to deploy Python 3 Django Application on CentOS 7 with Apache and mod_wsgi
- Install Python 3 / Python 2.7 on CentOS 8/ RHEL 8
- How to install pip3 and pip2 on Debian 10/ Debian 9
Python Learning books:
Python Crash Course, 2nd Edition: A Hands-On, Project-Based Introduction to Programming
$21.49 in stock
34 used from $15.80
Learning Python, 5th Edition
$33.94 in stock
60 used from $24.95
Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and The Cloud
$69.32 in stock
16 used from $58.26
Head First Python: A Brain-Friendly Guide
$41.94 in stock
26 used from $29.99