# Create canister and install code with wasm\_module

**URL:** <https://forum.dfinity.org/t/create-canister-and-install-code-with-wasm-module/6468>\
**Category:** Developers\
**Created:** [August 14, 2021, 8:59am UTC](https://forum.dfinity.org/t/create-canister-and-install-code-with-wasm-module/6468 "2021-08-14T08:59:13Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Safik](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/safik/32/953_2.png) [@Safik](https://forum.dfinity.org/u/Safik)\
**Post date:** [August 14, 2021, 8:59am UTC](https://forum.dfinity.org/t/create-canister-and-install-code-with-wasm-module/6468/1 "2021-08-14T08:59:14Z")

</div>

Hello.  
I can create the canister(s) using the new operation, referring to the actor class

Variant №1  
Example: (very roughly and briefly)

> actor class ObjectActor() = this{};  
> let oa: = await ObjectActor();  
> //ID new canister  
> let p: = Principal.fromActor(oa);

Variant №2  
I can also create an empty canister. And use it when necessary.  
Example:

> ```
> public func create_canister_empty_with_cycles(settings: ?canister_settings, amount : ?Nat)
> : async { canister_id : canister_id } { 
> ExperimentalCycles.add(100_000_000_000); 
> let canister_new_with_cycles = 
> await IC.provisional_create_canister_with_cycles({ settings : ?canister_settings; amount : ?Nat;});
> 
> return canister_new_with_cycles
> };
> 
> ```

Next, using template in the specification.  
Apparently, I can update an empty (and not empty) canister. I use the canister\_id obtained above.

> install\_code : shared {  
> arg : [Nat8];  
> wasm\_module : wasm\_module;  
> mode : { #reinstall; #upgrade; #install };  
> canister\_id : canister\_id;  
> } → async ();

Question: how do I compile the code of the actor class(ObjectActor) in wasm\_module?  
Is it possible to use it dynamically in the IC today?

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [August 15, 2021, 8:24am UTC](https://forum.dfinity.org/t/create-canister-and-install-code-with-wasm-module/6468/2 "2021-08-15T08:24:52Z")

</div>

You can compile the actor class to suitable wasm just by passing it as the root file to the moc compiler (or making it the main file of a dfx actor).

You’re main difficulty will then be quoting the code (and its serialized argument) as a couple of blobs in Motoko to pass to `IC.install_code` We don’t currently expose an official way for users to to serialize shared types to blobs.

(FTR: the way the compiler currently compiles an actor that imports an actor class is to recursively invoke the compiler on the actor class, embed the binary of the class as a blob in the importer, and compile a helper function for the class constructor that serializes the argument to a blob, creates and installs a new actor with the new blob and then casts the principal to the expected interface.)

---

<div class="post-metadata">

**Author:** ![Safik](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/safik/32/953_2.png) [@Safik](https://forum.dfinity.org/u/Safik)\
**Post date:** [August 15, 2021, 9:03am UTC](https://forum.dfinity.org/t/create-canister-and-install-code-with-wasm-module/6468/3 "2021-08-15T09:03:46Z")

</div>

Hi, Claudio. Thanks. The problem was adding cycles, but I solved it. I realized that it is better to use standard methods. Apparently, if you update the wasm code with install\_code (wash\_module), the built-in logic of the actor’s work ( functions, properties)it will be unavailable for calls from outside it.

---

<div class="post-metadata">

**Author:** ![avi](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/avi/32/1904_2.png) [@avi](https://forum.dfinity.org/u/avi)\
**Post date:** [October 31, 2021, 11:45am UTC](https://forum.dfinity.org/t/create-canister-and-install-code-with-wasm-module/6468/4 "2021-10-31T11:45:08Z")

</div>

Excuse me, is there a way to convert wasm to blob now? Our business needs this install\_code function very much

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [November 1, 2021, 11:57am UTC](https://forum.dfinity.org/t/create-canister-and-install-code-with-wasm-module/6468/5 "2021-11-01T11:57:47Z")

</div>

We haven’t added anything to give you access to the wasm blob of an imported actor class.

If you already have the wasm on disk, you can try to (manually) encode it as a blob literal.  
There some examples of using Text literals constrained to type Blob to introduce blobs here:

> <https://github.com/dfinity/motoko/blob/master/test/run/blobs.mo>

Unfortunately, we don’t have any direct way to include a file as a Text or Blob literal, but could conceivably add that in future.

There a simple example in our tests of installing a miminal wasm file constructed from blob literal here.

> <https://github.com/dfinity/motoko/blob/master/test/run-drun/install-empty-actor.mo>

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [November 1, 2021, 4:38pm UTC](https://forum.dfinity.org/t/create-canister-and-install-code-with-wasm-module/6468/6 "2021-11-01T16:38:59Z")

</div>

If you want to do this on the client side, you can use ic-repl, see the `install.sh` example: [GitHub - chenyan2002/ic-repl](https://github.com/chenyan2002/ic-repl#installsh)

---

<div class="post-metadata">

**Author:** ![avi](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/avi/32/1904_2.png) [@avi](https://forum.dfinity.org/u/avi)\
**Post date:** [November 4, 2021, 10:56am UTC](https://forum.dfinity.org/t/create-canister-and-install-code-with-wasm-module/6468/7 "2021-11-04T10:56:21Z")

</div>

Thanks, let me see  
This can bring me a lot of banzhu

---

<div class="post-metadata">

**Author:** ![avi](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/avi/32/1904_2.png) [@avi](https://forum.dfinity.org/u/avi)\
**Post date:** [November 4, 2021, 10:58am UTC](https://forum.dfinity.org/t/create-canister-and-install-code-with-wasm-module/6468/8 "2021-11-04T10:58:06Z")

</div>

I tried to convert wasm to blob format, but because the program was too large, the blob data reached 1.2m and the program could not be executed.  
I want to use the motoko code method to perform code update and installation operations

---

<div class="post-metadata">

**Author:** ![ayjayem](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ayjayem/32/1019_2.png) [@ayjayem](https://forum.dfinity.org/u/ayjayem)\
**Post date:** [January 5, 2022, 4:11pm UTC](https://forum.dfinity.org/t/create-canister-and-install-code-with-wasm-module/6468/9 "2022-01-05T16:11:34Z")

</div>

+1 for this request - I too would love to be able to add a function in a Motoko canister that installs another Motoko canister.

---

<div class="post-metadata">

**Author:** ![jzxchiang](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jzxchiang/32/2592_2.png) [@jzxchiang](https://forum.dfinity.org/u/jzxchiang)\
**Post date:** [January 6, 2022, 12:15am UTC](https://forum.dfinity.org/t/create-canister-and-install-code-with-wasm-module/6468/10 "2022-01-06T00:15:52Z")

</div>

I believe you can with actor classes. See [this](https://smartcontracts.org/docs/language-guide/actor-classes.html#actor_classes).

The catch is that the Motoko canister that’s being installed needs to be defined in source at compile time and imported by the “parent” Motoko canister.

There’s no way to dynamically install a Motoko canister, i.e. with arbitrary wasm passed in at runtime. At least that’s my understanding of this thread…

---

<div class="post-metadata">

**Author:** ![ayjayem](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ayjayem/32/1019_2.png) [@ayjayem](https://forum.dfinity.org/u/ayjayem)\
**Post date:** [January 6, 2022, 12:31am UTC](https://forum.dfinity.org/t/create-canister-and-install-code-with-wasm-module/6468/11 "2022-01-06T00:31:43Z")

</div>

Aha! This is exactly what I was looking for - thank you!

---

<div class="post-metadata">

**Author:** ![ayjayem](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ayjayem/32/1019_2.png) [@ayjayem](https://forum.dfinity.org/u/ayjayem)\
**Post date:** [January 26, 2022, 5:58pm UTC](https://forum.dfinity.org/t/create-canister-and-install-code-with-wasm-module/6468/12 "2022-01-26T17:58:19Z")

</div>

As a follow-up here - how can one add a controller (rather than set a controller) for a newly-created canister, similar to what’s described [here](https://forum.dfinity.org/t/how-to-get-the-controller-inside-the-canister-with-rust-motoko/5102/12)?

(Or even better, is it possible to specify controllers when creating a canister from Motoko in the first place?)

---

<div class="post-metadata">

**Author:** ![ayjayem](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ayjayem/32/1019_2.png) [@ayjayem](https://forum.dfinity.org/u/ayjayem)\
**Post date:** [January 26, 2022, 8:52pm UTC](https://forum.dfinity.org/t/create-canister-and-install-code-with-wasm-module/6468/13 "2022-01-26T20:52:54Z")

</div>

I think this may have been a silly question, as it looks like the example provided above demonstrates setting multiple controllers. Assuming this is correct, please disregard!

---

<div class="post-metadata">

**Author:** ![jamesbeadle](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jamesbeadle/32/10110_2.png) [@jamesbeadle](https://forum.dfinity.org/u/jamesbeadle)\
**Post date:** [August 17, 2024, 9:05am UTC](https://forum.dfinity.org/t/create-canister-and-install-code-with-wasm-module/6468/14 "2024-08-17T09:05:52Z")

</div>

Hi @claudio,

I need to update a canister’s actor class that was dynamically created by another canister and I’m just wondering how to do this.

I have a function that takes a wasm module:

```auto

    public func updateManagerCanisterWasm() : async () {
      let IC : Management.Management = actor (Environment.Default);
      let updateResult = await (
        IC.install_code(
          {
            mode = #upgrade(null);
            canister_id = Principal.fromText("ljxqq-4iaaa-aaaal-qjd4a-cai");
            wasm_module = "";
            arg = Blob.fromArray([]);
            sender_canister_version = null;
          }),
      );
    };

```

But I need this file:

> <https://github.com/jamesbeadle/OpenFPL/blob/master/src/OpenFPL_backend/canister_definitions/manager-canister.mo>

for the wasm\_module property…

Any help appreciated.

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [August 25, 2024, 10:50am UTC](https://forum.dfinity.org/t/create-canister-and-install-code-with-wasm-module/6468/15 "2024-08-25T10:50:35Z")

</div>

Sorry for the delay, I’m on holiday without a laptop.

There is way to do this, briefly described here, but without any examples:

> **[Language reference | Internet Computer](https://internetcomputer.org/docs/current/motoko/main/reference/language-manual#actor-class-management)**
>
> \<!--

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [August 25, 2024, 10:57am UTC](https://forum.dfinity.org/t/create-canister-and-install-code-with-wasm-module/6468/16 "2024-08-25T10:57:43Z")

</div>

A bad example from our tests is this line

> <https://github.com/dfinity/motoko/blob/master/test%2Frun-drun%2Factor-class-mgmt.mo#L102-L103>

But the test isn’t really meant for user consumption…
