Automate Instagram with Python and Instapy on Ubuntu Server

Automate Instagram with Python and Instapy on Ubuntu Server

In this article we are going to discuss how you can automate your Instagram with Python and Instapy. Following from the previous tutorial on automating twitter with python – we are going to do something similar with available libraries for Instagram.

automate instagram with python and instapy
automate instagram with python and instapy

What we will cover in this tutorial:

  • Set up development environment – specifically for ubuntu
  • Install instapy
  • Create an instagram session
  • Schedule python script to run on system file

Set up development environment – for automating Instagram with python and Instapy

We are going to work from an Ubuntu virtual machine, you can get one here:

Get digital ocean droplet, and use this video to set it up: initial virtual server set-up.

Then you can follow the following steps: note that if you get a virtual machine with Digital Ocean, you should select the latest version of Ubuntu and it will come pre-installed with python 3.8 (Ubuntu 20.04).

The steps we will be following are also available from the official Instapy documentation website.

Run this from the command like – to install python3 supporting libraries, pip3 etc.

sudo apt-get update
sudo apt-get -y install unzip python3-pip python3-dev build-essential libssl-dev libffi-dev xvfb

Install virtual environment

pip3 install virtualenv

Install Chrome and Firefox – tools that Instapy uses in the background, you need these for Instapy to work. Use wget to download chrome:

cd ~
wget "https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb"

Install chrome

sudo dpkg -i google-chrome-stable_current_amd64.deb

If you get errors, jus run the following command:

apt-get -f install

Remove the downloaded file:

sudo rm google-chrome-stable_current_amd64.deb

Install firefox and driver

sudo apt-get install firefox 
sudo apt install firefox-geckodriver

Navigate to the folder you want to work from and create virtual environment

mkdir instagram && cd instagram
virtualenv instagramenv
source instagramenv/bin/activate

pip install instapy

After running all the commands above successfully, you will have instapy installed inside of a virtual environment, you can start working with.

Create Instapy Session – Run some code

Create a file called config.py where you will enter your instagram username and password:

#config.py

insta_username = 'instagram-username'
insta_password = 'instagram-password'

In the same directory, create the following file – main.py

from instapy import InstaPy, smart_run
import config



def wordPressSession():
    comments = [
            'Learn more from https://skolo.online',
            'Learn WordPress https://skolo.online',
            'Learn WooCommerce from https://skolo.online',
            'Yes! Amazing stuff',
            'Just love wordpress',
            'Check us out https://skolo.online',
            'Check out Youtube https://rb.gy/n7tfbn',
            'Free Learning Videos https://rb.gy/n7tfbn',
            'Online Learning https://rb.gy/n7tfbn',
            'WooCommerce Series https://rb.gy/hgc8um',
            'Learn WordPress https://rb.gy/hgc8um',
            'Learn WooCommerce https://rb.gy/hgc8um'
            ]

    session = InstaPy(username=config.insta_username,
                      password=config.insta_password,
                      headless_browser=True)

    try:
        with smart_run(session):
            session.like_by_tags(["wordpress", 'woocommerce'], amount=10)
            session.set_do_comment(True, percentage=50)
            session.set_comments(comments)
    except:
        import traceback
        print(traceback.format_exc())
        pass


wordPressSession()

The code above will log you in to your instagram profile, search for the tags you have provides: ‘wordpress’ and ‘woocommerce’. It will then like a number of statuses with those tags, even comment on 50% of the statuses with the comments you have provided in the list.

Run this file by entering in the command line:

python main.py

Automate the whole process

The reason why we are running this on a virtual server is, we want the ability to run this code continuously on a schedule. Python provides a scheduling library fit for this purpose:

While still inside the virtual environment:

pip install schedule

Then edit your code to look like this:

from instapy import InstaPy, smart_run
import config
import schedule
import time
import random



def wordPressSession():
    comments = [
            'Learn more from https://skolo.online',
            'Learn WordPress https://skolo.online',
            'Learn WooCommerce from https://skolo.online',
            'Yes! Amazing stuff',
            'Just love wordpress',
            'Check us out https://skolo.online',
            'Check out Youtube https://rb.gy/n7tfbn',
            'Free Learning Videos https://rb.gy/n7tfbn',
            'Online Learning https://rb.gy/n7tfbn',
            'WooCommerce Series https://rb.gy/hgc8um',
            'Learn WordPress https://rb.gy/hgc8um',
            'Learn WooCommerce https://rb.gy/hgc8um'
            ]

    session = InstaPy(username=config.insta_username,
                      password=config.insta_password,
                      headless_browser=True)

    try:
        with smart_run(session):
            session.like_by_tags(["wordpress", 'woocommerce'], amount=10)
            session.set_do_comment(True, percentage=50)
            session.set_comments(comments)
    except:
        import traceback
        print(traceback.format_exc())
        pass



schedule.every().day.at("08:00").do(wordPressSession)

while True:
    schedule.run_pending()
    time.sleep(1)


The code above will run the function called: wordPressSession() – everyday at 08:00.

Create system file called Instagram:

sudo nano /etc/systemd/system/instagram.service

Inside the file, paste the following:

[Unit]
Description=Instagram Bot Skolo Online Service
After=multi-user.target

[Service]
Type=simple
Environment="PATH=/home/username/instagram/instagramenv/bin"
ExecStart=/home/username/instagram/instagramenv/bin/python /home/username/instagram/main.py


[Install]
WantedBy=multi-user.target

The code above will need to be modified for your use case:

Environment: Should be the absolute path to your env file/bin

Exec Start: Should be the absolute path to your python inside the env and the file location you are running. Sort of like: python main.py – but with absolute paths.

After this run:

sudo systemctl start instagram
sudo systemctl enable instagram
sudo systemctl status instagram

You should see a nice green light, after the last command when checking on the status of your system file – if all is well. If not check the error message and fix accordingly – the usual cause for errors is wrong absolute paths in the system file.

Full Video Tutorial – Automate instagram with python and instapy:

Full youtube video tutorial below.

Leave a Reply

Your email address will not be published.

Subscribe to our mailing list

And get this 💃💃 Free😀 eBook !

Holler Box