You can run the fun install command to install dependent pip or apt packages. This topic describes how to run the fun install command to install dependent pip and apt packages.

Query features related to the fun install command

Run the following command to query the features that are related to the fun install command:
fun install -h
Expected output:
Usage: fun install [-f|--function <[service/]function>] [-r|--runtime <runtime>] [-p|--package-type <type>] [--save] [-e|--env key=val ...] [packageNames...]
Options:   -f, --function <[service/]function>  Specify which function to execute installation task.   install dependencies which are described in fun.yml file.
-e PATH=/code/bin (default: [])   -d, --use-docker                     Use docker container to install function dependencies   -r, --runtime <runtime>              function runtime, avaliable choice is: nodejs6, nodejs8, nodejs10,                                        nodejs12, python2.7, python3, java8, php7.2, dotnetcore2.1, custom   -p, --package-type <type>            avaliable package type option: pip, apt, npm.   Options:
  -f, --function <[service/]function>  Specify which function to execute installation task.
--index-url <pip-url>                Base URL of Python Package Index (default https://pypi.org/simple). -e, --env <env>                      environment variable, ex. -e PATH=/code/bin (default: [])
                

Install a dependent pip package

Installation operation

This topic uses Python as an example. You can run the following command to install a dependent pip package in Python in the folder where you want to install the package.
fun install --runtime python2.7 --package-type pip tensorflow  
Note
  • Modify the runtime environment as required.
  • You can specify a custom name for the pip package, such as tensorflow.
Expected output:
skip pulling image aliyunfc/runtime-python2.7:build-1.2.0... Task => [UNNAMED]      => PYTHONUSERBASE=/code/.fun/python pip install --user tensorflow 
The following part describes the parameters:
  • --runtime: the runtime environment.
    Note If the runtime environment is specified in the fun.yml file, the file has been initialized. In this case, you can ignore this parameter.
  • --package-type: the type of the dependent package to be installed. Valid values: pip and apt.
  • PYTHONUSERBASE=/code/.fun/python pip install --user tensorflow: indicates that the commands are run in the Docker container and the commands are displayed line by line.

Installation result

After the pip package is installed, the .fun folder is created.
  • .fun/python/bin
  • .fun/python/lib/python2.7/site-packages: the directory where executable files are stored.
The following directory structure is used to store files:
.fun └── python     ├── bin     │   ├── freeze_graph     │   ├── markdown_py     │   ├── pbr     │   ├── saved_model_cli     │   ├── tensorboard     │   ├── tflite_convert     │   ├── toco     │   └── toco_from_protos     └── lib         └── python2.7             └── site-packages                 ├── tensorboard                 ├── tensorboard-1.12.2.dist-info                 ├── tensorflow                 ├── tensorflow-1.12.0.dist-info                 ├── termcolor-1.1.0.dist-info                 ...            
Compared with the pip install -t . <package-name> command, the fun install
  • File storage is more organized.
  • Dependency files and code files are separated so that you can initialize the dependency files by using Object Storage Service (OSS) or Apsara File Storage NAS.

After the pip package is installed, you must configure environment variables to enable programs to find the libraries. For more information about how to configure environment variables in Function Compute, see Configure environment variables. You can run the following command to query the environment variables:

fun install env           
Expected output:
LD_LIBRARY_PATH=/code/.fun/root/usr/local/lib:/code/.fun/root/usr/lib:/code/.fun/root/usr/lib/x86_64-linux-gnu:/code/.fun/root/usr/lib64:/code/.fun/root/lib:/code/.fun/root/lib/x86_64-linux-gnu:/code/.fun/root/python/lib/python2.7/site-packages:/code/.fun/root/python/lib/python3.6/site-packages:/code:/code/lib:/usr/local/lib 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:/code/node_modules/.bin:/code/.fun/python/bin:/code/.fun/node_modules/.bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/sbin:/bin NODE_PATH=/code/node_modules:/usr/local/lib/node_modules PYTHONUSERBASE=/code/.fun/python
Note If you run the fun local and fun deploy commands to debug and deploy functions, you do not need to configure environment variables. You can use the environment variables that are configured by Funcraft.

Subsequent operations

  • Save the commands in the file
    If you need to permanently save the commands in the Funfile file, you can run the following command:
    fun install --runtime python2.7 --package-type pip --save tensorflow                  
    Expected output:
    skip pulling image aliyunfc/runtime-python2.7:build-1.2.0... Task => [UNNAMED]      => PYTHONUSERBASE=/code/.fun/python pip install --user tensorflow 
  • View logs
    If you need to view detailed logs, you can run the following command:
    fun install -v
    Expected output:
    skip pulling image aliyunfc/runtime-python3.6:build-1.2.0... Task => [UNNAMED]      => apt-get update (if need) Ign http://mirrors.aliyun.com stretch InRelease Get:1 http://mirrors.aliyun.com stretch-updates InRelease [91.0 kB] Get:2 http://mirrors.aliyun.com stretch-backports InRelease [91.8 kB] Get:3 http://mirrors.aliyun.com stretch/updates InRelease [94.3 kB] Hit http://mirrors.aliyun.com stretch Release.gpg Hit http://mirrors.aliyun.com stretch Release Get:4 http://mirrors.aliyun.com stretch-updates/main Sources [3911 B] ....                   

Install a dependent apt package

Run the following command to install a dependent apt package:

fun install --runtime python3 --package-type apt libzbar0          
Expected output:
skip pulling image aliyunfc/runtime-python3.6:build-1.9.6... Task => AptTask      => sudo apt-get update (if need)      => apt-get install -y -d -o=dir::cache=/code/.fun/tmp/install libzbar0 --reinstall --no-install-recommends      => bash -c         for f in $(ls /code/.fun/tmp/install/archives/*.deb); do           echo "Preparing to unpack ${f##*/}"           dpkg -x $f /code/.fun/root;            echo "Setting up ${f##*/}"           mkdir -p /code/.fun/tmp/install/deb-control/${f%.*};           dpkg -e $f /code/.fun/tmp/install/deb-control/${f%.*};           if [ -f "/code/.fun/tmp/install/deb-control/${f%.*}/postinst" ]; then             FUN_INSTALL_LOCAL=true DPKG_MAINTSCRIPT_NAME=postinst /code/.fun/tmp/install/deb-control/${f%.*}/postinst configure;           fi;         done;      => bash -c 'rm -rf /code/.fun/tmp/install/archives'
Notice The method for installing the apt package and related parameters are similar to those for the pip package. You can change pip to apt in the command to install the apt package. The name of the apt package can be the name of a .deb package that can be installed by using apt-get