<p>There's an event you can listen to:</p>

<pre class="lang-php prettyprint-override"><code>use craft\events\PluginEvent;
use craft\services\Plugins;

        // Handler: EVENT_AFTER_INSTALL_PLUGIN
        Event::on(
            Plugins::class,
            Plugins::EVENT_AFTER_INSTALL_PLUGIN,
            function (PluginEvent $event) {
                if ($event-&gt;plugin === $this) {
                    // Do stuff here, we were just installed
                }
            }
        );
</code></pre>

<p>If you generate your plugin scaffolding via <a href="https://pluginfactory.io/" rel="nofollow noreferrer">pluginfactory.io</a> it will generate the above event for you in your base plugin.</p>

CC BY-SA 4.0
2019-06-13T13:56:16.943Z
2019-06-13T13:56:16.943Z
andrew.welch
<p>How can I run a function when my plugin is installed? I tried SafeUp in migrations but it doesn't work.</p>

CC BY-SA 4.0
2019-06-13T13:23:42.003Z
You tried making an [Install migration](https://docs.craftcms.com/v3/extend/migrations.html#plugin-install-migrations)?
Oli
<plugin-development><craft3>
How to run function after plugin installed?
# How to run function after plugin installed?

(Asked by: username_0 on 2019-06-13)

How can I run a function when my plugin is installed? I tried SafeUp in migrations but it doesn't work.

**Tags:** plugin-development, craft3

> **Comments:**
> **username_1**: You tried making an [Install migration](https://docs.craftcms.com/v3/extend/migrations.html#plugin-install-migrations)?  

---

## 1 answers

### Answer by username_2 on 2019-06-13. Score: 3

There's an event you can listen to:
use craft\events\PluginEvent;
use craft\services\Plugins;

        // Handler: EVENT_AFTER_INSTALL_PLUGIN
        Event::on(
            Plugins::class,
            Plugins::EVENT_AFTER_INSTALL_PLUGIN,
            function (PluginEvent $event) {
                if ($event->plugin === $this) {
                    // Do stuff here, we were just installed
                }
            }
        );

If you generate your plugin scaffolding via pluginfactory.io it will generate the above event for you in your base plugin.


---

