This article will tell you how to install both Python2 (pip2) and Python3 (pip3) on Ubuntu.
1. Install Python2 (pip2) On Ubuntu Steps.
- Ubuntu 20.04 repositories do not contain Python2, so we should install Python 2 in Ubuntu first.
- Run the command sudo add-apt-repository universe to set up the universe repository.
$ sudo add-apt-repository universe 'universe' distribution component is already enabled for all sources.
- Run the command sudo apt update to update the apt packages.
$ sudo apt update Hit:1 http://cn.archive.ubuntu.com/ubuntu focal InRelease ...... Fetched 1,111 kB in 6s (184 kB/s) Reading package lists... Done Building dependency tree Reading state information... Done 203 packages can be upgraded. Run 'apt list --upgradable' to see them.
- Run the command sudo apt install python2 to install python2 on Ubuntu.
$ sudo apt install python2 Reading package lists... Done Building dependency trpythonee Reading state information... Done The following additional packages will be installed: libpython2-stdlib libpython2.7-minimal libpython2.7-stdlib python2-minimal python2.7 python2.7-minimal Suggested packages: python2-doc python-tk python2.7-doc binfmt-support The following NEW packages will be installed: libpython2-stdlib libpython2.7-minimal libpython2.7-stdlib python2 python2-minimal python2.7 python2.7-minimal 0 upgraded, 7 newly installed, 0 to remove and 168 not upgraded. Need to get 3,816 kB of archives. After this operation, 16.5 MB of additional disk space will be used. Do you want to continue? [Y/n] y Get:1 http://cn.archive.ubuntu.com/ubuntu focal-updates/universe amd64 libpython2.7-minimal amd64 2.7.18-1~20.04.1 [335 kB] ....
- After that, you can run the command python2 –version to check whether python2 has been installed successfully or not.
$ python2 --version Python 2.7.18
- You can read the article How To Install The Latest PIP Version And Ipykernel For Python 2.7 to learn how to install pip2 for Python 2.7.
- If you want to invoke the Python2 installed pip command, you can run the command python2 -m pip, this command will run the pip command as a python2 module.
- For example, run the command python2 -m pip list will list all the installed python2 packages, the command python2 -m pip -h will show the pip help content.
- We can add the pip executable file path to the system environment PATH variable’s value and then you can call it directly.
- First, run the command python2 -m pip –version to get the Python2 pip installed location.
$ python2 -m pip --version pip 20.3.4 from /home/jerry/.local/lib/python2.7/site-packages/pip (python 2.7)
- Open a terminal in Ubuntu, run the command cd ~ to go to the user home directory.
- Run the command $ sudo gedit .profile to open the text editor to edit the .profile file.
- Add the text line PATH=”$HOME/.local/lib/python2.7/site-packages/pip:$PATH” at the end of the .profile file.
- Click the Save button to save the changes.
- Run the command . ./.profile to activate the changes.
- Run the command env to print out all the system environment variables values including the PATH variable.
$ env ...... PATH=/home/jerry/.local/lib/python2.7/site-packages/pip:/home/jerry/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin ......
- Now you can run the pip command directly in the terminal.
$ pip --version pip 20.3.4 from /home/jerry/.local/lib/python2.7/site-packages/pip (python 2.7)
2. Install Python3 (pip3) On Ubuntu Steps.
- Run the command sudo apt update to update the apt packages.
- Run the command sudo apt install python3-pip to install Python 3 and pip.
$ sudo apt install python3-pip Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: libexpat1-dev libpython3-dev libpython3.8 libpython3.8-dev libpython3.8-minimal libpython3.8-stdlib python-pip-whl python3-dev python3-distutils python3-setuptools python3-wheel python3.8 python3.8-dev python3.8-minimal zlib1g-dev Suggested packages: python-setuptools-doc python3.8-venv python3.8-doc binfmt-support The following NEW packages will be installed: libexpat1-dev libpython3-dev libpython3.8-dev python-pip-whl python3-dev python3-distutils python3-pip python3-setuptools python3-wheel python3.8-dev zlib1g-dev The following packages will be upgraded: libpython3.8 libpython3.8-minimal libpython3.8-stdlib python3.8 python3.8-minimal 5 upgraded, 11 newly installed, 0 to remove and 163 not upgraded. Need to get 7,271 kB/13.6 MB of archives. After this operation, 28.4 MB of additional disk space will be used. Do you want to continue? [Y/n] Y Get:1 http://cn.archive.ubuntu.com/ubuntu focal/main amd64 libexpat1-dev amd64 2.2.9-1build1 [116 kB] Get:2 http://cn.archive.ubuntu.com/ubuntu focal-updates/main amd64 libpython3.8-dev amd64 3.8.10-0ubuntu1~20.04.2 [3,950 kB] Get:3 http://cn.archive.ubuntu.com/ubuntu focal/main amd64 libpython3-dev amd64 3.8.2-0ubuntu2 [7,236 B] Get:4 http://cn.archive.ubuntu.com/ubuntu focal-updates/universe amd64 python-pip-whl all 20.0.2-5ubuntu1.6 [1,805 kB] Get:5 http://cn.archive.ubuntu.com/ubuntu focal-updates/main amd64 zlib1g-dev amd64 1:1.2.11.dfsg-2ubuntu1.2 [155 kB] Get:6 http://cn.archive.ubuntu.com/ubuntu focal-updates/main amd64 python3.8-dev amd64 3.8.10-0ubuntu1~20.04.2 [510 kB] Get:7 http://cn.archive.ubuntu.com/ubuntu focal-updates/main amd64 python3-distutils all 3.8.10-0ubuntu1~20.04 [141 kB] Get:8 http://cn.archive.ubuntu.com/ubuntu focal/main amd64 python3-dev amd64 3.8.2-0ubuntu2 [1,212 B] Get:9 http://cn.archive.ubuntu.com/ubuntu focal/main amd64 python3-setuptools all 45.2.0-1 [330 kB] Get:10 http://cn.archive.ubuntu.com/ubuntu focal/universe amd64 python3-wheel all 0.34.2-1 [23.8 kB] Get:11 http://cn.archive.ubuntu.com/ubuntu focal-updates/universe amd64 python3-pip all 20.0.2-5ubuntu1.6 [231 kB] Fetched 7,271 kB in 31s (237 kB/s) (Reading database ... 186789 files and directories currently installed.) Preparing to unpack .../00-libpython3.8_3.8.10-0ubuntu1~20.04.2_amd64.deb ... Unpacking libpython3.8:amd64 (3.8.10-0ubuntu1~20.04.2) over (3.8.10-0ubuntu1~20.04) ... Preparing to unpack .../01-python3.8_3.8.10-0ubuntu1~20.04.2_amd64.deb ... Unpacking python3.8 (3.8.10-0ubuntu1~20.04.2) over (3.8.10-0ubuntu1~20.04) ... Preparing to unpack .../02-libpython3.8-stdlib_3.8.10-0ubuntu1~20.04.2_amd64.deb ... Unpacking libpython3.8-stdlib:amd64 (3.8.10-0ubuntu1~20.04.2) over (3.8.10-0ubuntu1~20.04) ... Preparing to unpack .../03-python3.8-minimal_3.8.10-0ubuntu1~20.04.2_amd64.deb ... Unpacking python3.8-minimal (3.8.10-0ubuntu1~20.04.2) over (3.8.10-0ubuntu1~20.04) ... Preparing to unpack .../04-libpython3.8-minimal_3.8.10-0ubuntu1~20.04.2_amd64.deb ... Unpacking libpython3.8-minimal:amd64 (3.8.10-0ubuntu1~20.04.2) over (3.8.10-0ubuntu1~20.04) ... Selecting previously unselected package libexpat1-dev:amd64. Preparing to unpack .../05-libexpat1-dev_2.2.9-1build1_amd64.deb ... Unpacking libexpat1-dev:amd64 (2.2.9-1build1) ... Selecting previously unselected package libpython3.8-dev:amd64. Preparing to unpack .../06-libpython3.8-dev_3.8.10-0ubuntu1~20.04.2_amd64.deb ... Unpacking libpython3.8-dev:amd64 (3.8.10-0ubuntu1~20.04.2) ... Selecting previously unselected package libpython3-dev:amd64. Preparing to unpack .../07-libpython3-dev_3.8.2-0ubuntu2_amd64.deb ... Unpacking libpython3-dev:amd64 (3.8.2-0ubuntu2) ... Selecting previously unselected package python-pip-whl. Preparing to unpack .../08-python-pip-whl_20.0.2-5ubuntu1.6_all.deb ... Unpacking python-pip-whl (20.0.2-5ubuntu1.6) ... Selecting previously unselected package zlib1g-dev:amd64. Preparing to unpack .../09-zlib1g-dev_1%3a1.2.11.dfsg-2ubuntu1.2_amd64.deb ... Unpacking zlib1g-dev:amd64 (1:1.2.11.dfsg-2ubuntu1.2) ... Selecting previously unselected package python3.8-dev. Preparing to unpack .../10-python3.8-dev_3.8.10-0ubuntu1~20.04.2_amd64.deb ... Unpacking python3.8-dev (3.8.10-0ubuntu1~20.04.2) ... Selecting previously unselected package python3-distutils. Preparing to unpack .../11-python3-distutils_3.8.10-0ubuntu1~20.04_all.deb ... Unpacking python3-distutils (3.8.10-0ubuntu1~20.04) ... Selecting previously unselected package python3-dev. Preparing to unpack .../12-python3-dev_3.8.2-0ubuntu2_amd64.deb ... Unpacking python3-dev (3.8.2-0ubuntu2) ... Selecting previously unselected package python3-setuptools. Preparing to unpack .../13-python3-setuptools_45.2.0-1_all.deb ... Unpacking python3-setuptools (45.2.0-1) ... Selecting previously unselected package python3-wheel. Preparing to unpack .../14-python3-wheel_0.34.2-1_all.deb ... Unpacking python3-wheel (0.34.2-1) ... Selecting previously unselected package python3-pip. Preparing to unpack .../15-python3-pip_20.0.2-5ubuntu1.6_all.deb ... Unpacking python3-pip (20.0.2-5ubuntu1.6) ... Setting up python3-distutils (3.8.10-0ubuntu1~20.04) ... Setting up libpython3.8-minimal:amd64 (3.8.10-0ubuntu1~20.04.2) ... Setting up python3-setuptools (45.2.0-1) ... Setting up python3-wheel (0.34.2-1) ... Setting up libexpat1-dev:amd64 (2.2.9-1build1) ... Setting up zlib1g-dev:amd64 (1:1.2.11.dfsg-2ubuntu1.2) ... Setting up python3.8-minimal (3.8.10-0ubuntu1~20.04.2) ... Setting up python-pip-whl (20.0.2-5ubuntu1.6) ... Setting up libpython3.8-stdlib:amd64 (3.8.10-0ubuntu1~20.04.2) ... Setting up python3.8 (3.8.10-0ubuntu1~20.04.2) ... Setting up libpython3.8:amd64 (3.8.10-0ubuntu1~20.04.2) ... Setting up python3-pip (20.0.2-5ubuntu1.6) ... Setting up libpython3.8-dev:amd64 (3.8.10-0ubuntu1~20.04.2) ... Setting up python3.8-dev (3.8.10-0ubuntu1~20.04.2) ... Setting up libpython3-dev:amd64 (3.8.2-0ubuntu2) ... Setting up python3-dev (3.8.2-0ubuntu2) ... Processing triggers for mime-support (3.64ubuntu1) ... Processing triggers for gnome-menus (3.36.0-1ubuntu1) ... Processing triggers for libc-bin (2.31-0ubuntu9.2) ... Processing triggers for man-db (2.9.1-1) ... Processing triggers for desktop-file-utils (0.24-1ubuntu3) ...
- After that, you can run the command python3 –version and pip3 –version to check the installation result. If you get the below output that means your installation is successful.
$ python3 --version Python 3.8.10 $ pip3 --version pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)
3. How To Upgrade PIP Versions.
- Run the below command to upgrade the pip installation for python3.
$ python3 -m pip install --upgrade pip ...... Installing collected packages: pip Successfully installed pip-21.3.1
- After you upgrade the pip successfully and restart the Ubuntu OS, you will find the system has created 3 files related to pip, they are pip, pip2, pip3, you can run the below command to get the 3 pip related files saved directory & version.
$ which pip /home/jerry/.local/bin/pip $ pip --version pip 21.3.1 from /home/jerry/.local/lib/python3.8/site-packages/pip (python 3.8) $ which pip2 /home/jerry/.local/bin/pip2 $ pip2 --version pip 20.3.4 from /home/jerry/.local/lib/python2.7/site-packages/pip (python 2.7) $ which pip3 /home/jerry/.local/bin/pip3 $ pip3 --version pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)