07 Oct LangGraph Setup: Install LangGraph and Create Your Project
Before building bigger graphs, let’s set up a clean workspace to set up LangGraph. This takes about 10 minutes.
What you need
- Python 3.10 or higher (check with python –version)
- A code editor (VS Code works well)
- An API key from an LLM provider (needed from Chapter 8; you can skip it for now)
Step 1: Create a project folder and virtual environment
A virtual environment keeps this project’s libraries separate from the rest of your system.
mkdir langgraph-tutorial cd langgraph-tutorial python -m venv venv
Activate it:
# Windows venv\Scripts\activate # macOS / Linux source venv/bin/activate
You will see (venv) at the start of your terminal line when it is active.
Step 2: Install the libraries
pip install -U langgraph python-dotenv
- langgraph is the main library
- python-dotenv loads secret keys from a file, so they never appear in your code
For the AI chapters, also install one model package:
# Option A: OpenAI pip install -U langchain-openai # Option B: Anthropic (Claude) pip install -U langchain-anthropic
Step 3: Verify the installation
Run this in the terminal:
pip show langgraph
If you see a name and version number, you are set. You can also run the Chapter 1 example as a quick test.
Step 4: Store your API key safely
Create a file named .env in your project folder:
OPENAI_API_KEY=your-key-here
For Anthropic, use ANTHROPIC_API_KEY=your-key-here instead.
Load it in Python:
from dotenv import load_dotenv
import os
load_dotenv()
print("Key loaded:", os.getenv("OPENAI_API_KEY") is not None)
Output:
Key loaded: True
Important: never paste API keys directly into your code, and never upload .env to GitHub. Create a .gitignore file containing:
.env venv/ __pycache__/
Step 5: Recommended project structure
langgraph-tutorial/ ├── venv/ ├── .env ├── .gitignore ├── requirements.txt ├── ch01_hello.py ├── ch02_state_nodes_edges.py └── ch03_setup_check.py
One file per chapter keeps things easy to find. Save your dependencies with:
pip freeze > requirements.txt
Anyone can then recreate your setup with pip install -r requirements.txt.
Step 6: See your graph as a diagram
LangGraph can draw your graph. Add this to the end of your Chapter 2 code:
print(graph.get_graph().draw_mermaid())
Output (Mermaid text):
graph TD;
__start__ --> clean_question;
clean_question --> make_answer;
make_answer --> __end__;
Paste this text into a Mermaid viewer (for example, mermaid.live) to see the flowchart. We will explore visualization more in Chapter 20.
Common setup problems
- ModuleNotFoundError: langgraph: activate your venv, then reinstall
- python not found: try python3 instead
- Key shows None: check the .env file name and location, and that you called load_dotenv()
- Old Python version: install Python 3.10 or newer from python.org
Summary
- Use a virtual environment for every project
- Install langgraph, python-dotenv, and one model package
- Keep API keys in .env, and keep .env out of Git
- get_graph().draw_mermaid() gives you a diagram of any graph
Practice
- Create the project folder, virtual environment, and .gitignore
- Run the Chapter 2 example inside your new setup and print its Mermaid diagram
If you liked the tutorial, spread the word and share the link and our website, Studyopedia, with others.
For Videos, Join Our YouTube Channel:Â Join Now
Read More:
- Generative AI Tutorial
- AI Ethics
- Machine Learning Tutorial
- Deep Learning Tutorial
- Ollama Tutorial
- Retrieval Augmented Generation (RAG) Tutorial
- ChatGPT Tutorial
- Microsoft Copilot Tutorial
No Comments