Ubuntu16.04虚拟环境安装TensorFlow遇EnvironmentError[Errno13]求助
Hey there, let's work through this permission error you're facing when installing TensorFlow in your virtual environment. The core issue here is that your pip command is still trying to write to the system-wide /usr/local/bin directory instead of your isolated virtual environment—let's fix that step by step.
1. Verify Your Virtual Environment is Properly Activated
First off, make sure you've activated your virtual environment correctly. If you created it with venv, run this command in your terminal:
source /path/to/your/venv/bin/activate
You'll know it's active when you see (venv) at the start of your terminal prompt.
To double-check you're using the virtual environment's pip instead of the system one, run:
which pip3
The output should point to your virtual environment's directory (e.g., /home/your_username/venv/bin/pip3), not /usr/local/bin/pip3. If it shows the system path, your virtual environment isn't activated properly—re-run the activation command and try installing TensorFlow again.
2. Use the --user Flag as a Quick Fix
If activating the virtual environment still doesn't work (or if you want a temporary workaround), install TensorFlow for your user account only instead of system-wide:
pip3 install tensorflow --user
This will install all packages in your user's home directory (usually ~/.local/lib/pythonX.X/site-packages), avoiding the need for system permissions and the /usr/local/bin path that's causing the error.
3. Check Python Version Compatibility
You mentioned suspecting a Python version issue—let's confirm that. Ubuntu 16.04 comes with Python 3.5 by default, which only supports TensorFlow 1.x versions. If you're trying to install a newer TensorFlow 2.x release, it won't work with Python 3.5.
If you want to use TensorFlow 2.x:
- Upgrade Python to 3.6 or 3.7 (these are compatible with Ubuntu 16.04 and most TensorFlow 2.x releases)
- Create a new virtual environment using the upgraded Python:
python3.6 -m venv your_new_venv source your_new_venv/bin/activate pip3 install tensorflow
4. Reset Your Virtual Environment If Needed
If your virtual environment seems corrupted, delete it and start fresh:
rm -rf /path/to/your/venv python3 -m venv /path/to/your/venv source /path/to/your/venv/bin/activate pip3 install --upgrade pip # Make sure pip is up to date pip3 install tensorflow
Final Notes
The virtual environment approach is still the best practice because it keeps your project dependencies isolated from the system. Start with verifying the activation—this is the most common reason for this error even when using a venv.
内容的提问来源于stack exchange,提问作者Aashish75

