LangChain Environment Setup
This article will guide you through the installation and configuration of LangChain to ensure you can run your first LangChain program smoothly.
Environment requirements
| Projects | Minimum version | Recommended version |
|---|---|---|
| Python | 3.10 | 3.11 or 3.12 |
| pip | 22.0 | Latest version |
| Operating system | macOS / Linux / Windows are all supported |
Check the Python version:
$ python --version
Python 3.12.7
If you have not installed Python yet, you can refer to our Python3 Environment Setup。
Python package and environment management tools reference: uv Getting Started Tutorial。
Install LangChain
LangChain uses a modular design, with core features and third-party integrations installed separately.
Install the core package
$ pip install langchain
This command installs the main langchain package, which contains core APIs such as init_chat_model() and create_agent(), and automatically installs langchain-core as a dependency.

Install model provider packages
LangChain itself does not contain specific model implementations; you need to install the corresponding provider package according to the model you use:
| Model providers | Install Command | Model to use |
|---|---|---|
| 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. |
| 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
It is recommended that beginners directly install the following combination:
$ pip install langchain langchain-openai python-dotenv
python-dotenv is used to load API Keys from .env files; this is a best practice for managing sensitive information and avoids hard-coding Keys in code.
Configure API Key
Get an API Key
Taking OpenAI as an example, you need to first register an account and obtain an API Key:
- Access the https://platform.openai.com Register or log in
- Go to the API Keys page and click "Create new secret key"
- Copy the generated Key (in the format sk-xxxxxxxx)
The API Key is shown only once; save it in a safe place immediately. Do not commit the Key to a Git repository or share it with others.
The process for other providers is similar; please refer to the corresponding documentation.
Set environment variables
It is recommended to use a .env file to manage API Keys. Create a .env file in the project root:
Example
File path: .env
Fill in your API Key
OPENAI_API_KEY
=sk-your-api-key-here
If you use other models, configure them here as well
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
Load in code
Example
File path: config.py
Load the .env file at the beginning of the program
import
os
from
dotenv
import
load_dotenv
Load environment variables from the .env file
load_dotenv
(
)
Verify that the API Key was loaded successfully
api_key
=
os
.
getenv
(
"OPENAI_API_KEY"
)
if
api_key:
Show only the first 8 and last 4 characters, to avoid leaking the full Key
(
f
"API Key loaded: {api_key[:8]}...{api_key[-4:]}"
)
else
:
(
"Warning: OPENAI_API_KEY not found, please check the .env file"
)
Verify the installation
Run the following script to verify that the installation was successful:
Example
File path: verify_install.py
Verify the LangChain installation and API Key configuration
from
dotenv
import
load_dotenv
load_dotenv
(
)
Test 1: Verify the langchain import
try
:
import
langchain
(
f
"langchain version: {langchain.version}"
)
except
ImportError
:
(
"Error: langchain is not installed, please run pip install langchain"
)
Test 2: Verify the langchain-openai import
try
:
import
langchain_openai
(
"langchain-openai is installed"
)
except
ImportError
:
(
"Error: langchain-openai is not installed, please run pip install langchain-openai"
)
Test 3: Verify API Key configuration
import
os
api_key
=
os
.
getenv
(
"OPENAI_API_KEY"
)
if
not
api_key:
(
"Error: OPENAI_API_KEY is not configured, please set it in the .env file"
)
else
:
(
f
"API Key configured successfully: {api_key[:8]}...{api_key[-4:]}"
)
Test 4: Send a test request
from
langchain.
chat_models
import
init_chat_model
model
=
init_chat_model
(
"openai:gpt-4o-mini"
)
response
=
model.
invoke
(
"Introduce Runoob Tutorial (RUNOOB) in one sentence"
)
(
f
"\nModel response: {response.content}"
)
Running result:
langchain 版本: 1.3.0
langchain-openai 已安装
API Key 配置成功: sk-proj-z...xxxx
模型回复: Runoob Tutorial(RUNOOB)是一个面向编程初学者的中文技术学习平台。
If the last step reports an error, please check: (1) whether the API Key is correct; (2) whether a network proxy is affecting the connection; (3) whether the account balance is sufficient.
In China we can use the DeepSeek large model for testing; if you do not have one yet, you need to first go to https://platform.deepseek.com/api_keys to create an API key.
Refer to DeepSeek's API documentation:https://api-docs.deepseek.com/zh-cn/。
If you need to manage multiple third-party models in a unified way, you can choose to install LiteLLM as a model gateway:
pip install -U litellm
However, the examples in this article directly use ChatOpenAI together with openai_api_base parameters to connect to DeepSeek, with no need to install LiteLLM separately.
Example
import
os
from
langchain_openai
import
ChatOpenAI
It is recommended to place the API Key in an environment variable, or assign it directly here (do not hardcode in production)
os.environ["DEEPSEEK_API_KEY"] = "sk-your-DeepSeek-key"
Initialize the model
llm
=
ChatOpenAI
(
model
=
"deepseek-v4-pro"
,
DeepSeek V4 model name
openai_api_key
=
"sk-your-Key"
,
Fill in your DeepSeek API Key
openai_api_base
=
"https://api.deepseek.com"
,
DeepSeek's API address
temperature
=
0.7
,
max_tokens
=
1024
)
Test it
response
=
llm.
invoke
(
"Hello, DeepSeek! Please give a brief self-introduction."
)
(
response.
content
)
Running the above code will output the following content:
你好!很高兴认识你!
I am DeepSeek, an AI assistant created by DeepSeek Company. My features include:
。。。
Project directory suggestions
Recommended project directory structure:
langchain-learning/
├── .env # API Key 配置(不提交到 Git)
├── .gitignore # Git 忽略规则
├── config.py # 公共配置加载
├── 01_hello_world.py # 第一篇的示例
├── 02_first_agent.py # Agent 示例
└── ... # 更多练习