247

In my MacOS Mojave terminal I wanted to install a python package with pip. At the end it says:

You are using pip version 10.0.1, however version 18.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

So I wanted to update pip with the given command but I got an error:

Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: 
'/Library/Python/2.7/site-packages/pip-18.0-py2.7.egg/EGG-INFO/PKG-INFO'
Consider using the `--user` option or check the permissions.

I don't really understand what to do now. Also I realized it says Python 2.7 in the error message but I have and want to use only python 3.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Adler
  • 2,648
  • 2
  • 11
  • 16
  • 10
    Did you read the: "Consider using the `--user` option or check the permissions."? – Klaus D. Oct 23 '18 at 12:53
  • I just tried it you are right! I thought it would CHECK the permissions and not GIVE permissions. Thank you! – Adler Oct 23 '18 at 12:55
  • 5
    To install packages for python3 you need to use pip3 To upgrade pip just run as sudo if you run into permission errors. – BoboDarph Oct 23 '18 at 13:05
  • 1
    Possible duplicate of [pip install -r: OSError: \[Errno 13\] Permission denied](https://stackoverflow.com/questions/31512422/pip-install-r-oserror-errno-13-permission-denied) – phd Oct 23 '18 at 20:38
  • https://stackoverflow.com/search?q=%5Bpip%5D+Could+not+install+packages+due+to+an+EnvironmentError%3A+Errno+13+Permission+denied%3A+%2FLibrary%2FPython%2F2.7%2Fsite-packages%2F – phd Oct 23 '18 at 20:38

21 Answers21

358

If you want to use python3+ to install the packages you need to use pip3 install package_name

And to solve the errno 13 you have to add --user at the end

pip3 install package_name --user

EDIT:

For any project in Python it's highly recommended to work on a Virtual environment, which is a tool that helps to keep dependencies required by different projects separate by creating isolated Python virtual environments for them.

In order to create one with python3+ you have to use the following command:

virtualenv enviroment_name -p python3

And then you work on it just by activating it:

source enviroment_name/bin/activate

Once the virtual environment is activated, the name of your virtual environment will appear on the left side of the terminal. This will let you know that the virtual environment is currently active. Now you can install dependencies related to the project in this virtual environment by just using pip.

pip install package_name
Abdel
  • 69
  • 3
Gonzalo Garcia
  • 6,192
  • 2
  • 29
  • 32
  • 12
    I did this but I am still having a trouble. I installed flask but it still cannot be found within my venv. What can I do? – Leonard Sep 04 '19 at 08:15
  • 1
    Try virtualenv -p python3.8 enviroment_name Usage: virtualenv [OPTIONS] DEST_DIR – Tommy Gibbons Jul 24 '20 at 14:53
  • @Leonard I'm having this same issue, it's not installing in the venv. It's extremely frustrating, how did you fix it? – Amon Jun 09 '21 at 19:45
  • @GonzaloGarcia nope just regular python. I did not suspect such a simple task would cause such a significant issue – Amon Jun 09 '21 at 20:21
  • 1
    @Amon hi there, i asked this when i was a total noob and had a messed up python environment, when i didnt know i should not use anaconda and pip together. I basically reset all python environment, did clean new fresh install and havent faced much issue ever since. – Leonard Jul 04 '21 at 10:58
  • I was having some of the same issues. What worked for me was to first create a virtualenv, activate the venv, then upgrade pip, then install whatever you need without interfering with the system settings. – Timothy Lombard Aug 19 '21 at 03:55
  • @TimothyLombard Why not running the upgrade on the "system python" before creating the venv? The things that need to be updated have to be updated in any venv, so why not doing that already in the system install? I thought the venv is only put on top of the system install unless you tell it to have a chosen python version using `-p python3.10` when your system install is Python `3.8`? – questionto42 Jul 21 '22 at 18:49
43

I changed the rights of the venv that I was working in since the permissions were missing in the virtual environment subfolders.

sudo chmod -R a+rwx testenv

Then I could install an automatically recommended package from within codium.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
questionto42
  • 7,175
  • 4
  • 57
  • 90
37

Regarding the permissions command, try using sudo in front of your terminal command:

sudo pip install --upgrade pip

Sudo allows you to run the command with the privileges of the superuser and will install the package for the global, system-wide Python installation. Ideally, you should create a virtual environment for the project you are working on. Have a look at this

Regarding the python Try running pip as an executable like this:

python3.6 -m pip install <package>
Dominique Paul
  • 1,623
  • 2
  • 18
  • 31
  • 5
    I'm not completely sure, but running this kind of commands with root permissions usually is not the best idea: once you use sudo the user will change from "you" to "root", and that could lead to unwanted results. For example, libraries could be installed in /home/root instead of /home/yourusername. Also, files will be created in association with the root user/group. Sudo is needed only when the command should save files in system folders like /usr/local/bin ! ;) – funder7 Jan 06 '21 at 19:35
  • 2
    Yes, that's true. I've added a comment on that – Dominique Paul Jan 07 '21 at 08:33
13

To see if it's actually a problem with permissions run the following to install a package named xxx.

pip install --user xxx

for eg: to install package bcrypt run,

pip install --user bcrypt
Manoj Selvin
  • 2,247
  • 24
  • 20
  • 2
    This solved my life, but I had to include `-Iv` to overwrite what has been installed, `pip install -Iv --user xxx==x.y.z`, eg: `pip install -Iv --user pandas==1.3.5` – NoahVerner Mar 11 '22 at 16:31
11

I was making the same mistakes then I realized that I have created my virtual environment as root user. It was write protected, so please check whether your virtual environment is write protected. make a new venv and try again

  • Thats right check this tutorial and follow exactly (without changes) it works : https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-ubuntu-16-04 – DragonFire Oct 17 '22 at 06:35
10

The answer is in the error message. In the past you or a process did a sudo pip and that caused some of the directories under /Library/Python/2.7/site-packages/... to have permissions higher than current user.

Then you did a pip install whatever which modifies a directory you don't have write access to.

So to fix it, visit the /Library/Python/2.7/site-packages/... find the directory with the root or elevated permissions and either rm -rf yourpackages then reinstall the packages with your user, or just force ownership there to the user to whom ought to have access using chown -R ... or chmod -R ...

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
  • 2
    simple and working. use $whoami to know which user you are currently then $sudo chown username /Library/Python/2.7/site-packages/ then pip install whatever... You might have to use chown recursively – Sunil Kumar Aug 17 '19 at 08:02
  • 1
    Classic python-unix permission fight: `pip install blah` in an unix privileged directory assimilates to the higher, and unix puts it out of reach, then pip comes around for a second pass and says: "I can't change these directories or files". Here the python newbie gets to google search: "linux permission system". – Eric Leschinski May 19 '22 at 20:59
  • Just came to say that this was the exact answer I needed. I had some cruft leftover from getting Python to run on Apple Silicon when it was first released and it's pained me all the way until now. Cheers. – Alo Aug 22 '23 at 10:28
7

I had the same problem while installing numpy with pip install numpy.

Then I tried

sudo -H pip3 install --upgrade pip

sudo -H pip3 install numpy

It worked well for me.

Explanation : The -H (HOME) option with sudo sets the HOME environment variable to the home directory of the target user (root by default). By default, sudo does not modify HOME.

Deepam Gupta
  • 2,374
  • 1
  • 30
  • 33
5

I got the same error when I was trying to install a package (flask-classful).
I made the mistake of installing anaconda as root. I changed the ownership of the installed anaconda folder and I could install the package successfully.

Use the command chown with option -R to recursively change ownership of the installed anaconda folder like so:

chown -R owner:group /path/to/anaconda

Here owner is your username and group is the group name.

kRazzy R
  • 1,561
  • 1
  • 16
  • 44
sherminator35
  • 189
  • 1
  • 10
4

This worked for me:

 python3 -m venv env
 source ./env/bin/activate
 python -m pip install package

(From Github: https://github.com/googlesamples/assistant-sdk-python/issues/236 )

kk.
  • 3,747
  • 12
  • 36
  • 67
Lisa B.
  • 235
  • 1
  • 11
4

I was running python3 -m pip install xxx

ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/Library/Python/3.8'
Consider using the `--user` option or check the permissions.

/Library/Python/3.8 Indicates that python3 I'm using is the system wide python, and hence permission issues. Solutions involving --user flags and virtual envs are all solving this issue.

For me, using brew the most convenient:

brew install python@3.8
brew link python@3.8
which python3

After which python3 -m pip install xxx succeed without problems. Note that sudo should not be used.

The principle is the same: you are starting a new environment that is less privileged. This implies all the packages you need from the old environment need to be installed again in this new environment.

I guess use the --user flag if you don't want to reinstall everything.

Sida Zhou
  • 3,529
  • 2
  • 33
  • 48
2

For MacOs & Unix

Just by adding sudo to command will work, as it would run it as a superuser.

sudo pip install --upgrade pip

It is advised that you should not directly do it though - please see this post

Rohit Kumar
  • 983
  • 9
  • 11
  • 1
    This isn't helpful to Windows users at all, and this error is more common with Windows users due to Windows operating permissions. Also, commands should be enclosed in code blocks, when they're not it makes it hard to read – logos_164 Nov 02 '19 at 19:10
1

try this command line below for MacOS to check user's permission.

$ sudo python -m pip install --user --upgrade pip
Gonzalo Garcia
  • 6,192
  • 2
  • 29
  • 32
An Nguyen
  • 163
  • 2
  • 9
1

I have anaconda installed for Python 3. I also have Python2 in my mac.

python --version

gives me

Python 3.7.3

python2.7 --version

gives me

Python 2.7.10

I wanted to install pyspark package in python2, given that it was already installed in python3.

python2.7 -m pip install pyspark

gives me an error

Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/pyspark' Consider using the --user option or check the permissions.

The below command solved it. Thank god I didn't have to do any config changes.

python2.7 -m pip install pyspark --user

Collecting pyspark Requirement already satisfied: py4j==0.10.7 in /Library/Python/2.7/site-packages (from pyspark) (0.10.7) Installing collected packages: pyspark Successfully installed pyspark-2.4.4 You are using pip version 18.1, however version 19.3.1 is available. You should consider upgrading via the 'pip install --upgrade pip' command.

karthi190
  • 61
  • 4
1

I already tried all suggestion posted in here, yet I'm still getting the errno 13,

I'm using Windows and my python version is 3.7.3

After 5 hours of trying to solve it, this step worked for me:

I try to open the command prompt by run as administrator

nrmzmh
  • 453
  • 5
  • 13
1

Had similar issue, got solved with

sudo chown -R myuser /Users/myuser/Library/Python/
Jose Paez
  • 747
  • 1
  • 11
  • 18
0

I also had the same problem, I tried many different command lines, this one worked for me:

Try:

    conda install py-xgboost

That's what I got:

Collecting package metadata: done
Solving environment: done

## Package Plan ##

  environment location: /home/simplonco/anaconda3

  added / updated specs:
    - py-xgboost


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    _py-xgboost-mutex-2.0      |            cpu_0           9 KB
    ca-certificates-2019.1.23  |                0         126 KB
    certifi-2018.11.29         |           py37_0         146 KB
    conda-4.6.2                |           py37_0         1.7 MB
    libxgboost-0.80            |       he6710b0_0         3.7 MB
    mkl-2019.1                 |              144       204.6 MB
    mkl_fft-1.0.10             |   py37ha843d7b_0         169 KB
    mkl_random-1.0.2           |   py37hd81dba3_0         405 KB
    numpy-1.15.4               |   py37h7e9f1db_0          47 KB
    numpy-base-1.15.4          |   py37hde5b4d6_0         4.2 MB
    py-xgboost-0.80            |   py37he6710b0_0         1.7 MB
    scikit-learn-0.20.2        |   py37hd81dba3_0         5.7 MB
    scipy-1.2.0                |   py37h7c811a0_0        17.7 MB
    ------------------------------------------------------------
                                           Total:       240.0 MB

The following NEW packages will be INSTALLED:

  _py-xgboost-mutex  pkgs/main/linux-64::_py-xgboost-mutex-2.0-cpu_0
  libxgboost         pkgs/main/linux-64::libxgboost-0.80-he6710b0_0
  py-xgboost         pkgs/main/linux-64::py-xgboost-0.80-py37he6710b0_0

The following packages will be UPDATED:

  ca-certificates     anaconda::ca-certificates-2018.12.5-0 --> pkgs/main::ca-certificates-2019.1.23-0
  mkl                                            2019.0-118 --> 2019.1-144
  mkl_fft                              1.0.4-py37h4414c95_1 --> 1.0.10-py37ha843d7b_0
  mkl_random                           1.0.1-py37h4414c95_1 --> 1.0.2-py37hd81dba3_0
  numpy                               1.15.1-py37h1d66e8a_0 --> 1.15.4-py37h7e9f1db_0
  numpy-base                          1.15.1-py37h81de0dd_0 --> 1.15.4-py37hde5b4d6_0
  scikit-learn                        0.19.2-py37h4989274_0 --> 0.20.2-py37hd81dba3_0
  scipy                                1.1.0-py37hfa4b5c9_1 --> 1.2.0-py37h7c811a0_0

The following packages will be SUPERSEDED by a higher-priority channel:

  certifi                                          anaconda --> pkgs/main
  conda                                            anaconda --> pkgs/main
  openssl                anaconda::openssl-1.1.1-h7b6447c_0 --> pkgs/main::openssl-1.1.1a-h7b6447c_0


Proceed ([y]/n)? y


Downloading and Extracting Packages
libxgboost-0.80      | 3.7 MB    | ##################################### | 100% 
mkl_random-1.0.2     | 405 KB    | ##################################### | 100% 
certifi-2018.11.29   | 146 KB    | ##################################### | 100% 
ca-certificates-2019 | 126 KB    | ##################################### | 100% 
conda-4.6.2          | 1.7 MB    | ##################################### | 100% 
mkl-2019.1           | 204.6 MB  | ##################################### | 100% 
mkl_fft-1.0.10       | 169 KB    | ##################################### | 100% 
numpy-1.15.4         | 47 KB     | ##################################### | 100% 
scipy-1.2.0          | 17.7 MB   | ##################################### | 100% 
scikit-learn-0.20.2  | 5.7 MB    | ##################################### | 100% 
py-xgboost-0.80      | 1.7 MB    | ##################################### | 100% 
_py-xgboost-mutex-2. | 9 KB      | ##################################### | 100% 
numpy-base-1.15.4    | 4.2 MB    | ##################################### | 100% 
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
NelsonGon
  • 13,015
  • 7
  • 27
  • 57
Psybelo
  • 1
  • 2
0

I had similar trouble in a venv on a mounted NTFS partition on linux with all the right permissions. Making sure pip ran with --ignore-installed solved it, i.e.:

python -m pip install --upgrade --ignore-installed

elig
  • 2,635
  • 3
  • 14
  • 24
0

On Mac, there is no 3.7 directory or the directory 3.7 is owned by root. So, I removed that directory, create a new directory by current user, and move it there. Then installation finishes without error.

sudo rm -rf /Library/Python/3.7
mkdir 3.7
sudo mv 3.7 /Library/Python
ll /Library/Python/
pip3 install numpy
zhongxiao37
  • 977
  • 8
  • 17
0

This also happens to me when I try to install the opencv-python package:

installation attempt

I can fix it with command line

python3 -m pip install {name of package} --user

When I try to install the said package, the command becomes:

python3 -m pip install opencv-python --user

Resulting in this:

result

peki
  • 669
  • 7
  • 24
0

Sometimes once you run into this error you might have to remove and recreate the virtual environment as pip will not work. The reason is that while you were trying to upgrade pip, the old version is removed with unsuccessful installation of the new one due to access permissions. Then @Gonzalo Garcia's answer is applicable.

Abdel
  • 69
  • 3
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/34459824) – dpapadopoulos May 31 '23 at 06:59
-13

just sudo pip install packagename

XHFKA
  • 7
  • 1
  • giving a command that elevates to superuser without any explanation about the consequences is really bad advice. Especially so if you don't understand it yourself. – emilBeBri Apr 24 '22 at 10:07