15%

Save 15% on All Hosting Services

Test your skills and get Discount on any hosting plan

Use code:

Skills
Get Started
30.10.2024

How to Install a Library in Python: A Complete Step-by-Step Guide

Python is one of the most versatile and widely adopted programming languages in the world, and much of its power comes from an enormous ecosystem of third-party libraries. These libraries provide ready-made, battle-tested code that lets developers build web applications, automate workflows, perform data analysis, train machine learning models, and much more β€” without reinventing the wheel every time.

Whether you are running Python scripts on your local machine, on a VPS Hosting environment, or on a dedicated server, knowing how to install, manage, and maintain Python libraries is a fundamental skill every developer and sysadmin needs to master.

This guide walks you through every step of the process β€” from verifying your Python installation to using virtual environments and managing dependencies at scale.

Prerequisites

Before diving in, make sure you have:

  • Access to a terminal (Linux/macOS) or Command Prompt / PowerShell (Windows)
  • Sufficient permissions to install software (or a virtual environment, covered below)
  • An active internet connection to download packages from PyPI

Step 1: Verify That Python Is Installed

Before installing any libraries, confirm that Python is available on your system.

Open your terminal or command prompt and run:

python --version

Or, on systems where Python 3 is explicitly required:

python3 --version

Expected output:

Python 3.11.4

If Python Is Not Installed

  • Windows: Download the official installer from https://www.python.org/downloads/. During installation, check the box labeled "Add Python to PATH" β€” this is critical for running Python from the command line.
  • Ubuntu/Debian Linux:
  sudo apt update && sudo apt install python3
  • CentOS/RHEL/AlmaLinux:
  sudo dnf install python3
  • macOS (with Homebrew):
  brew install python

> Sysadmin tip: On production servers β€” including Dedicated Servers β€” always prefer installing Python via your distribution's package manager to ensure security patches are applied automatically.

Step 2: Verify That pip Is Installed

pip is Python's default package installer. It connects to the Python Package Index (PyPI) β€” a repository hosting hundreds of thousands of open-source libraries β€” and handles downloading, dependency resolution, and installation automatically.

Check whether pip is available:

pip --version

Or:

pip3 --version

Expected output:

pip 23.2.1 from /usr/lib/python3/dist-packages/pip (python 3.11)

If pip Is Not Installed

Download and run the official bootstrap script:

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py

Alternatively, on Debian/Ubuntu:

sudo apt install python3-pip

Once installed, pip is ready to use.

Step 3: Install a Python Library Using pip

With Python and pip confirmed, you can install any library from PyPI with a single command.

Basic syntax:

pip install library_name

Example β€” installing the requests HTTP library:

pip install requests

pip will:

  1. Resolve all dependencies automatically
  2. Download the required packages
  3. Install everything to your Python environment

You will see output similar to:

Collecting requests
  Downloading requests-2.31.0-py3-none-any.whl (62 kB)
Installing collected packages: urllib3, certifi, charset-normalizer, idna, requests
Successfully installed requests-2.31.0

Step 4: Verify the Library Was Installed Successfully

After installation, confirm the library works correctly by importing it in the Python interactive shell.

Launch the Python shell:

python

Then attempt to import the library:

import requests
print(requests.__version__)

Expected output:

2.31.0

If the import completes without errors, the installation was successful. If you see a ModuleNotFoundError, the library was not installed in the active Python environment β€” this is a common issue when multiple Python versions or environments coexist on the same system.

Step 5: Install a Specific Version of a Library

In production environments, dependency pinning is critical. Different versions of a library may behave differently or introduce breaking changes. To install a precise version:

pip install library_name==version_number

Example:

pip install requests==2.25.1

You can also define version ranges:

pip install "requests>=2.25.0,<3.0.0"

This installs the latest compatible version within the specified range β€” useful when you need flexibility without risking major version upgrades.

Step 6: Upgrade an Existing Library

To update a library to its latest available version:

pip install --upgrade library_name

Example:

pip install --upgrade requests

To upgrade pip itself (recommended regularly):

pip install --upgrade pip

> Best practice: On shared hosting environments or VPS with cPanel, always test upgrades in a virtual environment before applying them to production to avoid breaking existing applications.

Step 7: Install Multiple Libraries from a requirements.txt File

For any project beyond a simple script, you should track your dependencies in a requirements.txt file. This makes your project reproducible across different machines, servers, and team members.

Creating a requirements.txt File

Create a plain text file named requirements.txt and list each dependency with its pinned version:

requests==2.31.0
numpy==1.25.2
pandas==2.0.3
flask==3.0.0
sqlalchemy==2.0.20

Installing All Dependencies at Once

pip install -r requirements.txt

pip reads the file and installs every listed package with the correct version.

Generating a requirements.txt Automatically

If you want to capture the current state of your environment:

pip freeze > requirements.txt

This exports all currently installed packages and their exact versions β€” ideal for locking down a working environment before deploying to a server.

Step 8: Use Virtual Environments to Isolate Dependencies

This is arguably the most important best practice in Python development. Virtual environments create isolated Python environments for each project, preventing dependency conflicts between projects and keeping your global Python installation clean.

Why Virtual Environments Matter

Imagine Project A requires requests==2.25.1 and Project B requires requests==2.31.0. Without virtual environments, only one version can exist globally β€” causing one project to break. Virtual environments solve this entirely.

Step-by-Step: Creating and Using a Virtual Environment

1. Install virtualenv (if not already available):

pip install virtualenv

Alternatively, use the built-in venv module (Python 3.3+):

python -m venv venv

2. Create a virtual environment in your project directory:

virtualenv venv

This creates a venv/ folder containing an isolated Python interpreter and pip.

3. Activate the virtual environment:

  • Linux/macOS:
  source venv/bin/activate
  • Windows (Command Prompt):
  venvScriptsactivate
  • Windows (PowerShell):
  venvScriptsActivate.ps1

Once activated, your terminal prompt changes to show (venv), indicating the isolated environment is active.

4. Install libraries inside the virtual environment:

pip install requests flask numpy

All packages are installed exclusively within venv/ β€” your global Python installation is untouched.

5. Deactivate the virtual environment when done:

deactivate

> Pro tip: Add your venv/ directory to .gitignore so it is never committed to version control. Only commit requirements.txt.

Step 9: Uninstall a Library

If you no longer need a library, remove it cleanly with:

pip uninstall library_name

Example:

pip uninstall requests

pip will prompt you to confirm before removing the package. To skip the confirmation prompt (useful in scripts):

pip uninstall -y requests

To uninstall all packages listed in a requirements.txt file:

pip uninstall -r requirements.txt -y

Bonus: Useful pip Commands for Day-to-Day Management

CommandDescription
pip listList all installed packages
pip show library_nameDisplay details about a specific package
pip search library_nameSearch PyPI for packages (deprecated in newer pip)
pip checkVerify installed packages have compatible dependencies
pip freezeOutput installed packages in requirements.txt format
pip install --no-cache-dir library_nameInstall without using cached files

Common Errors and How to Fix Them

ModuleNotFoundError: No module named 'requests'

You installed the library with pip but are running a different Python interpreter. Use python -m pip install requests to ensure pip installs to the correct Python version.

Permission denied errors on Linux

Avoid using sudo pip install globally. Instead, use a virtual environment, or install for the current user only:

pip install --user library_name

SSL certificate errors during installation

This may occur on servers with misconfigured SSL. Ensure your system's CA certificates are up to date. If you manage your own server infrastructure, consider pairing it with properly issued SSL Certificates to maintain a secure environment across all services.

pip not found after Python installation on Windows

Ensure Python was added to PATH during installation. Re-run the installer and select "Modify," then enable the PATH option.

Deploying Python Applications on a Server

If you are building Python-based web applications, automation scripts, or data pipelines that need to run in a hosted environment, your choice of infrastructure matters significantly.

  • VPS Hosting gives you full root access to install any Python version, manage virtual environments, configure system services, and deploy frameworks like Django, Flask, or FastAPI.
  • Dedicated Servers are ideal for resource-intensive Python workloads such as large-scale data processing, web scraping infrastructure, or high-traffic APIs.
  • GPU Hosting is the right choice for machine learning and AI projects that rely on libraries like TensorFlow, PyTorch, or CUDA-accelerated NumPy.

Conclusion

Installing and managing Python libraries is a foundational skill that directly impacts the quality, stability, and reproducibility of your projects. Here is a quick summary of what we covered:

  1. Verify Python and pip are installed and accessible
  2. Use pip install to install libraries from PyPI
  3. Pin specific versions for production stability
  4. Upgrade libraries with the --upgrade flag
  5. Use requirements.txt to manage project dependencies declaratively
  6. Always use virtual environments to isolate project dependencies
  7. Uninstall cleanly with pip uninstall

By following these practices consistently β€” whether you are developing locally or deploying on a VPS Hosting environment β€” you will build more reliable, maintainable, and professional Python applications.

15%

Save 15% on All Hosting Services

Test your skills and get Discount on any hosting plan

Use code:

Skills
Get Started