You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot部署Elastic Beanstalk后,如何安装numpy和matplotlib?

在Elastic Beanstalk上为Spring Boot应用安装numpy和matplotlib

方法一:通过.ebextensions配置自动安装依赖

这是Elastic Beanstalk部署时自动安装依赖的标准方式,步骤如下:

  • 在Spring Boot项目的src/main/resources目录下创建.ebextensions文件夹(注意文件夹名开头有英文点)
  • 在该文件夹内新建一个配置文件,比如命名为python-dependencies.config
  • 根据你的Elastic Beanstalk操作系统版本,添加对应配置:

针对Amazon Linux 2环境

commands:
  # 安装系统依赖(matplotlib编译需要这些库)
  00_install_system_libs:
    command: "yum install -y python3-devel gcc libpng-devel freetype-devel"
  # 确保pip3可用
  01_install_pip:
    command: "yum install -y python3-pip"
  # 安装numpy和matplotlib
  02_install_numpy:
    command: "pip3 install numpy"
  03_install_matplotlib:
    command: "pip3 install matplotlib"

针对旧版Amazon Linux AMI环境

如果你的环境是较旧的Amazon Linux AMI,调整命令如下:

commands:
  00_install_system_libs:
    command: "yum install -y python27-devel gcc libpng-devel freetype-devel"
  01_install_pip:
    command: "yum install -y python-pip"
  02_install_numpy:
    command: "pip install numpy"
  03_install_matplotlib:
    command: "pip install matplotlib"

每次部署应用时,Elastic Beanstalk会自动执行这些命令,完成依赖安装。

方法二:使用requirements.txt批量管理依赖

如果需要管理多个Python包,可以用requirements.txt统一声明:

  • 在项目根目录创建requirements.txt文件,内容如下:
numpy
matplotlib
  • 在.ebextensions/python-dependencies.config中添加命令,批量安装依赖:
commands:
  00_install_system_libs:
    command: "yum install -y python3-devel gcc libpng-devel freetype-devel"
  01_install_pip:
    command: "yum install -y python3-pip"
  02_install_requirements:
    command: "pip3 install -r /var/app/current/requirements.txt"

注意:确保requirements.txt被Maven打包到Jar包的根目录,这样部署后路径/var/app/current/requirements.txt才会存在。

关键注意事项

  • 确认你的Spring Boot应用调用Python脚本时,使用的是安装了依赖的Python版本(比如调用python3而不是python)
  • 如果安装matplotlib时出现编译错误,优先检查是否安装了上述系统依赖库
  • Maven构建时,.ebextensions文件夹会被自动打包到Jar包中,无需额外配置pom.xml

内容的提问来源于stack exchange,提问作者KiteZeng

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 12:09:20