Ubuntu环境下安装TensorFlow CPU版创建Virtualenv报错求助
Hey there! Let's figure out what's going wrong here. The command you ran mixes two separate operations—installing packages via apt-get and creating a virtual environment with virtualenv—which is why you're hitting an error. Let's break this down into correct, actionable steps:
First, run the apt-get command without the virtualenv-specific flags (--system-site-packages and -p), since those don't belong to the package installation process. Also, python-virtualenv is deprecated in newer Ubuntu releases, so we only need virtualenv:
sudo apt-get update && sudo apt-get install python3-pip python3-dev virtualenv -y
Now that the tools are installed, use the virtualenv command to set up your environment in the target directory. The -p flag specifies which Python interpreter to use, and --system-site-packages lets the environment access system-wide installed packages:
virtualenv --system-site-packages -p python3 /home/ubuntu/workspace/tensor/
Note: If the /home/ubuntu/workspace/ directory doesn't exist yet, create it first with:
mkdir -p /home/ubuntu/workspace/
Once the environment is created, activate it—your terminal prompt will show (tensor) to confirm you're in the right context:
source /home/ubuntu/workspace/tensor/bin/activate
Now install the TensorFlow CPU version. For newer TensorFlow 2.x releases, the main tensorflow package automatically defaults to CPU mode if no GPU is detected, so you can use either of these commands:
# Option 1: Explicit CPU package pip install tensorflow-cpu # Option 2: Main package (auto-detects no GPU) pip install tensorflow
- Permission Errors: If you run into permission issues when creating the environment, don't use
sudowithvirtualenv—your user should have write access to the/home/ubuntu/workspace/directory. If you accidentally usedsudoearlier, fix the ownership with:sudo chown -R ubuntu:ubuntu /home/ubuntu/workspace/tensor/ - Pip Upgrade Errors: If
pipthrows installation errors, upgrade it first inside the activated environment:pip install --upgrade pip
内容的提问来源于stack exchange,提问作者user881703

