Langchain Env Setup
This tutorial will guide you through the installation and configuration of LangChain, ensuring you can successfully run your first LangChain program.
* * *
## Environment Requirements
| Item | Minimum Version | Recommended Version |
| --- | --- | --- |
| Python | 3.10 | 3.11 or 3.12 |
| pip | 22.0 | Latest version |
| Operating System | macOS / Linux / Windows |
Check Python version:
$ python --version
Python 3.12.7
> If you haven't installed Python yet, refer to our (https://www.python.org/downloads/).
>
>
> Python package and environment management tools reference: (https://example.com/python3/uv-tutorial.html).
* * *
## Installing LangChain
LangChain uses a modular design, with core functionality and third-party integrations installed separately.
### Installing the Core Package
$ pip install langchain
This command will install the langchain main package, which includes core APIs like init_chat_model() and create_agent(), and will automatically install langchain-core as a dependency.
!(https://example.com/wp-content/uploads/2026/05/tutorial_1778725725196.png)
### Installing Model Provider Packages
LangChain itself does not contain specific model implementations; you need to install the corresponding provider package based on the model you use:
| Model Provider | Installation Command | Models Used |
| --- | --- | --- |
| OpenAI | pip install langchain-openai | GPT-4, GPT-5, etc. |
| Anthropic | pip install langchain-anthropic | Claude series |
| DeepSeek | pip install langchain-deepseek | DeepSeek-V3, R1, etc. |
| Google | pip install langchain-google-genai | Gemini series |
| Ollama (local models) | pip install langchain-ollama | Llama, Qwen, and other local models |
| xAI | pip install langchain-xai | Grok series |
| Mistral | pip install langchain-mistralai | Mistral series |
### One-Click Installation
For beginners, it is recommended to install the following combination:
$ pip install langchain langchain-openai python-dotenv
> python-dotenv is used to load API Keys from .env files. This is the best practice for managing sensitive information and avoids hardcoding Keys in your code.
* * *
## Configuring API Key
### Getting an API Key
Using OpenAI as an example, you need to first register an account and obtain an API Key:
1. Visit [https://platform.openai.com](https://platform.openai.com/) to register or log in
2. Go to the API Keys page, click "Create new secret key"
3. Copy the generated Key (format: sk-xxxxxxxx)
> The API Key will only be displayed once, please save it to a secure location immediately. Do not commit the Key to a Git repository or share it with others.
The process for obtaining keys from other providers is similar; please refer to their respective documentation.
### Setting Environment Variables
It is recommended to use a .env file to manage API Keys. Create a .env file in the project root directory:
## Example
# File path: .env
# Enter your API Key here
OPENAI_API_KEY=sk-your-api-key-here
# If using other models, configure them here too
# ANTHROPIC_API_KEY=sk-ant-your-key
# DEEPSEEK_API_KEY=sk-your-key
Then create a .gitignore file to ensure .env is not committed:
# .gitignore
.env
__pycache__/*.pyc
### Loading in Code
## Example
# File path: config.py
# Load .env file at the beginning of the program
import os
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# Verify if API Key loaded successfully
api_key = os.getenv("OPENAI_API_KEY")
if api_key:
# Only show first 8 and last 4 characters to avoid leaking the full Key
print(f"API Key loaded: {api_key[:8]}...{api_key[-4:]}")
else:
print("Warning: OPENAI_API_KEY not found, please check .env file")
* * *
## Verifying Installation
Run the following script to verify if the installation was successful:
## Example
# File path: verify_install.py
# Verify LangChain installation and API Key configuration
from dotenv import load_dotenv
load_dotenv()
# Test 1: Verify langchain import
try:
import langchain
print(f"langchain version: {langchain.__version__}")
except ImportError:
print("Error: langchain not installed, please run pip install langchain")
# Test 2: Verify langchain-openai import
try:
import langchain_openai
print("langchain
YouTip