How to Install and Configure LangChain for LLM Development
This guide walks you through installing the LangChain library, adding model‑specific packages, verifying the setup with a Python script, configuring API keys via environment variables or a .env file, and preparing to use OpenAI‑compatible models such as DeepSeek or Qwen.
Chapter 1: Installation and Environment Setup
Welcome to the first chapter of the LangChain tutorial. Before building powerful LLM applications, you must install LangChain and configure the development environment correctly.
LangChain Installation
Since version v0.1.0, LangChain uses a modular package structure (e.g., langchain-core, langchain-community), but the tutorial recommends installing the umbrella package langchain to simplify usage.
1. Install LangChain
pip install langchainThis single command provides core abstractions, orchestration logic, Chains, Agents, and Retrieval‑Augmented Generation (RAG) features.
2. Install Model‑Specific Integration Packages
Integrations for particular models or components are released as separate packages. For OpenAI models: pip install langchain-openai For Google models:
pip install langchain-google-genaiVerify the Installation
A helper script check_installation.py validates that the langchain package and required environment variables are correctly set. Run: python check_installation.py The script checks: langchain package is installed.
OpenAI API key is present (if using OpenAI models).
Other environment configurations are functional.
Environment Setup: API Keys
Most LLM providers require an API key, which LangChain reads from environment variables. For OpenAI, set OPENAI_API_KEY either temporarily in the shell: export OPENAI_API_KEY="sk-..." or persistently in a .env file placed at the project root: OPENAI_API_KEY="sk-..." Load the file with python-dotenv:
pip install python-dotenv from dotenv import load_dotenv
load_dotenv()Model Usage
The tutorial uses OpenAI‑compatible models, so the langchain-openai package is required. Examples include the DeepSeek and Qwen models. The author uses the DeepSeek public‑cloud model named deepseek-v3; if you use the official DeepSeek model, the name should be deepseek-chat. Replace the name accordingly for Qwen.
Full Verification Script (check_installation.py)
# check_installation.py
import os
import sys
from dotenv import load_dotenv
def check_openai_api_key():
print("
--- Checking OpenAI API Key ---")
load_dotenv()
openai_api_key = os.getenv("OPENAI_API_KEY")
if not openai_api_key:
raise ValueError("OPENAI_API_KEY not found in environment variables. Please set it in a .env file.")
print(" - Found OPENAI_API_KEY environment variable.")
print(f" (Key: {openai_api_key[:5]}...{openai_api_key[-4:]})")
return True
def main():
"""Simple script to verify LangChain and related packages are installed and required env vars are set."""
print("--- LangChain Installation and Environment Check ---")
# 1. Verify python-dotenv import
try:
from dotenv import load_dotenv
print("
[SUCCESS] `python-dotenv` imported successfully.")
except ImportError as e:
print(f"
[ERROR] `python-dotenv` import failed: {e}")
print("Please run: pip install python-dotenv")
return
# 2. Verify LangChain import
try:
import langchain
from langchain.prompts import PromptTemplate
from langchain.schema import BaseMessage
print(f"
[SUCCESS] `langchain` imported successfully. Version: {langchain.__version__}")
except ImportError as e:
print(f"
[ERROR] `langchain` import failed: {e}")
print("Please run: pip install langchain")
return
# 3. Verify OpenAI integration package and API key
try:
from langchain_openai import OpenAI
print("[SUCCESS] `langchain-openai` imported successfully.")
check_openai_api_key()
api_base = os.getenv("OPENAI_API_BASE")
if api_base:
print(f" - Found OPENAI_API_BASE: {api_base}")
else:
print(" - [INFO] OPENAI_API_BASE not set. It's optional for custom endpoints.")
print(" To use a custom endpoint, add: OPENAI_API_BASE=\"https://your-endpoint.com/v1\" to .env")
except ImportError as e:
print(f"
[WARNING] `langchain-openai` not installed or failed to import: {e}")
print("If you need OpenAI models, run: pip install langchain-openai")
print("
--- Check Complete ---")
print("Environment is ready. You can proceed to the next LangChain chapters.")
if __name__ == "__main__":
main()References
How to: install LangChain packages – https://python.langchain.com/docs/how_to/install_langchain
How to: use LangChain with different Pydantic versions – https://python.langchain.com/docs/how_to/pydantic_compatibility
BirdNest Tech Talk
Author of the rpcx microservice framework, original book author, and chair of Baidu's Go CMC committee.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
