pythonpython-3.xubuntupipubuntu-18.04

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


I'm trying to install pip for Python 3.8 on an Ubuntu 18.04 LTS.

I know this has been asked way too many times. But those questions do not concern keeping Ubuntu's defaults specifically. And the answers on those questions either don't work or go on to suggest something so drastic that it would break the system - e.g. change default python3 version from 3.6 to 3.8, which you shouldn't!

So far, I've been able to install python3.8 successfully using the PPA - ppa:deadsnakes/ppa:

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.8

I changed python command from python2 to python3.8 using update-alternatives:

update-alternatives --remove python /usr/bin/python2
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 10

Now, I get Python 3.8 when I run python --version:

Python 3.8.5

The problem is, I still can't install pip for Python 3.8.
If I try to install python3-pip, it installs pip for Python 3.6 since python3 still points to python3.6.9, and I intend to keep it that way.
Try installing python-pip, and it will install pip for Python 2.7.
Also there's no such package as python3.8-pip, so I can't install it like:

sudo apt install python3.8-pip

Output:

E: Unable to locate package python3.8-pip
E: Couldn't find any package by glob 'python3.8-pip'
E: Couldn't find any package by regex 'python3.8-pip'

What can I do to install pip for Python 3.8 on an Ubuntu 18.04?


Solution

  • 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!