# Install extensions from the command line using Slicer 5.2.2

**URL:** https://discourse.slicer.org/t/install-extensions-from-the-command-line-using-slicer-5-2-2/31439
**Category:** Support
**Tags:** extensions, command-line, termianl
**Created:** [August 29, 2023, 9:26pm UTC](https://discourse.slicer.org/t/install-extensions-from-the-command-line-using-slicer-5-2-2/31439 "2023-08-29T21:26:19Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![jhlegarreta](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jhlegarreta/32/66542_2.png) [@jhlegarreta](https://discourse.slicer.org/u/jhlegarreta)
#### Post date: [August 29, 2023, 9:26pm UTC](https://discourse.slicer.org/t/install-extensions-from-the-command-line-using-slicer-5-2-2/31439/1 "2023-08-29T21:26:19Z")

</div>

Hi,  
can Slicer extensions be installed from the command line? If yes, where can I find the related documentation?

Thanks.

---

<div class="post-metadata">

### Author: ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)
#### Post date: [August 29, 2023, 9:42pm UTC](https://discourse.slicer.org/t/install-extensions-from-the-command-line-using-slicer-5-2-2/31439/2 "2023-08-29T21:42:23Z")

</div>

In the past I’ve used this:

> <https://github.com/pieper/SlicerDockers/blob/master/slicer-plus/Dockerfile#L16-L21>

to run this script:

> <https://github.com/pieper/SlicerDockers/blob/master/slicer-plus/install-slicer-extension.py>

But you can also use this newer code:  
[https://slicer.readthedocs.io/en/latest/developer\_guide/script\_repository.html#download-and-install-extension](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#download-and-install-extension)

---

<div class="post-metadata">

### Author: ![jhlegarreta](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jhlegarreta/32/66542_2.png) [@jhlegarreta](https://discourse.slicer.org/u/jhlegarreta)
#### Post date: [August 29, 2023, 9:57pm UTC](https://discourse.slicer.org/t/install-extensions-from-the-command-line-using-slicer-5-2-2/31439/3 "2023-08-29T21:57:06Z")

</div>

OK, thanks Steve. Will give it a try and report back as time permits.

---

<div class="post-metadata">

### Author: ![jhlegarreta](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jhlegarreta/32/66542_2.png) [@jhlegarreta](https://discourse.slicer.org/u/jhlegarreta)
#### Post date: [August 29, 2023, 11:08pm UTC](https://discourse.slicer.org/t/install-extensions-from-the-command-line-using-slicer-5-2-2/31439/4 "2023-08-29T23:08:38Z")

</div>

So, when launching the shell command (using Slicer 5.2.2 on an Ubuntu 22.04 machine), e.g.

```auto
slicer_ext="CurveMaker"

export EXTENSION_TO_INSTALL=${slicer_ext}

./Slicer \
  --no-splash \
  --no-main-window \
  --python-script "/<my_path>/install_slicer_extension.py"

```

where the `install_slicer_extension.py` script is esentially what you kindly posted in [https://github.com/pieper/SlicerDockers/blob/master/slicer-plus/install-slicer-extension.py](https://github.com/pieper/SlicerDockers/blob/master/slicer-plus/install-slicer-extension.py), I get the following error:

```auto
installing CurveMaker
Traceback (most recent call last):
  File "<string>", line 5, in <module>
  File "<string>", line 28, in <module>
  File "<string>", line 17, in main
AttributeError: qSlicerExtensionsManagerModel has no attribute named 'retrieveExtensionMetadataByName'

```

the command hangs, and the installation does not take place.

So maybe something changed from the version you used at the time (5.0.2 according to the commit message) and 5.2.2?

---

<div class="post-metadata">

### Author: ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)
#### Post date: [August 30, 2023, 4:53am UTC](https://discourse.slicer.org/t/install-extensions-from-the-command-line-using-slicer-5-2-2/31439/5 "2023-08-30T04:53:11Z")

</div>

Starting with Slicer 5.4, a dedicated API was introduced to streamline the programmatic installation of extensions.

See [https://slicer.readthedocs.io/en/latest/developer\_guide/script\_repository.html#download-and-install-extension](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#download-and-install-extension)

---

<div class="post-metadata">

### Author: ![jhlegarreta](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jhlegarreta/32/66542_2.png) [@jhlegarreta](https://discourse.slicer.org/u/jhlegarreta)
#### Post date: [August 30, 2023, 12:48pm UTC](https://discourse.slicer.org/t/install-extensions-from-the-command-line-using-slicer-5-2-2/31439/6 "2023-08-30T12:48:17Z")

</div>

Steve already pointed to that possibility for Slicer 5.4; I’m using 5.2.2, though.

---

<div class="post-metadata">

### Author: ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)
#### Post date: [August 30, 2023, 1:15pm UTC](https://discourse.slicer.org/t/install-extensions-from-the-command-line-using-slicer-5-2-2/31439/7 "2023-08-30T13:15:09Z")

</div>

> [@jhlegarreta](#):
>
> So maybe something changed from the version you used at the time (5.0.2 according to the commit message) and 5.2.2?

See [BUG: fix/doc DMRIInstall (#6594) · Slicer/Slicer@537281b · GitHub](https://github.com/Slicer/Slicer/commit/537281b6cfeaa3bdf9f35538efb53846a2fe3729#diff-1c3362f027f4595ea24d34d4627256f9d63d418fcddbd325fe8d022829c611ac)

---

<div class="post-metadata">

### Author: ![jhlegarreta](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jhlegarreta/32/66542_2.png) [@jhlegarreta](https://discourse.slicer.org/u/jhlegarreta)
#### Post date: [August 30, 2023, 1:24pm UTC](https://discourse.slicer.org/t/install-extensions-from-the-command-line-using-slicer-5-2-2/31439/8 "2023-08-30T13:24:22Z")

</div>

Hitting the very same error reported earlier even when modifying the script according to it.

---

<div class="post-metadata">

### Author: ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)
#### Post date: [August 30, 2023, 1:33pm UTC](https://discourse.slicer.org/t/install-extensions-from-the-command-line-using-slicer-5-2-2/31439/9 "2023-08-30T13:33:02Z")

</div>

With Slicer \>= 5.2, consider using the following script:

```python
import os

extensionName = os.environ['EXTENSION_TO_INSTALL']
em = slicer.app.extensionsManagerModel()
if not em.isExtensionInstalled(extensionName):
    em.interactive = False # prevent display of popups
    em.updateExtensionsMetadataFromServer(True, True) # update extension metadata from server now
    installDependencies = True
    if not em.downloadAndInstallExtensionByName(extensionName, installDependencies, True):
        raise ValueError(f"Failed to install {extensionName} extension")

```

Assuming you saved it as `/tmp/install_slicer_extension.py`, you may run it using:

```bash
$ EXTENSION_TO_INSTALL=Sandbox \
~/Downloads/Slicer-5.2.2-linux-amd64/Slicer \
  --exit-after-startup \
  --no-main-window \
  --disable-modules \
  --python-script /tmp/install_slicer_extension.py

Retrieving extension metadata for Sandbox extension
Retrieving Sandbox extension files (extensionId: 63f5ca688939577d9867ba35)
Downloading Sandbox extension (item_id: 63f5ca688939577d9867ba35, file_id: 64c8c82516826926710d5a97)
Installed extension Sandbox (63f5ca688939577d9867ba35) revision e163d61

```

---

<div class="post-metadata">

### Author: ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)
#### Post date: [August 30, 2023, 1:42pm UTC](https://discourse.slicer.org/t/install-extensions-from-the-command-line-using-slicer-5-2-2/31439/10 "2023-08-30T13:42:34Z")

</div>

And here is an improved version allowing to install extensions in Slicer 5.2.2 while passing extension names as regular arguments:

```python
import sys

def install_exttension(extensionName, installDependencies=True):
    print(f"\nInstalling extension {extensionName}")
    em = slicer.app.extensionsManagerModel()
    if em.isExtensionInstalled(extensionName):
        print(f"Extension {extensionName} already installed")
        return

    em.interactive = False # prevent display of popups
    em.updateExtensionsMetadataFromServer(True, True) # update extension metadata from server now

    if not em.downloadAndInstallExtensionByName(extensionName, installDependencies, True):
        raise ValueError(f"Failed to install {extensionName} extension")

if __name__ == " __main__":
    for extensionName in sys.argv[1:]:
        install_exttension(extensionName)

```

For future reference, to install extension in Slicer 5.4, there is a dedicated API to do so. See [here](https://slicer.readthedocs.io/en/5.4/developer_guide/script_repository.html#download-and-install-extension)

Then, to install extensions:

```bash
$ ads/Slicer-5.2.2-linux-amd64/Slicer \
  --exit-after-startup \
  --no-main-window \
  --disable-modules \
  --python-script /tmp/install_slicer_extension.py \
  Sandbox \
  SlicerHeart \
  SlicerDMRI

```

and example of output:

```plaintext
Installing extension Sandbox
Retrieving extension metadata for Sandbox extension
Retrieving Sandbox extension files (extensionId: 63f5ca688939577d9867ba35)
Downloading Sandbox extension (item_id: 63f5ca688939577d9867ba35, file_id: 64c8c82516826926710d5a97)
Installed extension Sandbox (63f5ca688939577d9867ba35) revision e163d61

Installing extension SlicerHeart
Retrieving extension metadata for SlicerHeart extension
Retrieving SlicerHeart extension files (extensionId: 63f5f3f08939577d9867dbc7)
Downloading SlicerHeart extension (item_id: 63f5f3f08939577d9867dbc7, file_id: 64de321206a93d6cff35a4c1)
"The following extensions are required by SlicerHeart extension therefore they will be installed now: SlicerIGT, SlicerIGSIO"
Retrieving extension metadata for SlicerIGT extension
Retrieving SlicerIGT extension files (extensionId: 63f5f2d78939577d9867da34)
Downloading SlicerIGT extension (item_id: 63f5f2d78939577d9867da34, file_id: 64aa9a94b81c954f339bfef6)
Retrieving extension metadata for SlicerIGSIO extension
Retrieving SlicerIGSIO extension files (extensionId: 63f5da488939577d9867c528)
Downloading SlicerIGSIO extension (item_id: 63f5da488939577d9867c528, file_id: 64c0f2c6c0e19dd3b30f7352)
Installed extension SlicerHeart (63f5f3f08939577d9867dbc7) revision 09e440d
Installed extension SlicerIGT (63f5f2d78939577d9867da34) revision 245fa92
Installed extension SlicerIGSIO (63f5da488939577d9867c528) revision b6328fd

Installing extension SlicerDMRI
Retrieving extension metadata for SlicerDMRI extension
Retrieving SlicerDMRI extension files (extensionId: 63fb32ce8939577d9869e2b2)
Downloading SlicerDMRI extension (item_id: 63fb32ce8939577d9869e2b2, file_id: 64ccc3ed16826926710f2500)
"The following extensions are required by SlicerDMRI extension therefore they will be installed now: UKFTractography"
Retrieving extension metadata for UKFTractography extension
Retrieving UKFTractography extension files (extensionId: 63f5eae38939577d9867d2b3)
Downloading UKFTractography extension (item_id: 63f5eae38939577d9867d2b3, file_id: 63f5eae38939577d9867d2ba)
Installed extension SlicerDMRI (63fb32ce8939577d9869e2b2) revision aad4331
Installed extension UKFTractography (63f5eae38939577d9867d2b3) revision fcf83e2

```

---

<div class="post-metadata">

### Author: ![jhlegarreta](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jhlegarreta/32/66542_2.png) [@jhlegarreta](https://discourse.slicer.org/u/jhlegarreta)
#### Post date: [August 30, 2023, 2:59pm UTC](https://discourse.slicer.org/t/install-extensions-from-the-command-line-using-slicer-5-2-2/31439/11 "2023-08-30T14:59:10Z")

</div>

OK, after some more work, got it working: looks like I had still carried over the statement to call `retrieveExtensionMetadataByName` in [https://github.com/pieper/SlicerDockers/blob/master/slicer-plus/install-slicer-extension.py#L7C3-L7C77](https://github.com/pieper/SlicerDockers/blob/master/slicer-plus/install-slicer-extension.py#L7C3-L7C77), so the instructions in [Install extensions from the command line using Slicer 5.2.2 - #10 by jcfr](https://discourse.slicer.org/t/install-extensions-from-the-command-line-using-slicer-5-2-2/31439/10) worked.

Thanks for the effort @jcfr.
