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 --versionOr, on systems where Python 3 is explicitly required:
python3 --versionExpected output:
Python 3.11.4If 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 --versionOr:
pip3 --versionExpected 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.pyAlternatively, on Debian/Ubuntu:
sudo apt install python3-pipOnce 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_nameExample β installing the requests HTTP library:
pip install requestspip will:
- Resolve all dependencies automatically
- Download the required packages
- 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.0Step 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:
pythonThen attempt to import the library:
import requests
print(requests.__version__)Expected output:
2.31.0If 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_numberExample:
pip install requests==2.25.1You 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_nameExample:
pip install --upgrade requestsTo 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.20Installing All Dependencies at Once
pip install -r requirements.txtpip 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.txtThis 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 virtualenvAlternatively, use the built-in venv module (Python 3.3+):
python -m venv venv2. Create a virtual environment in your project directory:
virtualenv venvThis 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.ps1Once 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 numpyAll 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_nameExample:
pip uninstall requestspip will prompt you to confirm before removing the package. To skip the confirmation prompt (useful in scripts):
pip uninstall -y requestsTo uninstall all packages listed in a requirements.txt file:
pip uninstall -r requirements.txt -yBonus: Useful pip Commands for Day-to-Day Management
| Command | Description |
|---|---|
pip list | List all installed packages |
pip show library_name | Display details about a specific package |
pip search library_name | Search PyPI for packages (deprecated in newer pip) |
pip check | Verify installed packages have compatible dependencies |
pip freeze | Output installed packages in requirements.txt format |
pip install --no-cache-dir library_name | Install 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_nameSSL 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:
- Verify Python and pip are installed and accessible
- Use
pip installto install libraries from PyPI - Pin specific versions for production stability
- Upgrade libraries with the
--upgradeflag - Use
requirements.txtto manage project dependencies declaratively - Always use virtual environments to isolate project dependencies
- 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.
