How to Install and Run Ollama on macOS and use with Python

Published on July 14, 2025

How to Install and Run Ollama on macOS and Create a Stock Report with Python

This tutorial guides you through installing Ollama on macOS to run AI large language models locally, interacting with the Mistral model in the terminal, and creating a Python program to generate a daily stock report using live stock data. The program will save the report to a file, send it via Telegram, and be scheduled to run automatically using cron. This is for educational purposes only; I am not responsible for your stock trading decisions.

Prerequisites

  • A macOS system with at least 16GB RAM (recommended for Mistral model).
  • Visual Studio Code installed.
  • Python 3 installed.
  • A Telegram bot set up with a bot token and chat ID for sending messages (not covered in this tutorial; ensure you have a working telegram_message function).

Step 1: Install and Set Up Ollama

  1. Download Ollama:

    • Visit the Ollama website and click "Download for macOS".
    • Open the downloaded file and drag the Ollama app to your Applications folder.
  2. Start the Ollama Server:

    • Open Terminal and run:
      ollama serve
      
    • This starts the Ollama server in the background.
  3. Download the Mistral Model:

    • Open a new Terminal window (Command + T).
    • Pull the Mistral model, which is lightweight and suitable for 16GB RAM systems:
      ollama pull mistral
      
    • Wait a few minutes for the download to complete, depending on your internet speed.
  4. Run and Test the Mistral Model:

    • In the new Terminal window, start the Mistral model:
      ollama run mistral
      
    • Wait a few seconds for it to boot up.
    • Test it by typing questions, such as:
      Where is Badlands National Park?
      
      or
      Why is the sky blue?
      
    • The model runs locally on your Mac’s CPU without an internet connection.

Step 2: Set Up the Python Environment

  1. Open Visual Studio Code:

    • Launch Visual Studio Code.
  2. Create a Virtual Environment:

    • Open the integrated terminal in Visual Studio Code (View > Terminal or Ctrl + ~).
    • Create a virtual environment:
      python -m venv venv
      
    • Activate the virtual environment:
      source venv/bin/activate
      
    • You should see (venv) in the terminal prompt.
  3. Install Required Libraries:

    • Install the yfinance and requests libraries:
      pip install yfinance requests
      

Step 3: Create the Stock Report Program

  1. Create a New File:

    • In Visual Studio Code, go to File > New File or press Ctrl + N.
    • Save the file as stock_report.py (File > Save As or Ctrl + S).
  2. Add the Stock Report Code:

    • Copy and paste the following code into stock_report.py:
      import yfinance as yf
      import requests
      import json
      from telegram import telegram_message
      
      # List of stocks
      stocks = ["AAPL", "MSFT", "NVDA", "AMZN", "TSLA"]
      
      # Get stock data for the past day
      data = yf.download(stocks, period="1d")
      
      # Create a prompt for Ollama with the stock data
      prompt = f"Generate a short report on these stocks for the past 24 hours: {', '.join(stocks)}. Data: {data.to_dict()}"
      print(f"Prompt: {prompt}")
      
      # Send the prompt to Ollama
      response = requests.post("http://localhost:11434/api/generate", json={"model": "mistral", "prompt": prompt, "stream": False})
      result = response.json()
      report = result.get("response", "")
      
      print('
      
      ')
      print('--------------------Report--------------------')
      
      # Display the report
      print(report)
      
      # Save the report to a text file
      with open('stock_report.txt', 'w') as f:
          f.write(report)
      
      # Send the report via Telegram
      telegram_message(report)
    • Save the file (Ctrl + S).
    • Note: Ensure you have a working telegram_message function defined elsewhere to send the report via Telegram.
  3. Run the Program:

    • Ensure the virtual environment is activated (source venv/bin/activate).
    • In the Visual Studio Code terminal, run:
      python stock_report.py
      
    • Alternatively, use Visual Studio Code’s Run feature (Run > Run Without Debugging or Ctrl + F5).
    • The program will:
      • Fetch stock data for AAPL, MSFT, NVDA, AMZN, and TSLA.
      • Generate a report using the Mistral model.
      • Print the report in the terminal.
      • Save it to stock_report.txt in your project folder.
      • Send it via Telegram.

Step 4: Schedule the Script with Cron

  1. Open the Crontab Editor:

    • In the Visual Studio Code terminal, open the crontab editor:
      crontab -e
      
  2. Add a Cron Job:

    • Add the following line to schedule the script to run daily at 6:00 PM (replace /path/to/your/project with your actual project folder path):
      0 18 * * * /bin/bash -c 'cd /path/to/your/project && source venv/bin/activate && python stock_report.py'
      
    • To find your project path, run pwd in the terminal while in your project folder and use that path.
    • For example, if your project is in /Users/jakegrim/YouTube_Demo, use:
      0 18 * * * /bin/bash -c 'cd /Users/jakegrim/YouTube_Demo && source venv/bin/activate && python stock_report.py'
      
  3. Save and Exit:

    • In the vi editor, press Esc, type :wq, and press Enter to save and exit.
  4. Verify the Cron Job:

    • Check that the cron job was added:
      crontab -l
      
    • The script will now run daily at 6:00 PM, generating and sending the stock report via Telegram.

Conclusion

You’ve successfully:

  • Installed Ollama and run the Mistral model locally on macOS.
  • Created a Python program to generate a daily stock report using live data and the Mistral model.
  • Saved the report to a file and sent it via Telegram.
  • Scheduled the program to run automatically using cron.

Now, you’ll receive a stock report every day at 6:00 PM via Telegram. Have fun exploring local AI and building awesome projects!