Upgrade pip to newest version on Ubuntu 16.04

12,024

Solution 1

Here's my number one tip: Do not use sudo with pip. You don't need it.

Instead, use a user-level Python distribution installed via pyenv. That way you don't risk yourself messing around with system-level frameworks (and hence creating a problem removing dependencies etc.).

Install Pyenv

All you have to do is:

  • Run the pyenv installer
  • Follow the instructions
  • Install the Python versions you need
  • Choose which Python version you want to use for a given directory, or globally

For example, to install 3.7, check which versions are available:

pyenv install -l | grep 3.7

Then run:

pyenv install 3.7.4

Switch to the new version

Now, you can choose your Python version:

pyenv global 3.7.4

This switches your python to point to 3.7.4. If you want the “old” system python, run:

pyenv global system

To check which Python versions are available, run pyenv versions.

Upgrade pip

Once you've switched to a Pyenv version, you can run pip without sudo, and install/upgrade packages easily — without interfering with your system Python:

pyenv global 3.7.4
pip install --upgrade pip
pip install numpy

Solution 2

Do not use apt-get to update pip. The newest version of pip available through apt-get is old.

Quick solution:

As @kenorb is saying in the comments, you can upgrade pip using pip with sudo like this:

sudo pip install --upgrade pip

Proper solution:

Use pyenv - see the answer by @slhck

Share:
12,024

Related videos on Youtube

Chanaka Karunarathna
Author by

Chanaka Karunarathna

Updated on September 18, 2022

Comments

  • Chanaka Karunarathna
    Chanaka Karunarathna over 1 year

    I'm trying to upgrade pip into the newest version (old version 9.0.1 and newest version 19.3.1. using the following commands.

    pip install --upgrade pip
    
    sudo apt-get upgrade pip
    

    But it won't upgrade it with the following error:

    **$ pip install --upgrade pip**
    
    Collecting pip
      Using cached https://files.pythonhosted.org/packages/00/b6/9cfa56b4081ad13874b0c6f96af8ce16cfbc1cb06bedf8e9164ce5551ec1/pip-19.3.1-py2.py3-none-any.whl
    Installing collected packages: pip
      Found existing installation: pip 9.0.1
        Uninstalling pip-9.0.1:
          Successfully uninstalled pip-9.0.1
      Rolling back uninstall of pip
    
    Exception:
    Traceback (most recent call last):
    
      File "/home/larz/.local/lib/python2.7/site-packages/pip/basecommand.py", line 215, in main
        status = self.run(options, args)
    
      File "/home/larz/.local/lib/python2.7/site-packages/pip/commands/install.py", line 342, in run
        prefix=options.prefix_path,
    
      File "/home/larz/.local/lib/python2.7/site-packages/pip/req/req_set.py", line 784, in install
        **kwargs
    
      File "/home/larz/.local/lib/python2.7/site-packages/pip/req/req_install.py", line 851, in install
        self.move_wheel_files(self.source_dir, root=root, prefix=prefix)
    
      File "/home/larz/.local/lib/python2.7/site-packages/pip/req/req_install.py", line 1064, in move_wheel_files
        isolated=self.isolated,
    
      File "/home/larz/.local/lib/python2.7/site-packages/pip/wheel.py", line 345, in move_wheel_files
        clobber(source, lib_dir, True)
    
      File "/home/larz/.local/lib/python2.7/site-packages/pip/wheel.py", line 316, in clobber
        ensure_dir(destdir)
    
      File "/home/larz/.local/lib/python2.7/site-packages/pip/utils/__init__.py", line 83, in ensure_dir
        os.makedirs(path)
    
      File "/usr/lib/python2.7/os.py", line 157, in makedirs
        mkdir(name, mode)
    
    OSError: [Errno 13] Permission denied: '/usr/local/lib/python2.7/dist-packages/pip'
    

    I need to install keras. While I was installing it asked me to upgrade pip. When I try to upgrade pip I'm getting above errors

    • CaldeiraG
      CaldeiraG over 4 years
      Can you please copy the error and paste it on your question? Images are unnecessary if the information is only text
    • Chanaka Karunarathna
      Chanaka Karunarathna over 4 years
      Sure: I have updated
    • kenorb
      kenorb over 4 years
      Since you're upgrading pip itself, run with sudo, e.g. sudo pip install --upgrade pip.
    • curusarn
      curusarn over 4 years
      Are you sure you want to use python2.7 instead of the latest python3.8? pip is used with python2.* and pip3 is used with python3.*.
  • Chanaka Karunarathna
    Chanaka Karunarathna over 4 years
    Yes thank you both of you. I solved it using above method. Thanks again kenorb and @curusarn. Yes I just found that pip3 used for python3. How can I set python3 as default. I tried to remove python2. But I end up with mess removing all dependencies etc..
  • curusarn
    curusarn over 4 years
    You need to install python3 separately: sudo apt-get install python3 python3-pip && sudo pip3 install --upgrade pip. Install any python3 package using sudo pip3 install package_name. To change the default python you can add this line to your ~/.bashrc: alias python=python3 Note that this only changes the default when you use it in the terminal. It's not a very good idea to change the system-wide default because some programs may only work with python2. Also, you still need to use sudo pip3 to install packages for python3.
  • slhck
    slhck over 4 years
    I prefer not teaching users to use/require sudo for managing Python under Linux. It just creates problems down the line when people try to install/uninstall packages and inadvertently removing required system dependencies.