Last updated: Apr 9, 2024
Reading timeยท3 min
Use the -H
flag to pip install a package globally instead of locally, e.g.
sudo -H pip install package-name
.
When the -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
install.
-H
flag makes it so pip
installs 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
--ignore-installed
option.
sudo -H pip install requests --ignore-installed # ๐๏ธ For Python v3 sudo -H pip3 install requests --ignore-installed
--ignore-installed
option 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.
deactivate
pip
installs 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 umask 022
command.
This is a two-step process:
umask 022
command to make the files of the package accessible to
all users.sudo pip install package-name
command 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
install.
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
The 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.The umask 022
command makes it so the default directory permissions are 755
and the default file permissions are 644.
Permissions of 755
mean that everyone has read
and execute
access and only
the owner of the directory has write
access.
Permissions of 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
root user.
sudo su umask 022 pip install requests pip3 install requests
You can learn more about the related topics by checking out the following tutorials:
--no-cache-dir
option