Install and run python2 and python3 at the same time in one machine

April 4, 2021

Table of Content

Try running the provided script first

Before starting manually, please try running the install.sh script, if it gives an error, you can always go step-by-step manually.

chmod +x install.sh
./install.sh

Check for installed python

  1. First and foremost, please check if you have python3.8, python3 or python2 (python2.7) installed on your machine. Normally you should only have python3.8 installed on ubuntu by default.

  2. Type python3 --version to check the version of python installed on your computer.
    It should give you :

Python 3.8.5
  1. And if you have fresh Ubuntu that has just been installed (or relatively fresh), the command
    python --version, python2 --version, python2.7 --version should not print out anything.

  2. Assuming you are set until now, you can start doing the procedures as follows.

Install Python 3 and pip3

First, as you already have the Python 3.8.5, you can start installing pip3 by the below command:

sudo apt install python3-pip

as mentioned, it will install pip3 for python3 (obviously), you can also check if it is installed by typing

pip3 --version

This will print something similar to
pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)

NOTE: The pip version at the beginning may be different from that of what mentioned in the above example. The main thing is that it should print python3.* (any python version after python3) in parentheses.
Now that we have python3.8 and pip3 set, we should be able to move onto the next steps.

Install Python 2 and pip2

You can start installing Python 2 by typing

sudo apt install python2

It will install the scripts python2, python2.7 in /usr/bin/ making them accessible through everywhere.
Check the installation by typing python2 --version which should print

Python 2.7.*

Now you should download and install pip2 for Python 2 through online script

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

This will download and save the installer script to get-pip.py file.
Run the script to finally install pip2 pip2

python2 get-pip.py

After the installation is done, there is one last step you should do in order to use pip2.

As the last step, you should add the directory that the script pip2 is installed in, which will be
/home/<username>/.local/bin (replace <username> with yours).

Just open your .bashrc file with sudo nano ~/.bashrc, and add this line to the end of the file.

export PATH="/home/<username>/.local/bin:$PATH"

It simply concatenates the $PATH variable with the new directory string in order to establish an even access to the pip2 script from anywhere.

Check if all are set

You can check by the below commands if you have python3, python2, pip3 and pip2 all at the same time and working!

python3 --version
python2 --version
pip3 --version
pip2 --version

These 4 commands should output you the corresponding versions for the mentioned scripts.