When installing Python packages using pip
, you might encounter the following message in the console:
Defaulting to user installation because normal site-packages
is not writeable
This message appears when pip
fails to install the package in the system-wide directory.
This is not an error message as pip
can still install the package, but you might have problems like unable to access the package from your terminal as follows:
$ pip install django
Defaulting to user installation because normal site-packages
is not writeable
...
Successfully installed django-4.1.7
$ django-admin
zsh: command not found: django-admin
This tutorial explains why this message occurs and how you can make it disappear in practice.
Why this message occurs
By default, pip
tries to install your package in the system-wide directory named site-packages
.
When you lack permission to write to system-wide site-packages
, then pip
will install it in the user-wide site-packages
instead.
You can verify this by running python -m site
command from the terminal. There should be multiple site-packages
directory as shown below. The screenshot is from a Macbook:
The one under /Applications/Xcode.app
is the system-wide directory that requires root permission to write.
The USER_SITE
one is where pip
installs packages when the “Defaulting to user installation” message appears.
How to solve for Windows users
If you’re using Windows, then this message likely occurs because you installed Python under C:\Program Files
which requires administrator permission to modify.
To remove the message, you need to add the --user
flag when installing packages:
pip install --user <package name>
If you want to permanently fix this, you need to uninstall Python from your computer and install it again using the default settings.
If you need to customize Python installation, then take note of the default installation path and use it when you customize the install location:
By using the location provided in Python default installation, you will have Python installed in a folder that’s writeable without admin permission.
Also, don’t forget to check the options ‘Use admin privileges’ and ‘Add python.exe to PATH’.
Try running pip install
command again and you won’t receive the message this time.
How to solve for Linux users
If you’re using Linux, then you will get this message because you installed python using sudo
as in:
sudo apt-get install python3
In Linux-based OS, the system-wide packages directory is also known as dist-packages
, and you need root permission to access this directory.
Again, you can verify this using the python -m site
command:
When pip
fails to install to the dist-packages
directory, the message appears to notify you that it’s installing to the site-packages
directory instead.
To remove the message, you need to add the --user
flag when installing packages:
pip install --user <package name>
This command tells pip
to skip the dist-packages
directory and go to site-packages
directly.
If you want to install to dist-packages
, then you need to add sudo
to the install command:
sudo pip install <package name>
But you might get a warning message in the console as follows:
WARNING: Running pip as the 'root' user can result in broken
permissions and conflicting behaviour with the system package manager.
It is recommended to use a virtual environment instead:
https://pip.pypa.io/warnings/venv
If you want to permanently fix this message, then you need to install Python without using sudo
. This means you can’t use apt-get
either.
You can get Python in Linux using Homebrew, or you can download the source and compile it yourself.
Follow the steps below to compile Python manually. Note that you can get the full path to Python .tgz
file from here:
# Install SSL for Python
sudo apt-get install libssl-dev
# Create a new directory and move to it
mkdir python && cd python
# Download Python tar file
wget https://www.python.org/ftp/python/3.9.9/Python-3.9.9.tgz
# Extract the file
tar xvfz Python-3.9.9.tgz
# Move to the extracted folder
cd Python-3.9.9
# Create a new directory where Python will be installed
# This is usually your user home directory
mkdir ~/python/python39
# Set configuration path
./configure \
--prefix=$HOME/python/python39 \
--enable-optimizations \
# Run make
make
make install
# Move to home directory
cd ~
# Export the path to Python in your .bashrc file
echo "export PATH=$HOME/python/python39/bin/:$PATH" >> .bashrc
source .bashrc
The make
command might take a while to complete. Once done, you should now have python3
and pip3
available from your user directory.
Use the which
command to verify the installation:
$ which python3 pip3
/home/nsebhastian/python/python39/bin//python3
/home/nsebhastian/python/python39/bin//pip3
Alright. Now you can install packages without using sudo
command:
Because you installed Python and pip without using sudo
, the default installation will use the site-packages
directory.
Keep in mind that installing Python from source like this enables Python only for the current user. You need to repeat the process for other users.
How to solve for Mac users
If you’re using a Mac, this message appears when you use Python and pip that are provided by the system.
It’s recommended to install your own Python version to avoid permission problems and future changes from the macOS itself.
You can install Python using Homebrew as follows:
brew install python
Once installed, you should open a new Terminal window for the changes to take affect.
You can see the active python3
and pip3
versions using the which
command as follows:
$ which python3 pip3
/opt/homebrew/bin/python3
/opt/homebrew/bin/pip3
Once both python3
and pip3
are using the Homebrew-sourced version, you’re good to go.
Install any packages using pip install
and you won’t receive the error message.
Conclusion
Now you’ve learned how to solve the “Defaulting to user installation” message that occurs when installing Python packages.
I hope this tutorial is helpful. Until next time! 👋