How to pip install a package Globally instead of Locally

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
3 min

banner

# Table of Contents

  1. Pip install a package globally instead of locally
  2. Pip install a package globally instead of locally using umask

# Pip install a package globally instead of locally

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.

shell
sudo -H pip install requests sudo -H pip3 install requests

install package globally using pip

Make sure to replace requests with the name of the package you're trying to install.

The -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.

shell
sudo -H pip install requests --ignore-installed # ๐Ÿ‘‡๏ธ For Python v3 sudo -H pip3 install requests --ignore-installed

using ignore installed flag when installing globally

The --ignore-installed option ignores the installed packages and overwrites them.

# Windows installs globally unless virtual environment is active

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.

shell
deactivate

deactivate virtual environment

If you have a virtual environment active, 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.

shell
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.

shell
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.

# Pip install a package globally instead of locally using umask

This is a two-step process:

  1. Use the umask 022 command to make the files of the package accessible to all users.
  2. Use the sudo pip install package-name command to install the package globally.
shell
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.

shell
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.

This is necessary because if you install a package with 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.

shell
sudo su umask 022 pip install requests pip3 install requests

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev