1 问题
ubuntu18.04 LTS默认安装了python2.7,但有时候我们需要python3的最新版本,以适应一些软件的框架。
2 安装
2.1 安装依赖
sudo apt-get install build-essential libbz2-dev libssl-dev libreadline-dev libsqlite3-dev
2.2 下载python源码
wget https://www.python.org/ftp/python/3.12.2/Python-3.12.2.tar.xz
2.3 编译安装python3.12
tar -xvf Python-3.12.2.tar.xz
cd Python-3.12.2
./configure
make -j2
make install
2.4 更新Python链接
sudo update-alternatives --install /usr/bin/python python /usr/local/bin/python3.12 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/local/bin/python3.12 1
2.5 查看结果
python -V
pip -V
如果安装成功,则结果如下:
python -V
Python 3.12.2
pip -V
pip 24.0 from /usr/local/lib/python3.12/site-packages/pip (python 3.12)
但是,由于旧版本遗留的原因,在输入pip -V
时,有可能出现如下的警告:
WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.
Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
To avoid this problem you can invoke Python with '-m pip' instead of running pip directly.
pip 24.0 from /usr/local/lib/python3.12/site-packages/pip (python 3.12)
此时,只需要重新安装下pip即可,命令如下:
python3 -m pip install --upgrade --force-reinstall pip