All Products
Document Center

Use fun install to install third-party dependencies

Last Updated: Jun 22, 2020


fun install is a subcommand of Fun. It is used to install pip and apt dependencies. You can use the command-line interface (CLI) or a Funfile to install dependencies.

Install pip dependencies

The following commands are used to install the TensorFlow package for Python.

  1. $fun install --runtime python2.7 --package-type pip tensorflow
  2. skip pulling image aliyunfc/runtime-python2.7:build-1.2.0...
  3. Task => [UNNAMED]
  4. => PYTHONUSERBASE=/code/.fun/python pip install --user tensorflow


  • --runtime: specifies the runtime. If the fun.yml file was initialized, this parameter can be ignored because the runtime was declared in fun.yml.
  • --package-type: specifies the type of dependency to be installed. Valid values: pip and apt.
  • tensorflow: the name of a pip package.

Commands are run in the container provided by fc-docker and will be displayed. For example, PYTHONUSERBASE=/code/.fun/python pip install --user tensorflow is run in the container and is displayed.

After the package is installed, a .fun directory is generated. The executable files are placed under the .fun/python/bin directory, and the library files are placed under the .fun/python/lib/python2.7/site-packages directory.

  1. .fun
  2. └── python
  3. ├── bin
  4. ├── freeze_graph
  5. ├── markdown_py
  6. ├── pbr
  7. ├── saved_model_cli
  8. ├── tensorboard
  9. ├── tflite_convert
  10. ├── toco
  11. └── toco_from_protos
  12. └── lib
  13. └── python2.7
  14. └── site-packages
  15. ├── tensorboard
  16. ├── tensorboard-1.12.2.dist-info
  17. ├── tensorflow
  18. ├── tensorflow-1.12.0.dist-info
  19. ├── termcolor-1.1.0.dist-info
  20. ...

Compared with files installed by the pip install -t . <package-name> command, files installed by the fun install command are better organized. The dependent files and code files are separated to help initialize dependent files by using OSS or Apsara File Storage NAS after data cleansing and splitting. However, you must customize the environment variable library so that files can be found by the program. To facilitate usage, Fun provides the fun install env command to display environment variables.

  1. $fun install env
  2. LD_LIBRARY_PATH=/code/.fun/root/usr/lib/x86_64-linux-gnu:/code:/code/lib:/usr/local/lib
  3. PATH=/code/.fun/root/usr/local/bin:/code/.fun/root/usr/local/sbin:/code/.fun/root/usr/bin:/code/.fun/root/usr/sbin:/code/.fun/root/sbin:/code/.fun/root/bin:/code/.fun/python/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/sbin:/bin
  4. PYTHONUSERBASE=/code/.fun/python

For more information about how to configure environment variables for Function Compute, see Environment variables. fun local and fun deploy allow you to debug and deploy without worrying about environment variables.

Use —save to save commands to the Funfile

install commands added with the —save parameter are saved to the Funfile.

  1. $fun install --runtime python2.7 --package-type pip --save tensorflow
  2. skip pulling image aliyunfc/runtime-python2.7:build-1.2.0...
  3. Task => [UNNAMED]
  4. => PYTHONUSERBASE=/code/.fun/python pip install --user tensorflow

The preceding commands are added with the --save parameter. The commands in the Funfile are as follows:

  1. RUNTIME python2.7
  2. RUN fun-install pip install tensorflow

You can directly run fun install to sequentially execute the tasks.

  1. $fun install
  2. skip pulling image aliyunfc/runtime-python2.7:build-1.2.0...
  3. Task => [UNNAMED]
  4. => PYTHONUSERBASE=/code/.fun/python pip install --user tensorflow

Use -v to view log details

  1. $fun install -v
  2. skip pulling image aliyunfc/runtime-python3.6:build-1.2.0...
  3. Task => [UNNAMED]
  4. => apt-get update (if need)
  5. Ign stretch InRelease
  6. Get:1 stretch-updates InRelease [91.0 kB]
  7. Get:2 stretch-backports InRelease [91.8 kB]
  8. Get:3 stretch/updates InRelease [94.3 kB]
  9. Hit stretch Release.gpg
  10. Hit stretch Release
  11. Get:4 stretch-updates/main Sources [3911 B]
  12. ....

Install apt dependencies

Function Compute allows you to install apt dependencies by using the fun install apt-get command.

  1. $fun install --runtime python3 --package-type apt libzbar0
  2. skip pulling image aliyunfc/runtime-python3.6:build-1.2.0...
  3. Task => [UNNAMED]
  4. => apt-get update (if need)
  5. => apt-get install -y -d -o=dir::cache=/code/.fun/tmp libzbar0
  6. => bash -c 'for f in $(ls /code/.fun/tmp/archives/*.deb); do dpkg -x $f /code/.fun/root; done;'
  7. => bash -c 'rm -rf /code/.fun/tmp/archives'

The installation method and parameters are similar to the pip dependency installation. --package-type must be set to apt. The package name must be specified as a deb package that can be installed by apt-get.

Use a Funfile to install dependencies

Fun will run the commands contained in a Funfile to produce certain results.

Initialize a Funfile

You can run the fun install init command to initialize a Funfile. After the command is run, you must select a runtime.

  1. $fun install init
  2. ? Select a runtime
  3. nodejs6
  4. nodejs8
  5. nodejs10
  6. python2.7
  7. python3
  8. java8
  9. php7.2
  10. (Move up and down to reveal more choices)

For example, if you select nodejs10, a file named Funfile will be created in the current directory. The command in the Funfile is as follows:

  1. RUNTIME nodejs10

You can also create a file named Funfile and enter commands because the Funfile is a text file that does not need Fun to initialize.

Edit the Funfile

If the Funfile exists in the root directory of the function you are operating, the Funfile will be automatically executed to install dependencies when you run the fun install or fun build command.For example, to install the Flask library of pip,you must use the RUNTIME command to specify the runtime for Flask, and then directly enter commands. You must add fun-install before the apt-get and pip commands. For example, the pip command to install Flask is pip install flask, and you must enter fun-install pip install flask in the Funfile.

The commands in the Funfile are as follows:

  1. RUNTIME python3
  2. RUN fun-install pip install flask

Run the fun install command. The command output is as follows:

  1. $fun install
  2. using template: template.yml
  3. start installing function dependencies without docker
  4. building pyzbar-srv/pyzbar-fun
  5. Funfile exist, Fun will use container to build forcely
  6. Step 1/2 : FROM aliyunfc/runtime-python3.6:build-1.6.1
  7. ---> 2110e9fa9393
  8. Step 2/2 : RUN fun-install pip install flask
  9. ---> Using cache
  10. ---> 992b0d65136d
  11. sha256:992b0d65136dfa63b9566b8280881bb4cf22c4f1b68155713545e4b61ada8dd9
  12. Successfully built 992b0d65136d
  13. Successfully tagged fun-cache-e95a9bfa-a6fa-4d70-b5b4-0084613e2b8d:latest
  14. copying function artifact to /Users/tan/code/fun/examples/install/pyzbar_example
  15. could not find any manifest file for pyzbar-srv/pyzbar-fun, [install] stage for manifest will be skipped
  16. Tips for next step
  17. ======================
  18. * Invoke Event Function: fun local invoke
  19. * Invoke Http Function: fun local start
  20. * Build Http Function: fun build
  21. * Deploy Resources: fun deploy

The dependencies installed by fun install will be installed to the code directory. You can use the fun local commands for local debugging and use fun deploy to deploy dependencies to the cloud.

Syntax supported by Funfile

The commands supported by Funfiles are included in Dockerfile commands, such as COPY, RUN, ENV, USER, and WORKDIR. For example, you can write the following commands in a Funfile to copy a file to the container and perform operations:

  1. RUNTIME nodejs8
  2. COPY ./package.json .
  3. RUN npm install

You can use the following complex commands to install the rlang package in Function Compute:

  1. RUNTIME python3
  2. RUN fun-install apt-get install libblas3
  3. RUN fun-install pip install pandas
  4. RUN apt-get build-dep -y r-base; \
  5. curl -L | tar -zxf -; \
  6. cd R-3.5.2 ; \
  7. ./configure --prefix=/code/.fun/R/ --enable-R-shlib --with-blas --with-lapack ; \
  8. make ; \
  9. make install;
  10. RUN PATH=/code/.fun/R/bin:$PATH LD_LIBRARY_PATH=/code/.fun/root/usr/lib/libblas:$LD_LIBRARY_PATH fun-install pip install rpy2
  11. RUN rm -rf R-3.5.2; \
  12. rm -rf .fun/root/usr/share; \
  13. rm -rf .fun/R/share; \
  14. rm -rf .fun/R/lib/R/doc; \
  15. rm -rf .fun/python/lib/python3.6/site-packages/pandas/tests; \
  16. rm -rf .fun/R/lib/R/library/*/{help,demo,html,doc}; \
  17. find .fun -type f -name "*.so" -exec strip --strip-all {} \;

You must add fun-install before the native Dockerfile apt-get and pip commands.


  1. The detailed commands written in a Funfile to install dependencies standardize the development process.
  2. Submitting a Funfile rather than specific dependencies to the version control system helps maintain function code.
  3. Funfile commands are similar to the native Dockerfile commands. You can test in an interactive environment by using fun install box and then write the final installation commands in a Funfile as in a Dockerfile.
  4. Funfiles also support the cache feature of Dockerfiles. If the project changes have no effect on Funfile, Funfile can directly use the cache to skip the corresponding commands. This speeds up the execution of Funfiles and improves development efficiency.