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

如何通过Bash脚本自动化在EC2实例上安装Anaconda?

Automating Anaconda Installation on EC2 with Expect

Got it, let’s fix this automation problem for you. The yes command falls short here because the Anaconda installer has specific, sequential prompts that need different inputs—not just endless "y"s. Expect is the right tool for the job, and setting it up on EC2 is straightforward. Here’s exactly how to pull off the full automated install:

Step 1: Install Expect on Your EC2 Instance

First, we need to get expect installed since it’s not included by default on most EC2 AMIs. Run the appropriate command based on your OS:

  • For Amazon Linux 2/RHEL/CentOS:
    sudo yum install -y expect
    
  • For Ubuntu/Debian:
    sudo apt update && sudo apt install -y expect
    

Step 2: Create the Expect Automation Script

Create a new file (let’s call it auto_install_anaconda.exp) with the following content. This script will handle every interactive prompt the Anaconda installer throws at you:

#!/usr/bin/expect

# Download the Anaconda installer (replace with your preferred version URL if needed)
spawn wget https://repo.anaconda.com/archive/Anaconda3-2024.02-1-Linux-x86_64.sh

# Wait for download to finish
expect eof

# Make installer executable
spawn chmod +x Anaconda3-2024.02-1-Linux-x86_64.sh

# Launch the installer
spawn ./Anaconda3-2024.02-1-Linux-x86_64.sh

# 1. Press Enter to start the installation flow
expect "Press Enter to continue"
send "\r"

# 2. Press q to skip reading the license agreement
expect "More"
send "q\r"

# 3. Enter yes to accept the license terms
expect "Do you accept the license terms? [yes|no]"
send "yes\r"

# 4. Press Enter to confirm the default installation location
expect "Press ENTER to confirm the location"
send "\r"

# 5. Enter yes to add Anaconda to your PATH via .bashrc
expect "Do you wish the installer to initialize Anaconda3 by running conda init? [yes|no]"
send "yes\r"

# Wait for installation to complete
expect eof

Step 3: Run the Automation Script

Make the expect script executable and execute it:

chmod +x auto_install_anaconda.exp
expect auto_install_anaconda.exp

Post-Installation

Once the script finishes, reload your .bashrc to activate the Anaconda PATH changes immediately:

source ~/.bashrc

Alternatively, log out and back in to your EC2 instance—either way, you’ll be able to run conda commands right away.

Quick Notes

  • If you want a specific Anaconda version, grab the corresponding download URL from the official Anaconda archive page and replace it in the script.
  • If wget isn’t installed on your instance, swap it out with curl -O instead.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 08:39:02