Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 976
Qumber Avatar asked Aug 01 '20 16:08

Qumber


People also ask

Is pip installed by default on Ubuntu?

Pip is a package management system that simplifies installation and management of software packages written in Python such as those found in the Python Package Index (PyPI). Pip is not installed by default on Ubuntu 18.04, but the installation is pretty straightforward.

Does Python install pip by default?

Key terms. pip is the preferred installer program. Starting with Python 3.4, it is included by default with the Python binary installers.

How do I install pip on a specific version?

How do I Install a Specific Version of a Python Package? To install a specific version of a Python package you can use pip: pip install YourPackage==YourVersion . For example, if you want to install an older version of Pandas you can do as follows: pip install pandas==1.1. 3 .

Does Python 3.8 include pip?

The current version of pip works on: Windows, Linux and MacOS. CPython 3.7, 3.8, 3.9, 3.10 and latest PyPy3.


3 Answers

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!

like image 94
Qumber Avatar answered Oct 11 '22 20:10

Qumber


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

like image 8
Vaibhav Panmand Avatar answered Oct 11 '22 19:10

Vaibhav Panmand


# 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
like image 3
mirekphd Avatar answered Oct 11 '22 18:10

mirekphd