35

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. 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

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?

Qumber
  • 13,130
  • 4
  • 18
  • 33

7 Answers7

36

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!

Qumber
  • 13,130
  • 4
  • 18
  • 33
  • runnning `sudo apt remove python-pip` on your system seems awfully risky. are you sure this won't break anything? – Arne Feb 11 '21 at 08:08
  • @Arne Removing `python-pip` is not risky at all. In fact Ubuntu does not ship with it. If you have it on your machine, it's probably because you installed it. – Qumber Feb 11 '21 at 10:21
  • the slim ubuntu you get when you run `docker run -it ubuntu` also doesn't come with python, but if you delete it from your workstation's ubuntu you brick your machine. But if you're confident this won't happen with `python-pip`, delete away. – Arne Feb 12 '21 at 08:07
  • You can't delete `python`, obviously. Many core features depend on it in Ubuntu. Deleting `pip` however is different, since it's only a package manager. – Qumber Feb 12 '21 at 09:44
  • 1
    unless a system utility uses it in code to do stuff like getting info on installed python packages. anyway, I was just chiming in to cast some doubt and say that "obviously" is a dangerous word. If your system came with `python-pip` pre-installed, please don't delete it and find a different way to install a recent one. if you installed it yourself, you're probably (not obviously) going to be fine removing it. – Arne Feb 12 '21 at 09:56
  • 1
    Surely, if you know that `python-pip` came preinstalled or was installed by any other program as a dependency, it's better to leave it as is. – Qumber Feb 12 '21 at 10:00
  • In my case, `python3 -m pip list` works, but `pip3` gives error. Should I accept it and use an alias or try to fix the issue `NameError: name '__main__' is not defined` – Timo Sep 25 '21 at 09:45
  • 1
    @Timo, This could be an issue with your `pip3` binary. Try upgrading or reinstalling it - `python3 -m pip install pip --upgrade` – Qumber Sep 25 '21 at 09:58
  • Maybe your try is similar to [this](https://stackoverflow.com/questions/49836676/error-after-upgrading-pip-cannot-import-name-main#comment102444289_51462434) changing the `pip3` binary file. – Timo Sep 25 '21 at 10:34
  • @Timo, Sure. If that doesn't work, try reinstalling - `python -m pip uninstall pip` && `python -m pip uninstall pip`. – Qumber Sep 25 '21 at 13:02
13

As suggested in official documentation you can try with get-pip.py.

wget https://bootstrap.pypa.io/get-pip.py
python3.8 get-pip.py

This will install pip as pip3.8

Vaibhav Panmand
  • 349
  • 4
  • 10
6
# install py3.8 and dependencies for the pip3 bootstrap script
add-apt-repository -y ppa:deadsnakes/ppa && \
    apt install -y python3.8 python3.8-distutils

# download and run the pip3 bootstrap script
cd /tmp && wget https://bootstrap.pypa.io/get-pip.py && \
    python3.8 /tmp/get-pip.py

# use pip py3.8 module to install python packages
python3.8 -m pip install numpy pandas
mirekphd
  • 4,799
  • 3
  • 38
  • 59
5

Another solution would be to install the pip that is in apt. sudo apt install python3-pip. The version of the pip that it installs is for all versions of Python not only for version 3.6 once installed you just need to update the pip with the command python3.8 -m pip install pip and he will be install the latest version of pip for Python.

I would not advise you to remove Python2 because it is an important module for the system you should just create a permanent "alias" in .bashrc for Python3 I did like this alias python="python3.8.

Don Hatch
  • 5,041
  • 3
  • 31
  • 48
  • Setting up an alias would not help if you're trying to programmatically access `python3.8`/`pip 3. 8` using `command python`, if you're trying to install package from source using `python setup.py build`, if you want to execute python by a different user e.g. `sudo python` and in many other cases. – Qumber Mar 06 '21 at 04:02
2

Install python v3.8 as python

RUN apt update --fix-missing && \
apt install python3.8 -y && \
update-alternatives --install /usr/bin/python python /usr/bin/python3.8 10

Install pip for python 3.8

RUN apt install python3-pip -y && \
python -m pip install --upgrade pip
tada007
  • 31
  • 4
0

I did this a couple days ago and I struggled a lot with it but I finally got it working, so I wrote up what I did as a blog post.

In the end I think I may have done mostly the same things as the above answer, but if you got lost following it, maybe my screenshots etc will help.

Here's the tl;dr of the process I did:

  • Uninstall python3-pip & python-pip using apt
  • Remove the old pip files from /usr/local/bin
  • Reinstall python3-pip using apt
  • Add $HOME/.local/bin to your $PATH (also restart your shell to make sure you did this right)
R River
  • 43
  • 7
0

On ubuntu server

sudo apt install python -y

For more information check this blog here.

https://teckresolve.com/install-python-packages-using-pip/

buddemat
  • 4,552
  • 14
  • 29
  • 49
iemkamran
  • 11
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 14 '23 at 22:36