-H flag to pip install a package globally instead of locally, e.g.
sudo -H pip install package-name.
-H flag is set, the
pip install command installs the package in the
system's home directory.
sudo -H pip install requests sudo -H pip3 install requests
Make sure to replace
requests with the name of the package you're trying to
-Hflag makes it so
pipinstalls the package in the root user's home directory (globally).
If you run
sudo pip install <package-name> without the
-H flag, the root
user writes to the current user's home directory which can cause issues because
the package cannot be used without running as root.
If you get a message that the package is already installed, use the
sudo -H pip install requests --ignore-installed # 👇️ for Python v3 sudo -H pip3 install requests --ignore-installed
--ignore-installedoption ignores the installed packages and overwrites them.
On Windows, when you do
pip install <package-name>, the package is installed
globally as long as you don't have a virtual environment active.
You can use the
deactivate command if you need to exit a virtual environment.
pipinstalls packages scoped to the virtual environment.
If you get a message that the package is already installed, but you need to overwrite the existing package, use one of the following options.
pip install requests --upgrade pip install requests --ignore-installed pip install requests --force-reinstall # 👇️ for Python v3 pip3 install requests --upgrade pip3 install requests --ignore-installed pip3 install requests --force-reinstall
The --force-reinstall option forces pip to reinstall the package.
You can also uninstall the package and then install it.
pip uninstall requests pip3 uninstall requests pip install requests pip3 install requests
An alternative approach to install a package globally is to use the
This is a two-step process:
umask 022command to make the files of the package accessible to all users.
sudo pip install package-namecommand to install the package globally.
umask 022 sudo pip install requests # 👇️ for Python 3 sudo pip3 install requests
Make sure to replace
requests with the name of the module you're trying to
If you already installed the module using
sudo, uninstall it first.
sudo pip uninstall requests sudo pip3 uninstall requests umask 022 sudo pip install requests sudo pip3 install requests
umask command is used to assign the default file permissions for newly
created files and folders.
sudo, it will likely be readable only by root.
umask 022 command makes it so the default directory permissions are 755
and the default file permissions are 644.
755 mean that everyone has
execute access and only
the owner of the directory has
644 mean that everyone has read access and the owner has read
and write access.
Instead of prefixing every
pip command with
sudo, you can also switch to the
sudo su umask 022 pip install requests pip3 install requests
You can learn more about the related topics by checking out the following tutorials: