How to install Python 2 with Virtualenv on Ubuntu 20.04

To
You can download this article in PDF format via the link below to support us.

Download the guide in PDF format

turn off
To

To
To

What is Python? Python is one of the most commonly used programming languages ​​for learners and experienced developers. It is easier to understand and uses a more readable language than any other programming language, making it suitable for beginners and easy to develop web applications. Python is also widely used as a scripting language to automate processes in Linux systems. In technical terms, python is defined as an object-oriented high-level programming language with integrated dynamic semantics, mainly used for Web and application development.

Python2 and Python3

In order to be able to write Python programs, a Python interpreter needs to be installed in the system. Python 2 was released in 2000, and the later version 2.7 was released in 2010. In 2008, Python3 was released, and it became more popular than python2. Currently, Python 3 is the default Python interpreter.

Ubuntu 20.04 and other versions of Debian Linux come with Python 3 pre-installed. That being said, Python 2 is still a legacy, and Python 3 is the future, and most developers are currently writing libraries that strictly work with Python 3. However, some companies still use Python2 for legacy reasons.

How to install Python2 on Ubuntu 20.04? In this guide, we will study how to install Python2 on Ubuntu 20.04. By default, Ubuntu 20.04 only pre-installs Python3.

Install Python2 on Ubuntu 20.04

Before any installation, please update and upgrade the system apt. Run the following command:

sudo apt-get update
sudo apt-get upgrade
sudo reboot

In this guide, we will install various Python packages using PIP (Python Package Manager). Therefore, we need to install pip for Python2. We need the Universe repository and get-pip.py script to install the 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 will use the following command to download the get-pip.py script used to install the Python 2 PIP tool.

curl https://bootstrap.pypa.io/get-pip.py --output get-pip.py

Install Python 2 pip tool on Ubuntu 20.04

Run the command shown below to install the python2 pip tool

sudo python2 get-pip.py

After successfully installing pip, you should see the following output.

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 the PIP installation by checking 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 a Python virtual environment? Virtualenv is a tool used to create a Python-isolated virtual environment. It can manage python project packages to avoid installing them globally, which may tamper with system tools or other projects. To install virtualenv on Ubuntu 20.04, run the following command

sudo apt install virtualenv

Basic usage of Python2 Virtualenv

After installing virtualenv, we need to create a directory for the virtual environment in the home directory.

$ mkdir ~/.virtualenvs

Go to the directory created by virtualenv

$ cd ~/.virtualenvs

In the catalog, create a virtual environment for your project. You can do this by specifying the Python interpreter to be used.

$ virtualenv --python=python2 env

This command will create a new directory with the same name as the virtual environment. This directory contains all isolated files, packages, modules, and executable files needed for the new environment. You should see the following output.

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 whether the correct version of the Python interpreter is installed in the environment, run the command shown below.

$ ls env/lib
python2.7

To activate the newly created virtual environment, use the following command:

$ source env/bin/activate

You should note that the name of the virtual environment is displayed on the left side of the terminal line and in brackets.

(env) [email protected]:~/.virtualenvs$

You can continue to use to install the packages required by the project idea command. For example, we install the “request module”. This is a python module that can use python to send HTTP requests.

$ pip install requests

After the work is done in the virtual environment, you can deactivate it by running the following command:

$ deactivate

Combine Python2 with Virtualenv on Ubuntu 20.04. Remember to check out the following more interesting guides:

  • 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 using 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, Second Edition: Getting started with project-based hands-on programming

$ 39.95 $ 17.00

In stock

19 new
From $17.00 with 13 used $13.99 free shipping

Buy now
How to install Python 2 with Virtualenv on Ubuntu 20.04Amazon.com

As of 12:07 PM, August 10, 2020


Learn Python, 5th edition

Learn Python, 5th edition

$ 74.99 $ 47.83

In stock

17 new
From $43.83 37 used $30.82 free shipping

Buy now
How to install Python 2 with Virtualenv on Ubuntu 20.04Amazon.com

As of 12:07 PM, August 10, 2020


Introduction to Python for Computer Science and Data Science: Learn to program with AI, big data and the cloud

Introduction to Python for Computer Science and Data Science: Learn to program using AI, big data and the cloud

$ 86.65 $ 69.99

In stock

7 new
From $69.99 with 7 used $72.34 free shipping

Buy now
How to install Python 2 with Virtualenv on Ubuntu 20.04Amazon.com

As of 12:07 PM, August 10, 2020


Python first: a friendly guide

Python first: a friendly guide

$59.99 $38.95

In stock

31 new
16 used $23.50 free shipping from $34.95

Buy now
How to install Python 2 with Virtualenv on Ubuntu 20.04Amazon.com

As of 12:07 PM, August 10, 2020


Fluent Python: clear, concise and effective programming

Fluent Python: clear, concise and effective programming

$59.99 $53.88

In stock

24 new
Starting from $ 46.86 18 used $ 27.97 free shipping

Buy now
How to install Python 2 with Virtualenv on Ubuntu 20.04Amazon.com

As of 12:07 PM, August 10, 2020

You can download this article in PDF format via the link below to support us.

Download the guide in PDF format

turn off
To

To
To

Sidebar