PRINTING IN LINUX WITH CUPS

本文档详细介绍了如何在Linux环境下使用CUPS打印系统进行打印机配置与管理,包括选择合适的打印机类型、安装CUPS软件包、通过网页浏览器设置新打印机、配置防火墙规则以及与Samba集成等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PRINTING IN LINUX WITH CUPS

By Robert Spotswood


General Printing Tips

  • Understand your printing needs. If you will print mostly text, a laser will give much better quality and cost per page. If you need low volume color, you'll want an inkjet. If you want high volume color, good luck!
  • Always check the compatibility lists! Don't buy a printer until you know your OS(s) is(are) supported.
  • If in doubt, make sure the cable is IEEE 1284. Some old printers don't need this cable, but most (all?) new printers require this type of cable. Using a non-IEEE 1284 cable could void your warranty.
  • If using a parallel port on a computer, set your bios correctly. The choices on a modern bios are SPP, ECP, EPP, ECP+EPP. SPP is the poorest choice. ECP is the best choice for printers and scanners. EPP works best for things like parallel port drives. ECP+EPP is a compromise between ECP and EPP. Don't select this unless you have multiple parallel port devices that would use both ECP and EPP.
  • Also in the bios, if you have the option, "EPP Mode Select", pick "EPP 1.9". Note that with an IEEE 1284 cable and this setting, cable lengths as long as 50 meters have been used successfully.
  • You get what you pay for. From the printing howto: "The least expensive printers, for example, have a MTBF [Mean Time Between Failures] of about three months".
  • If you buy supplies and equipment online, try to use the affiliate program for the linuxprinting organization. This helps to support them and other free printing software.

Printing in Linux Introduction

  • Setting up printing under Linux isn't fun or easy, especially for newbies.
  • One LinuxJournal article estimates that printing problems take up about 25% of a system administrator's time.
  • Some distros (Redhat for instance) come with semi-automatic printer configurators.
  • Unfortunately, these are unreliable and cover only a limited number of printers.
  • Printing other than straight text (<sarcasm>Who would want to print graphics anyway!?</sarcasm>) has always been something of an afterthought. It shows!

Why choose CUPS?


Printing Programs

The four main printing programs in Linux today are:

  • lpr (note this is also called lpd; here I use the terms interchangeably)
  • lprng
  • pdq
  • cups

Note a few programs have their own printing system, such as StarOffice, and WordPerfect. They tend to be inferior in that the number of printer drivers available is often vary limited.


lpr

  • Sometimes referred to as BSD-style print queue or lpd.
  • Runs as root. The fewer root programs you run, the safer your system is.
  • Listens on all interfaces (including your internet interface). You cannot turn this off. Therefore, this is another security risk.
  • Really only does text or postscript on its own.
  • It can be coupled with Ghostscript to do graphics and support more printers.
  • Setting this up can be maddening, but it works ok (sans security) once it is setup.
  • lpr is the least common denominator of standards-based network printing.
  • lpr is widely supported.
  • From the Linux printing howto: "...recommend that people install and use CUPS or PDQ in most cases instead of (or in addition to) lpd."

lprng

  • Designed to be an improvement on lpr.
  • LPRng is far easier to administer for large installations than lpr.
  • It supports authentication via PGP or Kerberos.
  • LPRng uses more or less the same basic filter model as lpr. This means lpr print filters will work with lprng too.
  • The Linux printing howto does not recommend it.
  • As one user put it:
    ...LPRng, which is in my view an encoded acronym for "You Don't Need a printer." The BSD print arrangement could be understood with sufficient study, but with LPRng you're better off on a North Carolina beach with a metal detector, looking for treasure. Where configuring the BSD-style stuff would impart neurosis, LPRng can get you a new garment, one whose arms buckle in the back...I set about making LPRng work with my LaserJet III-D with PostScript cartridge. Two hours later, it was time to consider other options.

pdq

  • Wasn't able to get to the home page for this project, so I can't offer much info. (What I have is all second hand.)
  • It is one of the two printing systems recommended by the printing howto.
  • PDQ is perhaps the easiest to understand and use of the four printing systems, but it is not as powerful as CUPS.
  • No info on security was available for this presentation.
  • Samba (at least 2.08) does not seem to support pdq.

CUPS

  • Cups is recommended by the printing howto.
  • It supports a new internet standard: the Internet Printing Protocol (IPP).
  • IPP has been embraced by dozens of printer and printer server manufacturers and is supported by Microsoft Windows 2000.
  • IPP, and hence CUPS, supports access control, authentication, and encryption.
  • Since IPP runs over http, and CUPS supports IPP, users can check status information on a printer, manage their printers, and manage their jobs using their web browser.
  • Has several GUI's for configuring, or you can use a web browser.
  • Samba supports CUPS.
  • KDE 2.2 includes direct cups support. Of course, you can still use cups with KDE 2.1.
  • Can be configured to run on specific interfaces and specific ports.
  • The cups-drivers package contains drivers for roughly 1220 printers. This doesn't include printers that are compatible (i.e. There is no hp lj2100 driver, but this printer can use the hp lj6p drivers which are present.)
  • Has command line replacements for the lpr commands, but they support more options.
  • Cups is supported by: Caldera, Conectiva*, Debian, easyLinux*, HP-UX, IRIX, Linux Mandrake*, Peanut*, Red Hat, Solaris, SuSE, Tru64, UNIX, TurboLinux, and AIX. The ones with a * use cups as the primary printing system.

Installing CUPS


RPM installs

  • Get the rpms. If they are not included in your distribution, you can get them from http://www.cups.org/software.html.
  • The latest version as of 07/06/01 is 1.1.9. There are two rpms: cups and cups-devel. The cups-devel is optional. Some rpm versions also include a third and fourth rpm: libcups1 and libcups1-devel.
  • The latest version of the cups-drivers can be found at http://www.rpmfind.net/Linux/rpm2html/search.php?query=cups-drivers. The latest version is 1.0-31. Note this does require ghostscript.
  • SSL support requires the SSL libraries of course.
  • Warning: cups will overwrite your old printing system, especially lpr. Backup anything you want to save!
  • Remove the old lpr "rpm -e lpr". Install cups: "rpm -Uvh cups-1.1.8-Linux-2.4-intel.rpm" to install the rpm(s). Note if you have a problems installing cups, trying downloading a src rpm and compiling your own version (rpm --rebuild --target="i386" rpm-source-name).
  • Whichever way you install CUPS, don't forget to start it. This will vary depending on your system, but the command "/usr/sbin/cupsd" will usually work. Also, don't forget to put it in your startup files so when the occasion comes to reboot your machine, printing will still work.

Tarball installs


Installing a New Printer via your web browser


Go to http://127.0.0.1:631/printers

Note:

  • You can change the port cups listens on but this is not recommended.
  • If cups is listening there, you can also use a different IP address.
  • If localhost resolves to 127.0.0.1 (as it should), you can use localhost instead of 127.0.0.1 .

printers screen


Click on the add printer button

authetication

Enter root and root's password.


First page of the Add printer "wizard"

  • Now enter a name, location, and description.
  • The location and description are optional but recommended.
  • Here, you can see I entered myprinter, next to the computer, and optra E312. Enter whatever is appropriate for your printer.

printer wizard first page


Second page of the Add printer "wizard"

  • Select the device the printer is attached to. In my case, it is my parallel port #1 (Ignore the Epson part).
  • The various options are: Appsocket/HP JetDirect; Internet Printing Protocol (another cups printer, or perhaps a Windows 2000 shared printer); LPD/LPR Host or printer; parallel ports (there is a listing for each one); serial ports (there is a listing for each one); or Unknown Network Device (SMB).
  • Note for all attachments except parallel there is an extra step. This is discussed shortly.

printer wizard second page


Third page of the Add printer "wizard"

  • Select the manufacturer of your printer (or compatible printer driver). In my case, the Optra E312 is made by Lexmark.
  • The printer drivers mentioned above have 40 different manufacturers.

printer wizard third page


Fourth page of the Add printer "wizard"

  • Select the model/driver of/for your printer (or compatible printer driver). In my case, it is the Optra E312

printer wizard fourth page


Fifth (and last) page of the Add printer "wizard"

  • If everything went well, this is what you'll see.

printer wizard fourth page


The New Printer is added!

printer wizard fourth page


Special Ports

  • For ports other than parallel, an extra page comes between the second and third page. An example is below.
  • To print to an lpd printer server, enter "lpd://hostname/queue" . You must know the hostname and the queue name. These are the names of the printer server, not your machine.
  • For things like an HP JetDirect (assuming you aren't using it's lpr support), enter "socket://hostname". Note at least some versions of the HP JetDirect firmware do not correctly implement IPP.
  • For print servers than support IPP, you can use either "http://hostname:631/ipp/printername: or "ipp://hostname/ipp/printername". I haven't used this method myself yet.
  • For serial ports, set the settings to the correct value. The command "lpinfo -v" should at least tell you the baud rate.
  • To print to a SMB server (such as windows), be sure to read the Samba and cups section first! There are several ways to specify a SMB server. See the table below.
Username and password Not requiredUsername and password required
This machine is in the same workgroupsmb://server/sharenamesmb://username:password@server/sharename
This machine is in a different workgroupsmb://workgroup/server/sharenamesmb://username:password@workgroup/server/sharename

The page for everything except parallel ports and serial ports:

printer wizard 2.5 non serial page

Here is the page for serial ports:

printer wizard 2.5 serial page


The cupsd.conf file

  • Like most servers in Unix, much of cups can be configured/controlled through the cupsd.conf file. Usually, this will be found in /etc/cups.
  • There are six different configuration files for cups. cupsd.conf is the only one you would normally edit by hand.
  • cupsd.conf has 69 different directives. We will only cover a few of them. See the official documentation for cups for information on the rest.

Portwalling

  • By default, cups will listen on every interface. Unless you want to offer your printer to the world, you must block this. Although you can firewall this (and that is a good idea anyway), you can also not have cups listen on your internet connection. This also prevents any possibility of a root exploit from the internet. Of course, you still have internal crackers to worry about...
  • In cups.conf, comment out the lines:
    Port 80
    Port 631
  • Replace them with:
    Listen 127.0.0.1:631
    Listen lan_ip_address:631
    Listen 127.0.0.1:80
    Listen lan_ip_address:80
  • Still leaves a UDP port 631 open to the world though. The simplest way to close this port is by turning browsing off, but it may cause problems if you have other cups servers (and maybe clients?). The line for this is:
    Browsing off
  • Another way to limit access to UDP on port 631, but less secure, is to add the following lines:
    BrowseAddress lan_ip_broadcast_address
    BrowseAddress 127.0.0.1
    BrowseAllow from 127.0.0.1
    BrowseAllow from lan_ip_subnet (i.e. 192.168.1.0/24)
    BrowseOrder deny,allow

Cups and Samba

  • Starting with Samba version 2.06, cups is supported.
  • If you are going to print to SMB servers, or allow clients to send print jobs to your cups server using the SMB protocol, you must install and configure Samba.
  • To configure Samba for CUPS (windows users can print to your cups server), edit the smb.conf file and replace the existing printing commands and options with the line:
    printing = cups
  • Versions of Samba older than 2.06 and broken versions of Samba (2.06 and 2.07 have been reported) require the following lines in your smb.conf file:
    printing = sysv
    printcap name = lpstat
    print command = lp -d %p -o raw %s; rm -f %s
  • You will also need to add a [printer] section to your smb.conf file. (Otherwise, you are sharing any printers!) Here is a sample one I use:

    # NOTE: If you have a CUPS print system there is no need to
    # specifically define each individual printer.
    # You must configure the Samba printers as "Generic PostScript Printer"
    # on your Windows clients.
    # If you wish to configure the printers directly on the Windows clients
    # (i.e. use the windows drivers on the clients) you must swap the
    # 'print command' line below with the commented one.
    [printers]
       comment = All Printers
       path = /var/spool/Samba
       browseable = yes
    # to allow user 'guest account' to print.
       guest ok = yes
       writable = no
       printable = yes
       create mode = 0700
    # =====================================
    # print command: see above for details.
    # =====================================
       print command = lpr-cups -P %p -o raw %s # using client side printer drivers.
    #   print command = lpr-cups -P %p %s # using cups own drivers (use
    #generic PostScript on clients).
    #       lpr -P %p %s
       lpq command = lpstat -o %p
       lprm command = cancel %p-%j
  • To configure CUPS for SAMBA (cups can use printers on windows machines), run the following command:
    ln -s `which smbspool` /usr/lib/cups/backend/smb

    Your Samba installation or cups installation *MAY* have already done this, so check for link first.

Cups command line tricks and tips

  • Because cups has a command line equivalent to the lp and lpr commands (I prefer lpr), all the commands that worked with the old LPD print system still work exactly the same.
  • Only the lpr version of the commands will be presented here. The equivalent lp commands do differ in some cases.
  • Cups, however, adds a bunch of new options you can use that are not present in LPD.
  • Only a few of the options will be presented here. See the docs for the full list.

Basic print commands

  • To print to your default printer, use the command: "lpr filename".
  • To print to a specific printer, use the command "lpr -P printername filename".
  • To see what printers are setup, you can use the web page (see above), or use the command "lpstat -d -p". The -d tells you the default printer, and the -p lists the printers available, and their status.

Advanced print commands - Paper orientation and size

With cups, you can easily change the printout paper orientation.

  • To print portrait, you don't need any special options. It's the default.
  • To print landscape, "lpr -o landscape filename"

Cups also allows you to specific media size, type and source. Of course, your printer must support the options you choose. From the cups manual, the most common are:

  • Letter - US Letter (8.5x11 inches, or 216x279mm)
  • Legal - US Legal (8.5x14 inches, or 216x356mm)
  • A4 - ISO A4 (8.27x11.69 inches, or 210x297mm)
  • COM10 - US #10 Envelope (9.5x4.125 inches, or 241x105mm)
  • DL - ISO DL Envelope (8.66x4.33 inches, or 220x110mm)
  • Transparency - Transparency media type or source
  • Upper - Upper paper tray
  • Lower - Lower paper tray
  • MultiPurpose - Multi-purpose paper tray
  • LargeCapacity - Large capacity paper tray

To use these options, the command is "lpr -o media=option1,option2,etc filename". Multiple options are allowed, and they are not case sensitive. Note there are no spaces in the list!


Advanced print commands - Duplexing

  • If your printer supports its, CUPS can do duplexing from the command line.
  • To do duplexing, the command is "lpr -o duplexingoption filename" where duplexingoption is one of the following.
  • "sides=two-sided-short-edge" is the option suitable for landscape pages.
  • "sides=two-sided-long-edge" is the option is suitable for portrait pages.
  • CUPS default is to print on only one side of the paper, so while there is an option for this ("sides=one-sided"), you don't need it.

If your printer doesn't support duplexing, but you want to fake it, CUPS again has options to help.

  • Use the "-o page-set=set option" to select printing just the even or just the odd pages.
  • The set value is either even, for even numbered pages, or odd, for odd numbered pages.
  • The default is to print all pages. There is really is no option for this.

Advanced print commands - Range of Pages

  • The default is to print every page.
  • The "-o page-ranges=pages" option selects a range of pages for printing.
  • "pages" can be a hyphenated range such as 1-4, or a comma separated (no spaces!) list of pages, such as 3,5,6 or both, such as 3,4-6,8.
  • The pages are always printed in ascending order, regardless of the order they are listed in.

Advanced print commands - Combining Options

  • Cups allow multiple options to be combined. Simply add another -o to the command.
  • Suppose you wanted to print pages 3-4, and 6 landscape from the file myletter to the printer myprinter.
  • The command is "lpr -P myprinter -o landscape -o page-ranges=3-4,6 myletter".
root@zhang:~# dpkg --list | grep -i python ii libpython3-stdlib:amd64 3.12.3-0ubuntu2 amd64 interactive high-level object-oriented language (default python3 version) ii libpython3.12-minimal:amd64 3.12.3-1ubuntu0.4 amd64 Minimal subset of the Python language (version 3.12) ii libpython3.12-stdlib:amd64 3.12.3-1ubuntu0.4 amd64 Interactive high-level object-oriented language (standard library, version 3.12) ii libpython3.12t64:amd64 3.12.3-1ubuntu0.4 amd64 Shared Python runtime library (version 3.12) ii python-apt-common 2.7.7ubuntu4 all Python interface to libapt-pkg (locales) ii python-babel-localedata 2.10.3-3build1 all tools for internationalizing Python applications - locale data files ii python3 3.12.3-0ubuntu2 amd64 interactive high-level object-oriented language (default python3 version) ii python3-apport 2.28.1-0ubuntu3.3 all Python 3 library for Apport crash report handling ii python3-apt 2.7.7ubuntu4 amd64 Python 3 interface to libapt-pkg ii python3-aptdaemon 1.1.1+bzr982-0ubuntu44 all Python 3 module for the server and client of aptdaemon ii python3-aptdaemon.gtk3widgets 1.1.1+bzr982-0ubuntu44 all Python 3 GTK+ 3 widgets to run an aptdaemon client ii python3-attr 23.2.0-2 all Attributes without boilerplate (Python 3) ii python3-babel 2.10.3-3build1 all tools for internationalizing Python applications - Python 3.x ii python3-blinker 1.7.0-1 all Fast, simple object-to-object and broadcast signaling (Python3) ii python3-bpfcc 0.29.1+ds-1ubuntu7 all Python 3 wrappers for BPF Compiler Collection (BCC) ii python3-brlapi:amd64 6.6-4ubuntu5 amd64 Braille display access via BRLTTY - Python3 bindings ii python3-cairo 1.25.1-2build2 amd64 Python3 bindings for the Cairo vector graphics library ii python3-certifi 2023.11.17-1 all root certificates for validating SSL certs and verifying TLS hosts (python3) ii python3-cffi-backend:amd64 1.16.0-2build1 amd64 Foreign Function Interface for Python 3 calling C code - runtime ii python3-chardet 5.2.0+dfsg-1 all Universal Character Encoding Detector (Python3) ii python3-click 8.1.6-2 all Wrapper around optparse for command line utilities - Python 3.x ii python3-colorama 0.4.6-4 all Cross-platform colored terminal text in Python - Python 3.x ii python3-commandnotfound 23.04.0 all Python 3 bindings for command-not-found. ii python3-configobj 5.0.8-3 all simple but powerful config file reader and writer for Python 3 ii python3-cryptography 41.0.7-4ubuntu0.1 amd64 Python library exposing cryptographic recipes and primitives (Python 3) ii python3-cups:amd64 2.0.1-5build6 amd64 Python3 bindings for CUPS ii python3-cupshelpers 1.5.18-1ubuntu9 all Python utility modules around the CUPS printing system ii python3-dateutil 2.8.2-3ubuntu1 all powerful extensions to the standard Python 3 datetime module ii python3-dbus 1.3.2-5build3 amd64 simple interprocess messaging system (Python 3 interface) ii python3-debconf 1.5.86ubuntu1 all interact with debconf from Python 3 ii python3-debian 0.1.49ubuntu2 all Python 3 modules to work with Debian-related data formats ii python3-defer 1.0.6-2.1ubuntu1 all Small framework for asynchronous programming (Python 3) ii python3-distro 1.9.0-1 all Linux OS platform information API ii python3-distro-info 1.7build1 all information about distributions' releases (Python 3 module) ii python3-distupgrade 1:24.04.26 all manage release upgrades ii python3-gdbm:amd64 3.12.3-0ubuntu1 amd64 GNU dbm database support for Python 3.x ii python3-gi 3.48.2-1 amd64 Python 3 bindings for gobject-introspection libraries ii python3-httplib2 0.20.4-3 all comprehensive HTTP client library written for Python3 ii python3-ibus-1.0 1.5.29-2 all Intelligent Input Bus - introspection overrides for Python (Python 3) ii python3-idna 3.6-2ubuntu0.1 all Python IDNA2008 (RFC 5891) handling (Python 3) ii python3-jinja2 3.1.2-1ubuntu1.2 all small but fast and easy to use stand-alone template engine ii python3-json-pointer 2.0-0ubuntu1 all resolve JSON pointers - Python 3.x ii python3-jsonpatch 1.32-3 all library to apply JSON patches - Python 3.x ii python3-jsonschema 4.10.3-2ubuntu1 all An(other) implementation of JSON Schema (Draft 3, 4, 6, 7) ii python3-jwt 2.7.0-1 all Python 3 implementation of JSON Web Token ii python3-launchpadlib 1.11.0-6 all Launchpad web services client library (Python 3) ii python3-lazr.restfulclient 0.14.6-1 all client for lazr.restful-based web services (Python 3) ii python3-lazr.uri 1.0.6-3 all library for parsing, manipulating, and generating URIs ii python3-louis 3.29.0-1build1 all Python bindings for liblouis ii python3-markdown-it 3.0.0-2 all Python port of markdown-it and some its associated plugins ii python3-markupsafe 2.1.5-1build2 amd64 HTML/XHTML/XML string library ii python3-mdurl 0.1.2-1 all Python port of the JavaScript mdurl package ii python3-minimal 3.12.3-0ubuntu2 amd64 minimal subset of the Python language (default python3 version) ii python3-netaddr 0.8.0-2ubuntu1 all manipulation of various common network address notations (Python 3) ii python3-netifaces:amd64 0.11.0-2build3 amd64 portable network interface information - Python 3.x ii python3-netplan 1.1.1-1~ubuntu24.04.1 amd64 Declarative network configuration Python bindings ii python3-oauthlib 3.2.2-1 all generic, spec-compliant implementation of OAuth for Python3 ii python3-olefile 0.46-3 all Python module to read/write MS OLE2 files ii python3-pexpect 4.9-2 all Python 3 module for automating interactive applications ii python3-pil:amd64 10.2.0-1ubuntu1 amd64 Python Imaging Library (Python3) ii python3-pkg-resources 68.1.2-2ubuntu1.1 all Package Discovery and Resource Access using pkg_resources ii python3-problem-report 2.28.1-0ubuntu3.3 all Python 3 library to handle problem reports ii python3-ptyprocess 0.7.0-5 all Run a subprocess in a pseudo terminal from Python 3 ii python3-pygments 2.17.2+dfsg-1 all syntax highlighting package written in Python 3 ii python3-pyparsing 3.1.1-1 all alternative to creating and executing simple grammars - Python 3.x ii python3-pyrsistent:amd64 0.20.0-1build2 amd64 persistent/functional/immutable data structures for Python ii python3-requests 2.31.0+dfsg-1ubuntu1 all elegant and simple HTTP library for Python3, built for human beings ii python3-rich 13.7.1-1 all render rich text, tables, progress bars, syntax highlighting, markdown and more ii python3-serial 3.5-2 all pyserial - module encapsulating access for the serial port ii python3-six 1.16.0-4 all Python 2 and 3 compatibility library ii python3-software-properties 0.99.49.1 all manage the repositories that you install software from ii python3-speechd 0.12.0~rc2-2build3 all Python interface to Speech Dispatcher ii python3-sss 2.9.4-1.1ubuntu6.2 amd64 Python3 module for the System Security Services Daemon ii python3-systemd 235-1build4 amd64 Python 3 bindings for systemd ii python3-typing-extensions 4.10.0-1 all Backported and Experimental Type Hints for Python ii python3-tz 2024.1-2 all Python3 version of the Olson timezone database ii python3-update-manager 1:24.04.9 all Python 3.x module for update-manager ii python3-urllib3 2.0.7-1ubuntu0.1 all HTTP library with thread-safe connection pooling for Python3 ii python3-wadllib 1.3.6-5 all Python 3 library for navigating WADL files ii python3-xdg 0.28-2 all Python 3 library to access freedesktop.org standards ii python3-xkit 0.5.0ubuntu6 all library for the manipulation of xorg.conf files (Python 3) ii python3-yaml 6.0.1-2build2 amd64 YAML parser and emitter for Python3 ii python3.12 3.12.3-1ubuntu0.4 amd64 Interactive high-level object-oriented language (version 3.12) ii python3.12-minimal 3.12.3-1ubuntu0.4 amd64 Minimal subset of the Python language (version 3.12) root@zhang:~# sudo apt remove python3.12
最新发布
06-07
<think>我们首先需要明确:用户想要卸载通过apt安装的Python3.12版本,同时避免影响系统依赖。根据之前的引用内容,Python3.12可能是通过两种方式安装的:1)从deadsnakesPPA安装;2)从源码编译安装。但用户明确要求使用apt命令卸载,所以我们假设是通过PPA安装的。同时注意:引用[4]中提到“不推荐将python3.12替代系统自带版本,这样会导致系统本身出现错误。”,所以我们卸载时要确保只卸载Python3.12,而不影响系统默认的Python(如Python3.10或3.8)。步骤:1.确认Python3.12是通过apt安装的(即通过deadsnakesPPA安装)2.使用apt命令卸载Python3.12及其相关包3.移除可能存在的替代版本链接(如果用户按照引用[2]设置了update-alternatives)4.清理依赖(可选)但是,卸载时需要注意:不要卸载任何系统默认的Python版本,也不要卸载其他可能依赖的包。具体操作:步骤1:卸载Python3.12命令:`sudoaptremovepython3.12`步骤2:卸载与Python3.12相关联的包,例如python3.12-venv(如果安装了)命令:`sudoaptremovepython3.12-venvpython3.12-dev`#根据实际情况,用户可能还安装了其他如dev包,但一般venv包是常见的但是,我们也可以使用自动删除来清理不再需要的依赖(注意:这个操作可能会删除一些其他包,所以建议先查看将被删除的包列表):命令:`sudoaptautoremove`步骤3:如果之前使用update-alternatives设置了python的替代版本,需要手动移除命令:`sudoupdate-alternatives--removepython/usr/bin/python3.12`但是,注意:在引用[2]中,添加替代版本时使用的是:sudoupdate-alternatives--install/usr/bin/pythonpython/usr/bin/python3.121因此,移除时使用上述命令即可。然而,如果用户并没有设置替代版本,那么这一步可以省略。步骤4:如果不再需要deadsnakesPPA,可以移除(可选)移除PPA并不会卸载已经安装的软件,但会阻止后续更新。移除命令:sudoadd-apt-repository--removeppa:deadsnakes/ppa注意:如果系统中还有其他来自deadsnakesPPA的Python版本,则不建议移除PPA。但是,用户仅要求卸载Python3.12,所以我们只卸载该版本。安全提示:在执行卸载前,建议先确认当前系统默认的Python版本(使用`python3--version`或`python--version`),确保我们不会影响到默认版本。完整步骤:1.检查已安装的Python3.12相关包dpkg--list|greppython3.122.卸载Python3.12主包sudoaptremovepython3.123.卸载与Python3.12关联的其他包(比如venv,dev等)sudoaptremovepython3.12-*4.自动移除不需要的包(可选,但建议先检查列表)sudoaptautoremove5.移除替代链接(如果有)sudoupdate-alternatives--removepython/usr/bin/python3.12注意:第3步中使用通配符卸载所有以python3.12开头的包,这样确保清理干净。但是,在执行sudoaptremovepython3.12-*时要小心,确保这些包确实可以卸载,不会误删其他版本。由于用户是通过apt安装的,所以使用apt卸载是安全的。另外,在引用[5]中提到在Ubuntu24.04中,有些库是通过apt安装的(如python3-pycryptodome),但这里我们只卸载Python3.12,不会影响这些库,因为这些库是安装在系统Python环境中的(可能是系统默认Python)。但是,有一点要注意:如果用户使用Python3.12创建了虚拟环境,这些虚拟环境在卸载Python3.12后将无法使用,需要用户自行删除。最后,我们提供详细的步骤:</think> ### 如何在 Ubuntu 上安全卸载 Python 3.12 根据引用内容,Python 3.12 主要通过两种方式安装: - **PPA安装**(deadsnakes PPA)[^3][^4] - **源码编译安装**[^1][^2] 以下是针对不同安装方式的卸载方法: --- #### 🛑 方法 1:卸载通过 PPA 安装的 Python 3.12 ```bash # 1. 卸载 Python 3.12 主程序 sudo apt remove python3.12 # 2. 卸载关联包(venv等) sudo apt remove python3.12-venv python3.12-dev # 3. 清理残留配置 sudo apt autoremove --purge ``` #### ⚠️ 注意事项 1. **避免误删系统 Python**: ```bash # 查看系统默认 Python 版本(通常为 python3.10/python3.8) ls /usr/bin/python3.* ``` 切勿卸载系统默认 Python(如 `python3.10`),否则会导致系统崩溃[^4] 2. **移除替代链接**(如果设置过): ```bash sudo update-alternatives --remove python /usr/bin/python3.12 ``` --- #### 🔧 方法 2:卸载源码编译安装的 Python 3.12 ```bash # 1. 定位安装目录(通常为 /usr/local/bin/python3.12) which python3.12 # 2. 删除安装文件 sudo rm -rf /usr/local/bin/python3.12* sudo rm -rf /usr/local/lib/python3.12 # 3. 删除符号链接 sudo rm -f /usr/local/bin/pip3.12 sudo rm -f /usr/local/bin/idle3.12 # 4. 清理源码目录(如存在) cd ~ && rm -rf Python-3.12.0* ``` --- #### 🧹 卸载后检查 ```bash # 验证是否卸载成功 python3.12 --version # 应显示 "command not found" # 检查关联库(如Crypto/Pandas等库是否受影响) apt list --installed | grep python3.12 ``` --- #### ⚠️ 重要安全提示 1. **切勿卸载系统默认 Python**(如 `python3.10` 或 `python3.8`),否则会导致系统崩溃[^4][^2] 2. 虚拟环境不影响系统安全,可直接删除相关目录: ```bash rm -rf ~/.venv # 删除用户虚拟环境 ``` 3. 通过 APT 安装的 Python 库(如 `python3-pandas`)会自动随 Python 卸载[^5] 4. **PPA 保留问题**:若不再需要,可移除 PPA 源: ```bash sudo add-apt-repository --remove ppa:deadsnakes/ppa ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值