在 CentOS 上因為自帶的 Python 版本比較舊,所以要安裝高版本的 Python 但同時要求不能影響原來的 python3
命令。
先不急著動手,可以先看看關於多版本的專題。
非常不建議直接覆蓋當前系統的 python
和 python3
命令,Python 每個版本之間多少有點不兼容問題,處理起來非常麻煩,萬一系統用到了,就是個大問題。
Building Python:
make install
can overwrite or masquerade thepython3
binary.make altinstall
is therefore recommended instead ofmake install
since it only installsexec_prefix/bin/pythonversion
.
If you want it to install to, for example, /usr/bin
instead of the default (/usr/local/bin
in ubuntu/debian), then instead of ./configure
, type ./configure --prefix=/usr
when told to use it in the guide.
For in your $HOME/bin
directory, use --prefix=$HOME
.
If it doesn’t exist, add $HOME/bin
to your $PATH like this:
$ export PATH=$HOME/bin:$PATH
This may already be in your .bashrc
in ubuntu, and others. If it is, when you next log in, $HOME/bin
will be added to your $PATH
automatically.
Let’s take a look at the generated Makefile!
First, the install target:
install: altinstall bininstall maninstall
It does everything altinstall
does, along with bininstall
and maninstall
Here’s bininstall
; it just creates the python
and other symbolic links.
# Install the interpreter by creating a symlink chain:
# $(PYTHON) -> python2 -> python$(VERSION))
# Also create equivalent chains for other installed files
bininstall: altbininstall
-if test -f $(DESTDIR)$(BINDIR)/$(PYTHON) -o -h $(DESTDIR)$(BINDIR)/$(PYTHON); \
then rm -f $(DESTDIR)$(BINDIR)/$(PYTHON); \
else true; \
fi
(cd $(DESTDIR)$(BINDIR); $(LN) -s python2$(EXE) $(PYTHON))
-rm -f $(DESTDIR)$(BINDIR)/python2$(EXE)
(cd $(DESTDIR)$(BINDIR); $(LN) -s python$(VERSION)$(EXE) python2$(EXE))
... (More links created)
And here’s maninstall
, it just creates “unversioned” links to the Python manual pages.
# Install the unversioned manual pages
maninstall: altmaninstall
-rm -f $(DESTDIR)$(MANDIR)/man1/python2.1
(cd $(DESTDIR)$(MANDIR)/man1; $(LN) -s python$(VERSION).1 python2.1)
-rm -f $(DESTDIR)$(MANDIR)/man1/python.1
(cd $(DESTDIR)$(MANDIR)/man1; $(LN) -s python2.1 python.1)
TLDR: altinstall
skips creating the python
link and the manual pages links, install
will hide the system binaries and manual pages.
一般在 /usr/bin/
下放一個鏈接,鏈接到 /usr/local/bin/
下面:
$ ln -s /usr/local/bin/pip /usr/bin/pip
$ ln -s /usr/local/bin/pip3 /usr/bin/pip3
$ ln -s /usr/local/bin/pip3.9 /usr/local/bin/pip3
Login to your CentOS 8 / CentOS 7 system as root or user with sudo privileges.
Then do system update
sudo yum -y install epel-release
sudo yum -y update
Reboot after the upgrade before you continue to install dependencies
sudo reboot
Install required software development tools required to build Python 3.9 on CentOS 8 / CentOS 7:
sudo yum groupinstall "Development Tools" -y
sudo yum install openssl-devel libffi-devel bzip2-devel -y
Confirm gcc
is available:
$ gcc --version
gcc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-4)
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Ensure wget
is installed:
sudo yum install wget -y
Use wget to download the latest Python 3.9 release.
wget https://www.python.org/ftp/python/3.9.10/Python-3.9.10.tgz
Extract the archive file using tar:
tar xvf Python-3.9.10.tgz
Switch to the directory created from the file extraction:
cd Python-3.9*/
Run the command below to configure Python installation.
./configure --enable-optimizations
Build Python 3.9 on CentOS 8 / CentOS 7:
sudo make altinstall # altinstall is important, DO NOT use `make install`.
Python 安裝時
make install
和make altinstall
的區別: altinstall skips creating the python link and the manual pages links.altinstall
跳過創建 Python 鏈接和手冊頁鏈接的操作。如果使用make install
,在系統中將會有兩個不同版本的 Python 在/usr/bin
目錄中,這將會導致很多問題。
Be patient as this takes quite some time depending on number of CPU cores in your system.
Check Python 3.9 installation on CentOS 8 / CentOS 7. Run below command to confirm successful installation of Python 3.9 on CentOS 8 / CentOS 7:
$ python3.9 --version
Python 3.9.10
Pip3.9 must have been installed as well:
$ pip3.9 --version
pip 21.2.4 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)
Upgrade pip
$ /usr/local/bin/python3.9 -m pip install --upgrade pip
$ pip3.9 --version