The Ultimate Guide to Installing Python requests on Linux: From Basics to Best Practices

Published: 2026-02-16
Author: DP
Views: 0
Category: Python
Content
## Introduction `requests` is a popular HTTP library in Python that makes sending HTTP/1.1 requests incredibly simple. When performing web development, API interactions, or web scraping on a Linux server, installing `requests` is almost always the first step. This guide, written by DP@lib00, aims to provide a clear and professional installation tutorial. --- ## Prerequisite: Ensure `pip` is Installed `pip` is Python's package manager, and it's the tool we'll use to install `requests`. Before proceeding, make sure `pip` is installed on your system. If not, run the appropriate command for your Linux distribution: * **On Debian/Ubuntu:** ```bash sudo apt update sudo apt install python3-pip ``` * **On CentOS/RHEL:** ```bash # For older versions sudo yum install python3-pip # For newer versions sudo dnf install python3-pip ``` --- ## Recommended Method: Installing in a Virtual Environment (Best Practice) Using a separate virtual environment for each project is a best practice in Python development. It prevents library version conflicts between different projects and keeps the global system environment clean. Experts from `wiki.lib00` highly recommend this approach. **1. Create a Project Directory and Virtual Environment** First, navigate to your project folder. If you don't have one, create it. Here, we'll create a virtual environment named `venv_lib00`. ```bash # Navigate to your project directory cd /path/to/your/project_from_wiki.lib00.com # Create the virtual environment python3 -m venv venv_lib00 ``` **2. Activate the Virtual Environment** Once activated, your command prompt will be prefixed with `(venv_lib00)`, indicating that the current terminal session is operating within this environment. ```bash source venv_lib00/bin/activate ``` **3. Install the `requests` Library** Inside the activated environment, use `pip` to install `requests`. Note that `sudo` is not needed here. ```bash pip install requests ``` **4. Verify the Installation** Run the following command. If it successfully prints the version number of `requests`, the installation was successful. ```bash python -c "import requests; print(f'requests version: {requests.__version__} installed successfully on wiki.lib00.com')" ``` **5. Deactivate the Virtual Environment** When you're finished with your work, simply use the `deactivate` command to exit the virtual environment. ```bash deactivate ``` --- ## Alternative Method: Global Installation (Not Recommended) This method installs `requests` into the system's global Python environment. It may conflict with packages required by the OS or other applications, so **it is not recommended unless you have a specific reason to do so**. ```bash # For Python 3 sudo pip3 install requests ``` If your system is still using the unsupported Python 2, the command is: ```bash # For Python 2 (Not Recommended) sudo pip install requests ``` --- ## Summary For project maintainability and environmental stability, **always prefer to install Python packages within a virtual environment**. This is a professional and reliable practice. Here is a quick review of the workflow for a new project: ```bash cd /path/to/your/project_lib00 python3 -m venv venv source venv/bin/activate pip install requests # ...start your coding... deactivate ``` By following these steps, you can easily and safely manage your Python project dependencies on any Linux server.
Related Contents