How to Install Pip in Ubuntu

How can I install pip for Python2.7 in Ubuntu 20.04

Pip for Python 2 is not included in the Ubuntu 20.04 repositories.

Try this guide which suggests to fetch a Python 2.7 compatible get_pip.py and use that to bootstrap pip.

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

How to install pip for Python 3.9 on Ubuntu 20.04

You can install pip for python 3.9 the following way:

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

It is important you use python3.9 instead of just python3, to ensure pip is installed for python 3.9.

If you see any permissions errors, you may need to use

python3.9 get-pip.py --user

If you get an error like No module named 'distutils.util' when you run python3.9 get-pip.py, and you are on a Debian-based Linux distribution, run

sudo apt install python3.9-distutils

and then rerun your get-pip.py command. If you are not on a Debian-based distribution, use the equivalent command for your distribution's package manager.

These instructions are based in part on the official installation instructions provided by the pip maintainers.


This portion of my answer is a bit out of the scope of the question, since the question is specifically for python 3.9. However, for anyone trying to install pip on python 3.6 or older, at the time of writing the file at https://bootstrap.pypa.io/get-pip.py only supports python 3.7 or newer.

The workaround is to instead download from https://bootstrap.pypa.io/pip/<python version>/get-pip.py instead. For example, if you want to install pip for python 3.6, then you can download from https://bootstrap.pypa.io/pip/3.6/get-pip.py, and then follow all of the steps above as usual.

Proper way to install pip on Ubuntu

I believe that you can install it on Ubuntu with

sudo apt-get install python-pip

or

sudo apt-get install python3-pip

for Python 3. Sure, it's an older version but its functionality is there.

How do I install pip for python 3.8 on Ubuntu without changing any defaults?

While we can use pip directly as a Python module (the recommended way):

python -m pip --version

This is how I installed it (so it can be called directly):

Firstly, make sure that command pip is available and it isn't being used by pip for Python 2.7

sudo apt remove python-pip

Now if you write pip in the Terminal, you'll get that nothing is installed there:

pip --version

Output:

Command 'pip' not found, but can be installed with:

sudo apt install python-pip

Install python3.8 and setup up correct version on python command using update-alternatives (as done in the question).

Make sure, you have python3-pip installed:

(This won't work without python3-pip. Although this will install pip 9.0.1 from python 3.6, we'll need it.)

sudo apt install python3-pip

This will install pip 9.0.1 as pip3:

pip3 --version

Output:

pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6)

Now, to install pip for Python 3.8, I used pip by calling it as a python module (ironic!):

python -m pip install pip

Output:

Collecting pip

  Downloading https://files.pythonhosted.org/packages/36/74/38c2410d688ac7b48afa07d413674afc1f903c1c1f854de51dc8eb2367a5/pip-20.2-py2.py3-none-any.whl (1.5MB)

  100% |████████████████████████████████| 1.5MB 288kB/s

Installing collected packages: pip

Successfully installed pip-20.2

It looks like, when I called pip (which was installed for Python 3.6, BTW) as a module of Python 3.8, and installed pip, it actually worked.

Now, make sure your ~/.local/bin directory is set in PATH environment variable:

Open ~/.bashrc using your favourite editor (if you're using zsh, replace .bashrc with .zshrc)

nano ~/.bashrc

And paste the following at the end of the file

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/.local/bin" ] ; then
PATH="$HOME/.local/bin:$PATH"
fi

Finally, source your .bashrc (or restart the Terminal window):

source ~/.bashrc

Now if you try running pip directly it'll give you the correct version:

pip --version

Output:

pip 20.2 from /home/qumber/.local/lib/python3.8/site-packages/pip (python 3.8)

Sweet!

How to Install pip for python 3.7 on Ubuntu 18?

In general, don't do this:

pip install package

because, as you have correctly noticed, it's not clear what Python version you're installing package for.

Instead, if you want to install package for Python 3.7, do this:

python3.7 -m pip install package

Replace package with the name of whatever you're trying to install.

Took me a surprisingly long time to figure it out, too. The docs about it are here.

Your other option is to set up a virtual environment. Once your virtual environment is active, executable names like python and pip will point to the correct ones.

How to install pip with Python 3?

edit: Manual installation and use of setuptools is not the standard process anymore.

If you're running Python 2.7.9+ or Python 3.4+

Congrats, you should already have pip installed. If you do not, read onward.

If you're running a Unix-like System

You can usually install the package for pip through your package manager if your version of Python is older than 2.7.9 or 3.4, or if your system did not include it for whatever reason.

Instructions for some of the more common distros follow.

Installing on Debian (Wheezy and newer) and Ubuntu (Trusty Tahr and newer) for Python 2.x

Run the following command from a terminal:

sudo apt-get install python-pip 

Installing on Debian (Wheezy and newer) and Ubuntu (Trusty Tahr and newer) for Python 3.x

Run the following command from a terminal:

sudo apt-get install python3-pip
Note:

On a fresh Debian/Ubuntu install, the package may not be found until you do:

sudo apt-get update

Installing pip on CentOS 7 for Python 2.x

On CentOS 7, you have to install setup tools first, and then use that to install pip, as there is no direct package for it.

sudo yum install python-setuptools
sudo easy_install pip

Installing pip on CentOS 7 for Python 3.x

Assuming you installed Python 3.4 from EPEL, you can install Python 3's setup tools and use it to install pip.

# First command requires you to have enabled EPEL for CentOS7
sudo yum install python34-setuptools
sudo easy_install pip

If your Unix/Linux distro doesn't have it in package repos

Install using the manual way detailed below.

The manual way

If you want to do it the manual way, the now-recommended method is to install using the get-pip.py script from pip's installation instructions.

Install pip

To install pip, securely download get-pip.py

Then run the following (which may require administrator access):

python get-pip.py 

If setuptools is not already installed, get-pip.py will install setuptools for you.



Related Topics



Leave a reply



Submit